Professional Documents
Culture Documents
Object - Oriented Principles
Object - Oriented Principles
Object-Oriented Principles
Robert P. Biuk-Aghai BiukDepartment of Computer and Information Science Faculty of Science and Technology University of Macau robertb@umac.mo
Overview
Why Object-Orientation? ObjectThe Object Model OO vs. Procedure -Oriented Approach ProcedureFundamental OO Concepts Examples Conclusions
Why Object-Orientation?
Why Object-Orientation?
The software crisis
n
Software is of poor quality Software is expensive to produce Software complexity is continuously increasing
30% of large software projects are cancelled before completion 50% of software projects are over budget by more than 200% The majority of completed projects deliver 60% or less of prescribed functionality Many delivered information systems are under - used underbecause they don meet user needs and/or expectations don t Legacy systems are a serious and growing bottleneck to organizational evolution
The ability of software products to perform their exact tasks, as defined by their specifications. [Mey97] specifications. The ability of software systems to react appropriately to abnormal conditions. [Mey97] conditions. Producing bug -free software Correcting software bugs
Robustness
n
Difficulties:
n n
The ability of software elements to serve for the construction of many different applications. [Mey97] applications. The ease of adapting software products to changes of specification. specification. [Mey97] Changing software is hard Hard to apply software outside its original intended use
Extendibility
n
Difficulties:
n n
The ease of combining software elements with others. The ease of transferring software products to various hardware and software environments The ease with which people of different backgrounds can learn to use software products The ability of a software system to place as few demands as possible on hardware resources
Portability
n
Ease of use
n
Efficiency
n
Object -Orientation
A way of thinking about a problem
n n
Regard the world as consisting of interacting objects Aim to understand objects, their properties, operations, and interactions with other objects
Build systems consisting of representations of objects Objects interact with each other
Objects Example
Loudspeaker Data Projector
Microphone PC
OHP Chair
Facilitates architectural and code reuse Models more closely reflect the real world
n n n
More accurately describe corporate data and processes Decomposed based on natural partitioning Easier to understand and maintain A small change in requirements does not mean massive changes in the system under development
Stability
n
ObjectObject-oriented analysis is a method of analysis that examines requirements from the perspective of the classes and objects found in the vocabulary of the problem domain. [Boo94]
ObjectObject-oriented design is a method of design encompassing the process of object-oriented objectdecomposition and a notation for depicting both logical and physical as well as static and dynamic models of the system under design. [Boo94]
ObjectObject-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships. [Boo94] relationships. C++ Java Smalltalk Eiffel etc.
Encapsulation
Abstraction
Modularity
Abstraction
Definition:
n
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. [Boo94]
Key concepts:
n
Concentrating only on essential characteristics Allows complexity to be more easily managed Abstraction is relative to the perspective of the viewer Many different views of the same object are possible
Hierarchy
Object Orientation
Source: Boo94
Examples of Abstraction
Employee
Customer
Product
Encapsulation
Definition:
n
Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behaviour; encapsulation serves to separate the contractual interface of an abstraction and its implementation. [Boo94]
Key concepts:
n
Packaging structure and behaviour together in one unit Makes objects more independent Objects exhibit an interface through which others can interact with it Hides complexity from an object clients object s
Protects object internal state from being changed by object s outside users Changes can be done to the behaviour implementation without affecting other objects
Source: Boo94
Example of Encapsulation
Hide implementation from clients
n
Modularity
Definition:
n
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. [Boo94]
Modules are cohesive (performing a single type of tasks) Makes modules more reusable Modules are loosely coupled (highly independent) Makes modules more robust and maintainable
Source: Boo94
Example of Modularity
The breaking up of something complex into manageable pieces
Order Entry
Order Fulfillment
Billing
Hierarchy
Definition:
n
Types of hierarchies:
n n n n n n
Hierarchy of Abstractions
Source: Boo94
Example of Hierarchy
Levels of abstraction
Increasing abstraction
Asset
BankAccount
Security
RealEstate
Savings
Checking
Stock
Bond
Decreasing abstraction
Classes at the same level of the hierarchy should be at the same level of abstraction
Emphasis is on doing things (algorithms) Large programs are divided into smaller programs (functions) Most of the functions share global data Data move openly around the system from function to function Functions transform data from one form to another Employs top-down approach in program design top-
Global Data
Global Data
Emphasis is on data rather than procedure Programs are divided into objects (abstracted into classes) classes ) Data is encapsulated in the objects Objects communicate with each other through functions (called methods ) methods) Follows bottom -up approach in program design bottom-
Fundamental OO Concepts
Object
An object represents an entity Objects can be:
n n
Class
A class represents a type of object A class is a definition of an object
n
A class is a template (or blueprint) for producing blueprint objects of a certain type Every object is an instance of a class Examples of classes:
n n n
A class is a set of objects that share a common structure and a common behaviour. [Boo94]
Attribute (Property)
An attribute is a named property of a class that describes the range of values that instances of the property may hold. [Boo99 ] Boo99] An attribute has a type that defines the type of its instances The given set of values of the attributes define s the defines state of the object Only the object itself should be able to change the value of its attributes
Example: Attributes
public class Employee { protected int employeeNo; protected String surName; protected String givenName; protected String jobTitle; protected int salary; }
Attributes
Operation (Method)
An operation is the implementation of a service that can be requested from any object of the class to affect behavio ur. [Boo99] behaviou Boo99] An operation can be:
n n
Question (does not change the value of the object) Command (may change the value of the object)
Example: Operations
public class Employee { protected int employeeNo; protected String surName; protected String givenName; protected String jobTitle; protected int salary; public void assignWork(Task t) {} public void doWork() {} public void submitResult(Result r) {} }
Operations
Object Definition
Definition:
n
An object has state, behaviour, and identity; the structure and behaviour are defined in their common class; the terms instance and object are interchangeable. [Boo94]
Example object:
n
Object State
Definition:
n
The state of an object encompasses all of the (usually static) properties of the object plus the current (usually dynamic) values of each of these properties. [Boo94]
Properties of an object are fixed (static) Property values of an object change (dynamic)
employeeNo: 1234 surName: "Chan" givenName: "Tai Man" jobTitle: "Tecnico 1a Classe" salary: 400
Behaviour
Definition:
n
Behaviour is how an object acts and reacts, in terms of its state changes and message passing. [Boo94] passing.
Updating value of property salary Assigning work (invoking method assignWork() in the corresponding object)
Example: Behaviour
Example of behaviour: object state change; salary of employee Chan Tai Man is increased
An action that updates any of the object property values s causes a change of the object state s
Identity
Definition:
n
Identity is that property of an object which distinguishes it from all other objects. [Boo94]
Distinct objects remain distinct even when all their property values are identical Every object has a unique object identifier Example: two Chan Tai Mans (in Macau, Portugal) Mans
oid = 1 oid = 2
Modeling Classes
Modeling more classes of the domain:
Class Relationships
Modeled classes can be related in multiple ways:
n n n n n n
Association, inheritance
Association
Association:
n
One-to-one: One-to-one: one instance of the first class associated with one instance of the second class One-to-many: One-to-many: one instance of the first class associated with many instances of the second class Many-toMany-to- many: many instances of the first class associated with many instances of the second class
Association Example
Example associations:
n
Employee is head of agency / agency is headed by employee (1:1) Employee works for agency / agency provides work for employee (1:N)
Association Example 2
SelfSelf -association:
n
Agency is associated to agency (parent agency) (0 (0 1 : 0 N) 0 Gabinete Direc Direco Departamento Diviso Sec Seco
Inheritance
Inheritance:
n
A relationship among classes, wherein one class shares the structure or behaviour defined in one or more other classes. [Boo94]
A subclass inherits attributes and operations from its superclass Inheritance represents the is -a relationship: isn n
The subclass is a specialization of the superclass The superclass is a generalization of the subclass
Inheritance is a key concept of object-orientation objectClass modeling seeks to identify inheritance relationships between classes
Inheritance Example
Multiple classes with common attributes:
n n
surName givenName
A class that cannot be directly instantiated. [BRJ99] An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour. [Boo94] A class that can be directly instantiated. [BRJ99] A class whose implementation is complete and thus may have instances. [Boo94]
No objects of this class can be created Possible to create objects of these classes
Objects
Abstract class
eMacao- 14- 03eMacao- 14- 03- 66
Polymorphism
Polymorphism is the ability to hide many implementations behind the same interface Interface is the outside view of a class, consisting of its public attributes and operations. Example:
n n
All employees can apply for leave The process of leave application is implemented differently for different kinds of employees (e.g. agency heads vs. ordinary employees)
Interfaces
Definition:
n
An interface is a collection of operations that are used to specify a service of a class or a component. [B RJ99] component. RJ99
An interface is a contract of related services and a set of conditions that must be true for the contract to be faithfully executed Interfaces formalize polymorphism, they allow us to define polymorphism in a declarative way unrelated to implementation
Examples
Example 1: Objects
Geometric shapes:
n
Many different shapes, but some commonalities (area, circumference, width, height, centre, etc.)
Example 1: Classes
Define different classes for different kinds of objects
Triangle
Pentagon
Hexagon
Octagon
Ellipse
Parallelogram
Trapezoid
Rectangle
Square
Circle
Trapezoid
Example 1: Polymorphism
Different implementations of getArea() method:
n n n
Example 1: Discussion
Design maximizes use of inheritance & polymorphism Properties / operations are placed in classes as high up in the class hierarchy as possible Subclasses:
n n n
inherit properties / operations override properties / operations add new properties / operations
Common interface allows different classes to be used interchangeably Different implementations through polymorphism
Example 2: Objects
A citizen registers a complaint against a government agency Objects (highly simplified view):
n n n n
Citizen Lei Wai Keong Complaint Agency DSSD Complaint Handler Chan Tai Man
Complaint Complaint
Agency DSSD Citizen Lei Wai Keong Complaint Handler Chan Tai Man
Example 2: Classes
Provide operation postComplaint() to citizens postComplaint() Queues complaints with the responsible ComplaintHandler Processes complaints May forward complaints to other agencies Allows Person to post complaint (citizen, visitor, employee, etc.)
Example 2: Objects
Note: Diagram shows the case when the complaint needs to be forwarded to another agency
Example 2: Discussion
Models are generic
n n
If necessary, classes can modify their implementations (within the scope of specified interfaces) without affecting other classes
Conclusions
Virtues of Object-Orientation
Encapsulation & data abstraction
n
Easier to modularize code, for better development and debugging Functionality is localized where it is used
Inheritance
n n
Summary
OO methods regard the world as composed of interacting entities
n n
Identify abstractions of entities classes Identify relationships between entities association, inheritance, etc. Hide details of implementation from clients, allow different implementations of interfaces polymorphism
OO methods have the potential to result in higher quality, more modular & reusable software
References
[Boo94] Grady Booch, Object-Oriented Analysis and Design With Booch, ObjectApplications, Applications , Benjamin Cummings, 1994 [BRJ99] Grady Booch, James Rumbaugh, Ivar Jacobson, The Unified Booch, Rumbaugh, Modeling Language User Guide, Addison Wesley, 1999 Guide, [Mey97] Bertrand Meyer, Object-Oriented Software Construction, 2nd ed., ObjectConstruction, Prentice Hall, 1997 [MK04] Jim McKim, Object Oriented Programming and Design: Object McKim, Oriented Concepts, 2004 Concepts, [Pap01] Petraq Papajorgji, Introduction to Object Oriented Design and Papajorgji, Unified Modeling Language (UML), 2001 (UML), [RBPEL91] James Rumbaugh, Michael Blaha, William Premerlani, Rumbaugh, Blaha, Premerlani, Frederick Eddy, William Lorensen, Object-Oriented Modeling and Lorensen, ObjectDesign, Design, Prentice Hall, 1991 [Sch00] Douglas C. Schmidt , Object-Oriented Design and Programming, Schmidt, ObjectProgramming, 2000