abstraction file

25
Recap: Object Oriented Approach Four major elements: Abstraction Encapsulation Modularity Hierarchy Three minor elements: Typing Concurrency Persistence

Upload: tony-nguyen

Post on 11-Jan-2017

28 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Abstraction file

Recap: Object Oriented Approach

Four major elements: Abstraction Encapsulation Modularity Hierarchy

Three minor elements: Typing Concurrency Persistence

Page 2: Abstraction file

Abstraction

Car

Jeep

Motor Bike

LemonTruck

Apple

Banana

Mango

Chiku

Maruti

Wagon-R

Bajaj

Scooter

Page 3: Abstraction file

Abstraction

Dahl, Dijkstra, and Hoare suggest that “abstraction arises from a recognition of similarities between certain objects, situations, or processes in the real world, and the decision to concentrate upon these similarities and to ignore for the time being the differences” [42].

Page 4: Abstraction file

Abstraction

An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.

Page 5: Abstraction file
Page 6: Abstraction file

Abstraction: Perspective of User

ParentChild Name

ClassRoll No

Mobile No

EmployeeNameId No

Mobile NoTax Payer

NamePan No

Annual Return

Bank Customer

NamePan NoAcct No

Acct Type

Loyalty Program Member

NameCard NoPoints

Page 7: Abstraction file

Abstraction

An abstraction focuses on the outside view of an object and so serves to separate an object’s essential behavior from its implementation.

Deciding on the right set of abstractions for a given domain is the central problem in object-oriented design.

Page 8: Abstraction file

Abstraction

Abstraction or separation of behavior / implementation can be achieved by applying:

Principle of least commitment: the interface of an object provides its essential behavior, and nothing more

Principle of least astonishment: an abstraction captures the entire behavior of some object, no more and no less, and offers no surprises or side effects that go beyond the scope of the abstraction

Page 9: Abstraction file

Abstract Data Type (ADT)

An ADT is a mathematical model of a data structure that specifies the type of data stored, the operations supported on them and the type of parameters of the operations.

Specifies what each operation does, but not how it does it

Page 10: Abstraction file

Encapsulation

Encapsulation hides the details of the implementation of an object Typically, the structure of an object is hidden, as

well as the implementation of its methods.

Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior;

Encapsulation serves to separate the contractual interface of an abstraction and its implementation.

Page 11: Abstraction file

Encapsulation

Page 12: Abstraction file

Encapsulation

Encapsulation is most often achieved through information hiding (not just data hiding), which is the process of hiding all the secrets of an object that do not contribute to its essential characteristics

Page 13: Abstraction file

Data/Information Hiding

Access House

X ModifyX () Modify X

Private data – hidden from others

Function to safeguard the private data

Page 14: Abstraction file

Data Hiding

"... the purpose of hiding is to make inaccessible certain details that should not affect other parts of a system.“ [Ross et al, 1975]

Page 15: Abstraction file

Data Hiding

Data must be hidden/ privateRead access through read() functions Write access through write() functions For each data,

Allow both read and write Allow read only Allow write only No access

Page 16: Abstraction file

Encapsulation / Data Hiding

void modifyX (int newVal) {

if (newVal > 100) or (newVal < 0) { return error;}else X = newVal

}

void readX() { return X;

}

Page 17: Abstraction file

Abstraction & Encapsulation

Complementary concepts: The abstraction of an object should precede the decisions about its

implementation.

Once an implementation is selected, it should be treated as a secret of the abstraction and hidden from most clients i.e. encapsulated

“For abstraction to work, implementations must be encapsulated

Abstraction: focuses on the observable behavior of an object

Encapsulation: focuses on the implementation that gives rise to this behavior

Page 18: Abstraction file

Anatomy of a Class

PRIVATE PUBLIC

Data

Private Functions

Read/ Write Functions

Constructors/ Destructors

ADT Functions Pop(), Insert ()

Public Interface

Class • Realization of an ADT

• State /fields /data members

• Behavior /methods /member functions

• Public Interface: signatures (names, return types, argument types) of a class’s public member functions, only part of the class that can be accessed by a user of the class

Page 19: Abstraction file

Class

• It defines the data being stored and the operations supported by the objects that are instances of the class

• Every class must have two parts: • an interface – what? – Abstraction • an implementation – how? – Encapsulation

Page 20: Abstraction file

Abstraction Encapsulation

External Interface The interface of a

class captures only its outside view, encompassing abstraction of the behavior common to all instances of the class.

Internal Implementation The implementation of

a class comprises the representation of the abstraction as well as the mechanisms that achieve the desired behavior.

Class, Abstraction & Encapsulation

Page 21: Abstraction file

Abstraction Encapsulation

Exposes Generic / Generalized Features The interface of a

class is the one place where we assert all of the assumptions that a client may make about any instances of the class

Hides implementation details The implementation

encapsulates details about which no client may make assumptions.

Class, Abstraction & Encapsulation

In C++ a Class embodies both abstraction and encapsulation

Please read: http://www.tonymarston.co.uk/php-mysql/abstraction.txt

Page 22: Abstraction file

Point Product

class Point{

public:

Point (double xval, double yval);void move(double dx, double dy);double get_x() const;double get_y() const;

private:double x;double y;

};

class Product{

public:

Product();void read();bool is_better_than(Product b) const;void print() const;

private:string name;double price;int score;

};

Examples

Page 23: Abstraction file

Objects

Object Instantiation of a class

Initialization of Objects Constructors Called automatically every time an object is created, ensures proper

initialization Overcome the problem of improper initialization in procedural languages

Resource De-allocation Destructors Ensures de-allocation of resources before the object dies, or goes out of

scope Overcome memory leaks etc. in procedural languages

Page 24: Abstraction file

Objects

Life-cycle of an Object Born Healthy

Properly initialized by use of constructors

Lives Safely Using read/write functions, ensure data integrity

Dies Cleanly Using destructors

Page 25: Abstraction file

References

[Lafore] Chapter 1[Booch et al] Chapters 1 & 2[Deital & Deital] Chapter 3[Horstmann & Budd] Chapter 5http://www.tonymarston.co.uk/php-mysql/

abstraction.txt