Sunteți pe pagina 1din 77

The importance of object-

oriented programming in the


era of mobile application
development
By
Dr Noor Azah Samsudin
azah@uthm.edu.my
Universiti Tun Hussein Onn Malaysia
Introduction to Object-Oriented
Programming
Introduction
Object-oriented programming (OOP) is a way of organizing and
developing software.
The foundation of object-oriented programming languages goes back
to Simula and Smalltalk.
Concepts of OOP
Object versus class
Encapsulation and Inheritance
Object Communication (message sent versus method call)
Object vs. Class
Object is an entity. It contains attributes and provides services.
Object is created from a class.
Object is considered as an instance of a class.
Class is considered as a template from which objects are instantiated
Can create an object or many objects from a class.
Object vs. Class (Cont.)

Diagram 1: Class Car Diagram 2: MyCar as an Object

Car

Door
Seat
Type
Model

Drive
Stop
Lock
Unlock
Object
Analogy
Apple pie recipe and apple pies
Encapsulation and Inheritance
Encapsulation means both data and methods for an object are
contained inside the object.
Inheritance is a relationship between classes where one class is the
parent class (superclass) of its child class (subclass).
Encapsulation and Inheritance (Cont.)
In C++, for example, if we have a class Student, an object (say
Angelina) is created as follows:
Student Angelina;
We can then create a subclass Primary from its superclass Student as
follows:
class Primary: public class Student
Object Communications
Object communicate by sending messages
Object-Oriented Programming
Three important steps:
Develop class to establish the objects data and behavior
Create object or instance (instantiating)
Sending message to establish object communications
Object-Oriented Programming (Cont.)
class Hello {
public:
void DisplayDetail(); };
// class Hello
void Hello::DisplayDetail()
{
//display details of method
cout << " Hello World! ";
};
// method DisplayDetail
int main()
{ // create a new instance of a class Hello
Hello word;
// invoke a method
word.DisplayDetail();
return 0; };
Advantages of OOP
Reusability
Extensibility
Maintainability
Object and Class (Recall)
Class is a template from which objects are instantiated
Object is created (instantiated) from class

Car Name of the class


Door
Seat
Type Attributes/Data
Model
Drive
Stop Services/Methods/Functions
Lock
Unlock
Example

Class Sum

Sum Name of the class

Attributes/Data

Sum2No Services/Methods/Functions
DisplayDetails
Class Template
class class_name {
private:
variables_declaration;
public:
methods_declaration;
}
return_type class_name::method_name(parameters)
{
.
}
Class File vs Class Main
//Filename: Sum.cpp //Filename: SumDr.cpp
//This program declares a class Sum for //This program serves as a driver (main) for class Sum.
//summation of two numbers //It instantiates the class Sum and call method from class
#include <iostream.h> //Sum
class Sum { #include <iostream.h>
public: #include "Sum.cpp"
int Sum2No(int no1, int no2); int main()
void DisplayDetails(int x, int y); {
}; // class Sum // create a new instance of a class Sum
Sum Sumof2No;
int Sum::Sum2No(int no1, int no2) {
//return result of summation of 2 numbers // declaration of variables
return no1 + no2; int x, y;
}; // method Sum2No cout << "Type a number for variable x : ";
cin >> x;
void Sum::DisplayDetails(int x, int y) { cout << "Type a number for variable y : ";
// display output via method cin >> y;
cout << "\nThe sum of " << x << " and " << y; // invoke method
cout << " are " << Sum2No(x, y) << '\n'; Sumof2No.DisplayDetails(x, y);
}; // method DisplayDetails return 0;
}
Object-Oriented Programming
class Hello {
public:
void DisplayDetail(); };
// method implementation
void Hello::DisplayDetail()
{
//display details of method
cout << " Hello World! ";
};
// method DisplayDetail
int main()
{ // create a new instance of a class Hello
Hello word;
// invoke a method
word.DisplayDetail();
return 0; };
Header (*.h) vs. Implementation (*.cpp)

//Filename: Student.h //Filename: Student.cpp


//The declaration of attributes & //The implementation of methods in
//methods of class Student //class Student
class Student { #include <iostream.h>
private: #include Student.h
char Name[25]; void Student::SetName() {
public: cout << "Enter student name: ";
void SetName(); cin >> Name;
void GetName(); }; // method SetName
}; // class Student void Student::GetName() {
cout << " \nStudent name : " << Name;
cout << "\n";
}; // method GetName
Header (*.h) vs Implementation (*.cpp) (Cont.)

