Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Thinking in Objects
Fundamentals of OO Approach
Thinking in Objects
Thinking in Objects
Module 1
Module 2
..
Module N
Program 1
Program 2
Program N
Function 1
Function 2
Function N
Thinking in Objects
User Module
Admin Module
Book Tickets
Upcoming releases
Search Movie
Programs
The above example shows a typical procedural approach where we have identified the Modules and Programs
Thinking in Objects
{
Programs
Add new review Accept movie information() Set release status of movie() Store Movie information()
{
Functions
Copyright 2009 Pratian Technologies www.pratian.com
Thinking in Objects
Thinking in Objects
includes
Thinking in Objects
Thinking in Objects
X
is comprised of
AcceptMovieInformation(); all the functions share data movieName rating releaseStatus starcast genre posterImage actorRole
AddNewMovie.h
StoreMovieInformation();
SetReleaseStatusOfMovie();
AssignRolesToActor();
includes
AddNewMovie.c
Thinking in Objects
Thinking in Objects
The OO Approach
APPLICATION
Module 1
Module 2
..
Module N
Use Case 1
Class 1
Class 2
Class N
Use Cases are not specific to OO approach. They are popularly used here. However Use Cases can be used in the Procedural approach too.
Thinking in Objects
The OO Approach
User Module: Use Cases
WebUser
Thinking in Objects
The OO Approach
Admin Module: Use Cases
Admin
Admin
Add new actor
Admin
Thinking in Objects
Procedural to OO
}
We now realize classes from each Use Case
Copyright 2009 Pratian Technologies www.pratian.com
Essentially classes are identified by using different techniques One of the simple techniques is NOUN-IDENTIFICATION
Thinking in Objects
The OO Model
The OO Model for the Use Case Add New Movie has the following classes
The OO Model shows the classes and the relationships between the classes
AddMovieForm
has
MovieMgr
has
Actor
*
Movie
has
Thinking in Objects
The OO approach
Representation of the Classes in Java
Thinking in Objects
Object-Oriented Programming
Object-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... Grady Booch
Thinking in Objects
Easier to find nouns and construct a system centered around the nouns than actions in isolation
Manager 1 *
Salesperson
Easier to visualize an encapsulated representation of data and responsibilities of entities present in the domain The modern methodologies recommend the object-oriented approach even for applications developed in C or Cobol
Thinking in Objects
Thinking in Objects
Identifying Classes
Develop an Object Model for the following requirement in an ILT (instructor-led training): A trainer trains many trainees on a given course. Every course has many modules each module is comprised of different units and each unit has many topics. Identify the different classes from the above problem statement
Thinking in Objects
Identifying Classes
Recommended Class Identification Technique
Focus on identifying NOUNS (Data Driven approach) We will look at other techniques subsequently
Thinking in Objects
Identifying Relationships
Trainer - Trainee Trainer HAS many Trainees Every Trainee HAS a Trainer
Trainer
1
has
*
Trainee
Course
has
*
Module
Thinking in Objects
Identifying Relationships
Module Unit Module HAS many Units
Module
has
*
Unit
Topic
Thinking in Objects
The OO Model
Trainer
1
has
*
Trainee
Course
has
*
Module
has
*
Unit
has
*
Topic
Thinking in Objects
has
Trainee Training A Trainee (HAS) attends many Trainings A Training HAS many Trainees
Trainee
*
Training
*
has
Training
*
Copyright 2009 Pratian Technologies www.pratian.com
Thinking in Objects
Conceptual Entity
Training - Course The Training (HAS) (is conducted for) a Course A Course HAS (can have) many Trainings
Training
*
has
1
Course
Thinking in Objects
Solution
Trainer
1 1
has
*
Trainee
*
has
has
Training
* * 1 * Easy to model real-world problems using the OO approach
Course
has
*
Module
has
*
Unit
Copyright 2009 Pratian Technologies www.pratian.com
has
*
Topic
Thinking in Objects
Acompanysellsdifferentitemstocustomerswhohave A company sells different items to customers who have placedorders.Anordercanbeplacedforseveralitems. However,acompanygivesspecialdiscountstoits However, a company gives special discounts to its registeredcustomers.
Identify the different classes from the above problem statement Identify the different connections (relationships) between the above classes
Thinking in Objects
Thinking in Objects
Thinking in Objects
Mary
David
employeeID : integer name : String department : String signIn() signOut() work() takeSalaray()
Classes are set of objects that share a common structure and behavior The structure of a class includes its Primary structure Reused structure The behavior of a class includes its Visible behavior Implementation behavior
Thinking in Objects
State
The set of values that an object holds
Behavior
How an object acts and reacts, in terms of its state changes and message passing The visible behavior of an object is modeled by the set of messages it can respond to
Identity
That property of an object which distinguishes it from all other objects
Thinking in Objects
Thinking in Objects
Abstraction
Simply put, public view of an object
Order
AddItems GetItems GetTotal GetStatus Cancel
Thinking in Objects
Abstraction
Defines the public contract (The Outside View)
Client1 Order
add Item s() getItems() getOrd erTotal() getStatu s() ca ncel ()
Client2
Client3
getTotal() cancel()
ExternalOrder
getTot al() cancel()
Thinking in Objects
Encapsulation
Hide Implementation Details
Encapsulation is
The grouping of related ideas into a single unit, which can thereafter be referred to by a single name. The process of compartmentalizing the elements of an abstraction that constitute its structure and behavior.
Employee
empId : String name : String address : Address getEmpID() : String setEmpId(empId : String) getName() : String setName(name : String) getAddress() : Address setAddress(address : Address)
Thinking in Objects
Encapsulation - Example
Implementation Order
orderNum AS INT custNum AS INT CalculatePrice( ) Public methods of Order
Outside View
Thinking in Objects
Hierarchies
Object Relationships
Define relationships between objects
Objects defined in terms of other objects Allows state and behavior to be shared and specialized as necessary Encourages code reuse
Employee
Company
Employee
Manager
Thinking in Objects
Inheritance
An object oriented system organizes classes into a subclass-super class hierarchy
Inheritance encourages code reuse
Generalization
Mary
Employee
Specialization
Manager
David
Salesperson
Joe
Thinking in Objects
Polymorphism
One function, many implementations
All employees do some work Mary
Employee work()
Manager
David David does a managers work
Salesperson work()
Joe
work()
Early binding
Function Call mapped compilation Function Overloading at
Late binding
Function Call mapped at run-time Function Overriding
Copyright 2009 Pratian Technologies www.pratian.com
Delegation
Delegation is the use of other within a class class
Class forwards method calls to the contained class
Thinking in Objects
Delegation Example
Order references a ShipInfo object
Order
PRIVATE: ShipInfo shipObj = NEW ShipInfo( ) PUBLIC: GetShipDate( ) shipObj:GetDate( )
ShipInfo
PRIVATE: id shipdate promisedate PUBLIC: SetDate( ) GetDate( )
calls
Thinking in Objects
Thinking in Objects
Thinking in Objects
Topics
Is-a, Has-a and Uses relationships Multiplicity, navigability & role name Case Study
Identifying objects real time Analyze object relationships Handle the complexity of these relationships The tricks of designing a complex system with several collaborating objects
Thinking in Objects
Thinking in Objects
Relationships
Classification
<<Is-a>> <<Has-a>> <<Uses>>
Generalization Realization
Dependency
For all practical purposes we will represent Is-a relationship as Has-a relationship as Uses relationship as
Thinking in Objects
Navigability
Can be unidirectional or bidirectional Navigability is by default bi-directional
Role name
The name of the instance in the relationship Multiple has-a based on different roles are possible
Thinking in Objects
Exercise 1
See Exercise1.doc Identify the classes Identify the relationships Please focus on relevant OO principles Convey your ideas using simple has-a, is-a or uses notations Specify multiplicity, navigability and role name (where necessary) Focus on classes more than the attributes and methods
Thinking in Objects
Solution
Consider the following classes
Company Branch Address
Company
2..*
Company has two or more instances of the Branch Instances of Branch called corporate office and registered office are mandatory
Branch
Address
Thinking in Objects
-others
1
0..*
Address
Thinking in Objects
Customer
name age c ustId dtMem fees discount t ype dtReg
Not all instances of Customer will need the complete information. Different instances have different cohesion Mixed Instance Cohesion, which is a problem
Copyright 2009 Pratian Technologies www.pratian.com
Thinking in Objects
Specialization
Use Specialization to resolve the problem of Mixed Instance Cohesion Customer
name age custId dtMem
RegCustomer
dt Reg t ype fees dis count renewDate t imesRenewed
Thinking in Objects
35 35 35 35 35 35 35 35
A A A B B B C C
5% .... 5% .... 5% .... 10% .... 10% .... 10% .... 15% .... 15% ....
RegCustomer
dtReg t ype fees dis count renewDate t imesRenewed
There is redundancy in the information contained in the RegCustomer class, which is replicated unnecessarily across all its instances belonging to a given type
Copyright 2009 Pratian Technologies www.pratian.com
Thinking in Objects
Category
type fees discount
Move the redundant information out of the RegCustomer class into the Category class However, RegCustomer needs to still reuse the information. How do you reuse this information is-a or has-a ?
Thinking in Objects
Controlled multiplicity
Use has-a there is multiplicity whenever controlled
Customer
custId dtMem
There are many instances of RegCustomer There are a few instances of Category But each instance of RegCustomer does not have its own unique copy of category information (controlled multiplicity)
Controlled Multiplicity
RegCustomer
dtReg renewDate timesRenewed
Category
t ype fees discount
Thinking in Objects
Class Hierarchy
Consider the Employee classes. A hierarchy is created by the Salesperson and BranchMgr classes. Employee
Branch
empId basic band exp getSalary()
BranchMgr
Salesperson
Thinking in Objects
Problem of reuse
Both the Employee class and Customer class have some common information like Employee Customer Name
Age Address
empId basic band exp getS alary()
custId dtMem
BranchMgr
Salesperson
RegCustomer
dtReg renewDate timesRenewed
It is hence important to decouple the common information from these two classes into another class and reuse the same How do you promote the reuse (is-a or has-a) ?
Thinking in Objects
Employee
empId basic band exp getSalary()
Customer
custId dtMem
Thinking in Objects
Employee
empId basic band exp getSalary()
Customer
custId dtMem
The Employee and Customer classes do not have any common abstractions (i.e. behavior), but only common structure They are known as peer classes Whenever two classes have only common structure (information) and not behavior use has-a relationship
Copyright 2009 Pratian Technologies www.pratian.com
Thinking in Objects
Changing rules
Consider the implementation of getSalary() in the Employee class What if the rules change frequently?
Do not associate rules with an entity class Decouple the rules into a conceptual entity known as the SalCalc
Employee
empId basic band exp getSalary()
SalCalc
fi ndSal() fi ndIncen() fi ndAllow()
Thinking in Objects
SalCalc
findSal() findIncen() findAllow()
This delegation need not be done by using has-a as the SalCalc does not contain any information, but only methods
Consider making the methods static Avoid creating the instance of SalCalc for each call made to getSalary()
Thinking in Objects
Employee
* empId basic band exp getSalary() *
Customer
custId dtMem
Company has many Customers A Customer is associated with many Companies Company has many Employees
Thinking in Objects
Solution
Explicit role-naming Company
*
Address
PersonalInfo
name age
Peer classes cannot have common abstractions Mixed Instance Cohesion (MIC)
Employee
-others
1
0..*
Customer
custId dtMem
SalCalc BranchMgr
fi ndSal () fi ndIncen() fi ndAl low()
Category
type fees discount
Thinking in Objects
Conclusion
Thinking in objects
Owning a hammer doesnt make one an architect Knowing an object-oriented language is a necessary but insufficient first step to create object systems. Knowing how to think in objects is critical
Thinking in Objects
Question Time
Thinking in Objects