/Filename: StudentD.cpp
//This program serves as a driver (main) for class Student.
//It instantiates the class Student and call methods from class Student
#include "Student.cpp"
int main()
{
// create a new instance of student
Student student1;
// invoke methods
student1.SetName();
student1.GetName();
return 0;
};
Constructor, Destructor and Data
Structures
Constructor and Destructor
A constructor is used as a method to initialize the data of an object
once it has been instantiated from the class.
A destructor is used as a method to delete the object once it has
been used.
Constructor and Destructor (Cont.)
class class_name {

private: variables_declaration;
public: class_name; // declaration of the constructor
public:~class_name;// declaration of the destructor
public: methods_declaration;
};
Use of Constructor and Destructor
class Point{ class Point{
private: private:
int x, y; // point coordinate
int x, y;
public:
public:
Point(); // c1
void setX(int val); Point(int val1, int val2); //c2
void setY(int val); ~Point(); // destructor
int getX(); void setX(int val);
void setY(int val);
int getY();
int getX();
}; // class Point
int getY();
}; // class Point
Data Structures struct
struct struct_name struct Student_Data
{ {
member_declaration; char Name[20];
member_declaration; char Course[30];
. int Result:
}; };

struct Student_Data stdata1, stdata2;


Data Structures struct (Cont.)

struct struct_name struct Student_Data


{ {
member_declaration; char Name[20];
member_declaration; char Course[30];
. int Result:
}variables_declaration; }stdata1, stdata2;
Data Structures typedef
typedef struct Student_Data Student_Data;

Student_Data stdata3, stdata4, stdata5;

cout << Enter student name: ;


cin >> stdata1.Name;
Information Hiding and
Encapsulation
Encapsulation
Encapsulation means both data and methods for an object are
contained inside the object.
When an objects data is inside the object, the object can protect that
data against use (or misuse) by other objects.
This concept is also known as information hiding.
Information hiding means that only operations that preserve
information correctness are available to the client, so that instead of
considering the implementation of a certain operation, a client simply
uses the existing one.
Encapsulation (Cont.)
C++, for example, uses the terms private, protected and public to
encapsulate the data inside the class.
These terms give the access control over data inside the class.
Most often, data is declared private in order to hide (encapsulate) the
data.
Access to the data inside the class is given via the methods (services)
provided by the class.
Encapsulation (Cont.)

Person From this diagram, a class Person introduces two


data: Name and DateofBirth. These data can be
Name
DateofBirth used (or manipulated) via the methods.
For example, data Name is manipulated by
SetName
GetName
methods SetName and GetName and data
SetDateofBirth DateofBirth is manipulated by methods
GetDateofBirth
FindAge
SetDateofBirth and GetDateofBirth.
Method FindAge, on the other hand, can use
data DateofBirth to find the age of a particular
person.
Container and Iterator
Containers refer to collection of classes
Eg to hold collection of objects through linked list
Common services include searching, sorting, testing an item to
determine if it is a member of the collection
Example arrays, stacks, queues, trees and linked lists
Pointer (Recall)
int *Ptr;
Ptr = 0;
Ptr = NULL; // equivalent to Ptr = 0
Ptr = &count; // Ptr containing the address of count

Note that symbol * is used to indicate the pointer declaration and


symbol & is used to indicate the address of the pointer.
Pointer (Recall) (Cont.)
ThisPoint apoint; ThisPoint *Ptr;
apoint.DisplayDetails(); Ptr->DisplayDetails();
Overloading
Operator Overloading
Can redefine (overload) operators in C++ to work with new types
For example operator (+) can be overloaded in C++
In fact, most of C++ operators can be overloaded
In C++ can overload the operator by defining a function whose name
is operator followed by the operator symbol

int operator+(int a, int b)


{ ..
}
Operator Overloading (Cont.)
Can Redefine Functions Add and Remove using Operators
Overloading
Function Add becomes operator +=
Function Remove becomes operator -=
Example: Using Operator Overloading
class StudentList { class StudentList {
private: private:
struct ListNode { struct ListNode {
Student astudent; Student astudent;
ListNode *next; ListNode *next;
}; };
ListNode *head; ListNode *head;
ListNode *tail; ListNode *tail;
public: public:
StudentList(); StudentList();
~StudentList(); ~StudentList();
int IsEmpty(); int IsEmpty();
// adding Student object // adding Student object
void Add(Student newstudent); void operator+=(Student newstudent);
//removing Student object //removing Student object
void Remove(char name[25]); void operator-=(char name[25]);
void DisplayList(); void DisplayList();
}; // class StudentList }; // class StudentList
Example (Cont.)
void StudentList::Add(Student void StudentList::operator+=(Student
newstudent) newstudent)
{ {

}; //method Add
}; //method Add aka +=

void StudentList::Remove(char
name[25]) void StudentList::operator-=(char
{ name[25])
{
}; // method Remove
}; // method Remove aka -=
Example (Cont.)
#include <iostream.h> #include <iostream.h>
#include "StdList.cpp" #include "StdList.cpp"
int main() { int main() {
const int size = 5; const int size = 5;
int i; int i;
Student newstudent; Student newstudent;
StudentList alist; StudentList alist;
char name[25]; char name[25];

cout <<"Inserting " <<size <<" objects\n"; cout <<"Inserting " <<size <<" objects\n";
for (i=0; i<size; i++) { for (i=0; i<size; i++) {
//create an instance of class Student //create an instance of class Student
newstudent.SetData(); newstudent.SetData();
//add an instance of class Student to the list //add an instance of class Student to the list
alist.Add(newstudent); alist+=newstudent;
} }
alist.DisplayList(); alist.DisplayList();
cout <<"deleting\n"; cout <<"deleting\n";
cout<<"Enter name to be deleted : "; cout<<"Enter name to be deleted : ";
cin>>name; cin>>name;
alist.Remove(name) ; alist-=name ;
cout <<"after removing 1 object\n"; cout <<"after removing 1 object\n";
alist.DisplayList(); alist.DisplayList();
return 0; }; return 0; };
Demo: Operators for Remove and Add Functions
Class Student
Class StudentList using Operator += for Add Function and Operator -=
for Remove Function
Inheritance
Inheritance
Inheritance is a relationship between classes where one class is the
parent class (superclass) of its child class (subclass).
Inheritance is also used to communicate the concept that one class
can inherit part of its behaviour and data from another class.
For example, a subclass of a program can inherit some code from its
superclass. In particular, in specification B inherits A, class B
contains the data and methods defined for class A in addition to
those defined for B.
Inheritance (Cont.)
Inheritance can also be divided into two categories: single inheritance
and multiple inheritance.
Single inheritance means that a subclass may inherit instance
variables and methods of a single parent class, possibly adding some
methods and instance variables of its own. Once the new class
subclasses the other class, it does not need to re-implement the basic
functionality of its superclasses but needs only to add its own
specialized behaviour and state.
Multiple inheritance means that a subclass may inherit instance
variables and methods from multiple parent classes.
Inheritance (Cont.)
Careful: There is a tendency to be confused between the concept of
inheritance and object.
Recall that an object is created (instantiated) from a class.
Inheritance, is created via the relationships between subclasses and
superclass.
An object is created in C++ as follows:
Student Angelina;
A subclass Primary is created as follows:
class Primary: public class Student
Inheritance (Cont.)
C++ permits a new class to be created from an existing class by
inheritance.
The standard C++ syntax for using inheritance in a class is as follows:
class derived_class_name : public base_class_name
{
private: variables_declaration;
public: methods_declaration; //may include constructor declaration
};
Inheritance (Cont.)
Example: Recall Class Point
Then can use class Point to introduce class Circle using inheritance as
follows:
class Circle: public Point {
private :
float Result;
public:
Circle(); //constructor declaration
void SetPoint();
void GetResult();
};
Polymorphism
Polymorphism
Polymorphism is a term used for more than one method that has the
same name.
The methods must belong to different kinds of objects.
Polymorphism also allows significant code sharing and code reuse.
Polymorphism (Cont.)
The compiler determines which function to invoke depending on the
object from which it is instantiated
This invocation is also known as early binding (static binding)
For dynamic binding, C++ introduces virtual function
Virtual Function
For polymorphism, C++ also introduces virtual function for
abstraction and dynamic binding.
Virtual functions are used in C++ to ensure that the correct function is
called from the appropriate derived class.
This is done at run time where dynamic binding occurs.
Virtual Function (Cont.)
class ThisCircle: public ThisPoint {
private:
float Result;
public:
void SetPoint();
void GetResult();
virtual void DisplayDetails(); //changed it to virtual function
};

Note: word virtual is a C++ reserved word for virtual function


Summary of Object-oriented programming
Object vs Class
Object-Oriented Concepts
Encapsulation (private data; public methods)
Inheritance (subclass; superclass)
Polymorphism (polymorphic function)
Advantages of Object-Oriented Approach
Reusability
Extensibility
Maintainability
Why Mobile Application?
Moving towards using mobile devices

Replacing personal computers

Common method for accessing the Internet

Portable (palm size, pocket size), more powerful

Google Android, Apple IOS, Windows Phone


Motivation
Billion of people use mobile devices; use smartphones; By 2020 6 billion
smartphone connections

Revenues from app stores

Free mobile app downloads

Spend more time in apps

Digital advertising
History of mobile applications
Integrated into our daily lives

Make and receive calls, send text messages

Smart phone not limited for communication purposes,


multifunctional learn, earn, and fun
Mobile application
A mobile app is a computer program designed to run on mobile
devices such as smartphones and tablet computers.
Gathering requirements
Use case

User data:
Users
Tasks
location or context
info requirement to complete task
info available to users
info accessibility or delivery
Designing
Illustrates all of the steps

Task flow user interaction: how, when? Navigable?

A set of mobile-specific server side resources requirements to handle


data delivery

Extensive client side logic to process data returned by the service


Development
Mobile app web view supports remote content (authored using
Apache Trinidad or ADF Faces Rich Client components) not
supporting offline use.

Applications authored in MAF AMX integrate with device services,


enabling users to view files, utilize GPS services, allows collaboration

MAF AMX set data visualization tools, supports offline use (transfer
data from remote source and store it locally, enabling end users to
view info when they are not connected
Development
A set of wizards and editors that
build basic application itself
application features that are implemented from MAF AMX and local HTML
content

Descriptor files for configuring the application and incorporating its


application features, a set of default images for splash screens,
springboards, navigation bar items
Deployment
Publish it to end users

Testing and debugging

Deploy to a mobile device or simulator (testing)

Package for distribution to application markets (Apple App Store,


Google Play)
Deployment
Create a deployment configuration describes target platform and its
devices and simulators

Deployment configuration - select splash screen, launch icons used in


different orientations (landscape or portrait), different devices (phone
or tablets)
Testing and debugging
Optimize the application deploy the application in debug mode

Various simulators and devices

Review the debugging output (Oracle Enterprise Pack for Eclipse


(OEPE)) and platform-specific tools
Securing
Risks throughout application development process

Accesses remotely served data

Cross-site scripting, cross-site request forgery

APIs that generate strong password to secure access to the SQLite


database, encrypt and decrypt data
Securing
Security configuration
Select login server (Oracle Access Mobile and Social server, web page
protected by basic HTTP authentication mechanism)

Configure session management

Set the endpoint to the access control service (hosts applications user roles)

Redeploy, retest and debug


Mobile Application Frameworks
Oracle Mobile Application Framework is a hybrid mobile architecture

HTML5 user interface in the web view

Java business logic

Apache Cordova device features (GPS activities, e-mail)

Cross-platform technologies can build same for Android and iOS devices
Mobile Application Frameworks
Build same application for smartphones or tablets, thereby allowing
reusability of business logic and target various types of devices

Application features are essentially the building blocks of a mobile


application.

Each application feature performs a specific set of tasks

Can be grouped together to complement each other functionality

Example: Pair customer contacts with product inventory


Mobile App Frameworks
jQuery Mobile robust framework to build cross-mobile-platform app

Cordova/PhoneGap JavaScript APIs that connect to device native functions (camera,


compass, contacts, geolocation); build a mobile application without the native
programming language

Sencha Touch powered by HTML5 and CSS3, providing APIs, animations, and
components that are compatible with the current mobile platforms and browsers.

Ratchet a collection of User Interface and JavaScript plugins, providing reusable HTML
classes

Ionic, Lungo, jQT, Junior, Jo, Famo.us


Is it necessary to learn OOP to develop
mobile apps?
If you want to program just about anything these days, you should better
learn object-oriented programming

Learning object-oriented programming is crucial to all modern software


development including mobile apps.

Object can be the driving force in the programming industry.

Programming languages, scripting languages, application designs object-


based
Programming languages
iOS: Objective-C

Android: Java

Windows Phone: . NET (C#, Visual Basic)

BlackBerry: Java
Why has OOP become so important?
Emergence of the Internet as a place of business

Java network in mind, highly portable, graphical user interface

Programming, scripting languages and frameworks used to create


mobile applicatins rely on object technology: Java, C# .NET, Visual
Basic. NET, Objective-C, JavaScript, jQuery Mobile etc.
Conclusion
Procedural-oriented programming focus on procedures, with function
as the basic unit.

Object-oriented programming focus on components that the user


perceives, with objects as the basic unit.

Android is designed to run on many different types of devices


(phones, television, tablets).
Conclusion
Range of devices provides a huge potential audience for your
application

In order for your application to be successful on all of the devices:


Tolerate some feature variability
Provide flexible user interface (adapts different screen configurations)
THANK YOU

azah@uthm.edu.my

S-ar putea să vă placă și