OO DesignOO Design
Roshan ChitrakarRoshan Chitrakar
Analysis to DesignAnalysis to Design
Do the RIGHT thingDo the RIGHT thing Requirement AnalysisRequirement Analysis Domain Modeling with Domain Modeling with
addition of attributes addition of attributes and associationsand associations
Use Case Modeling Use Case Modeling (Behavioral modeling)(Behavioral modeling)
Requisite Pro and Use Requisite Pro and Use case diagramscase diagrams
Do the thing RIGHTDo the thing RIGHT Responsibility Responsibility
assignmentassignment Object DesignObject Design Design Class diagrams Design Class diagrams Determining visibilityDetermining visibility Interaction diagrams Interaction diagrams
(Sequence diagrams & (Sequence diagrams & Collaboration Collaboration diagrams, Class diagrams, Class diagram)diagram)
OO Design: IntroductionOO Design: Introduction
A logical solution based on OO A logical solution based on OO paradigm is developedparadigm is developed
Constitutes a design modelConstitutes a design model Creation of Interaction diagrams Creation of Interaction diagrams
(collaboration among objects and/or (collaboration among objects and/or sequence of messages)sequence of messages)
Class diagrams are drawn in parallel Class diagrams are drawn in parallel with or after the interaction diagramswith or after the interaction diagrams
GRASPGRASP
Fundamental principles of Object Fundamental principles of Object design are: -design are: - Responsibility assignment and Responsibility assignment and Design patternsDesign patterns
GRASP (General Responsibilities GRASP (General Responsibilities Assignment Software Patterns) Assignment Software Patterns) describe the principles of object describe the principles of object design and responsibility design and responsibility assignment, expressed as patterns.assignment, expressed as patterns.
ResponsibilityResponsibility
It is a contract or obligation of a classifierIt is a contract or obligation of a classifier It is assigned to classes during object designIt is assigned to classes during object design Type of ResponsibilitiesType of Responsibilities
Knowing Knowing (e.g. “a Sale is responsible for knowing its total”)(e.g. “a Sale is responsible for knowing its total”) aboutabout private encapsulated data private encapsulated data aboutabout related objects related objects aboutabout things it can derive or calculate things it can derive or calculate
Doing Doing (e.g. “a (e.g. “a SaleSale is responsible for creating is responsible for creating SalesLineItemsSalesLineItems”)”) something itselfsomething itself initiating action in other objectsinitiating action in other objects controlling and coordinating activities in other objectscontrolling and coordinating activities in other objects
MethodsMethods
Responsibilities are translated into Responsibilities are translated into methods methods
Depending upon the granularity, Depending upon the granularity, several methods are implemented to several methods are implemented to fulfill the responsibilitiesfulfill the responsibilities
Methods either act alone or Methods either act alone or collaborativelycollaboratively
Both responsibilities and methods are Both responsibilities and methods are reflected in interaction diagramsreflected in interaction diagrams
Responsibility & Methods Responsibility & Methods in Interaction Diagramin Interaction Diagram
makePayment(cashTendered)create(cashTendered)
: Sale
: Payment
Responsibility: To create Payments
PatternsPatterns A pattern is a A pattern is a namednamed problem/solution problem/solution
pair that can be applied in new context, pair that can be applied in new context, with advice on how to apply it in novel with advice on how to apply it in novel situation and discussion of its trade-offs.situation and discussion of its trade-offs.e.g.e.g.Pattern NamePattern Name: Information Expert: Information ExpertSolutionSolution: Assign a responsibility to the : Assign a responsibility to the class that has the information needed to class that has the information needed to fulfill it.fulfill it.Problem It SolvesProblem It Solves: What is a basic : What is a basic principle by which to assign principle by which to assign responsibilities to objects.responsibilities to objects.
The First Five GRAS The First Five GRAS PatternsPatterns
They are: -They are: -1.1. Information Expert (or Expert)Information Expert (or Expert)
2.2. CreatorCreator
3.3. High CohesionHigh Cohesion
4.4. Low CouplingLow Coupling
5.5. ControllerController They cover very basic, common and They cover very basic, common and
fundamental design issuesfundamental design issues
Information ExpertInformation Expert It guides that objects do things related to It guides that objects do things related to
the information they have.the information they have. It is a class that has information necessary It is a class that has information necessary
to fulfill the responsibilityto fulfill the responsibility There may have partial information experts There may have partial information experts
who will collaborate for the responsibilitywho will collaborate for the responsibility
Design ClassDesign Class ResponsibilityResponsibility
SaleSale Knows sale totalKnows sale total
SalesLineItemSalesLineItem Knows line item subtotalKnows line item subtotal
ProductSpecificationProductSpecification Knows product priceKnows product price
Expert IllustrationExpert Illustration:: To find out the To find out the grand totalgrand total
Domain model showing association of Sale
Expert IllustrationExpert Illustration:: To find out the To find out the grand totalgrand total
•Knowing Line Item sub total and knowing product price in order to get the grand total.
•Responsibilities are being added to both interaction and Class diagrams also.
•More than one class can act as information expert.
•They collaborate by passing messages.
Expert: Pro and ConsExpert: Pro and Cons
Advantages: -Advantages: - Encapsulation is maintainedEncapsulation is maintained Behavior is distributed across the Behavior is distributed across the
classesclasses Disadvantages: -Disadvantages: -
Leads to problems of coupling, cohesion Leads to problems of coupling, cohesion and duplicationand duplication
CreatorCreator
It guides assigning responsibilities It guides assigning responsibilities related to the creation of objectsrelated to the creation of objects
The concept of aggregation is the The concept of aggregation is the basic guiding principle of the basic guiding principle of the Creator.Creator.
It suggests that the enclosing It suggests that the enclosing container or recorder class is a good container or recorder class is a good candidate for the responsibilitycandidate for the responsibility
Creator IllustrationCreator Illustration:: Creating a Sales Creating a Sales Line ItemLine Item
Sale Domain
Creator IllustrationCreator Illustration:: Creating a Sales Creating a Sales Line ItemLine Item
Creator: Pro and ConsCreator: Pro and Cons
Benefits: -Benefits: - Low coupling is supported i.e lower Low coupling is supported i.e lower
maintenance dependence and higher maintenance dependence and higher possibility of reuse.possibility of reuse.
Contraindications: -Contraindications: - In special cases, all creations are In special cases, all creations are
delegated to a helper class called delegated to a helper class called Factory. (Which is against the principle Factory. (Which is against the principle of Creator)of Creator)
Low CouplingLow Coupling
It suggests assigning a responsibilities It suggests assigning a responsibilities to classes such that their placement to classes such that their placement does not produce negative results.does not produce negative results.
It recommends designs of classes to be It recommends designs of classes to be more independent and hence reduce more independent and hence reduce the impacts of change.the impacts of change.
No absolute measure of low/high No absolute measure of low/high coupling, designer has to assess it so coupling, designer has to assess it so that negative results would not occur.that negative results would not occur.
Low Coupling IllustrationLow Coupling Illustration:: Creating a Creating a PaymentPayment
Payment Register Sale
Creator pattern suggests Register as a candidate for creating the Payment.
Coupling of Register class to the knowledge of
Payment class
Low Coupling IllustrationLow Coupling Illustration:: Creating a Creating a PaymentPayment
An alternate solution: Coupling of Sale class with the Payment class
Sale creates the Payment; does not
increase the coupling
High CohesionHigh Cohesion ““High functional cohesion exists when High functional cohesion exists when
the elements of a component (e.g. a the elements of a component (e.g. a class) all work together to provide some class) all work together to provide some well-bounded behavior”well-bounded behavior”
- Booch- Booch High Cohesion has fewer methods, with High Cohesion has fewer methods, with
highly related functionality, collaborates highly related functionality, collaborates with other objects to share the effort.with other objects to share the effort.
High Cohesion pattern has real world High Cohesion pattern has real world analogy or delegating the too much analogy or delegating the too much work.work.
High Cohesion IllustrationHigh Cohesion Illustration:: Creating a Creating a PaymentPayment
Register creates the Payment by taking the work by itself: Low cohesion
High Cohesion IllustrationHigh Cohesion Illustration:: Creating a Creating a PaymentPayment
Register delegates the work to Sale : Low cohesion
High Cohesion: Pro and High Cohesion: Pro and ConsCons
Advantages: -Advantages: - Maintenance and enhancement is Maintenance and enhancement is
simplifiedsimplified Low coupling is often supportedLow coupling is often supported Reuse is increased.Reuse is increased.
Disadvantages: -Disadvantages: - Distributed servers take more overhead Distributed servers take more overhead
and pay reduced performanceand pay reduced performance For only one person to maintain and For only one person to maintain and
support the component, it is difficult.support the component, it is difficult.
ControllerController
It provides guidance for handlers for It provides guidance for handlers for external inputs / events messages or external inputs / events messages or signals that system receives.signals that system receives.
It delegates, coordinates and controls It delegates, coordinates and controls the work to other objects: does not the work to other objects: does not do the actual work.do the actual work.
The System class made during the The System class made during the analysis phase, is now taken care of analysis phase, is now taken care of by Controller class.by Controller class.
System Class to System Class to Controller ClassController Class
Register acts as the single Controller
Class
Two handlers act like
Controllers
Controller IllustrationController Illustration:: enterItem() enterItem()
Controller IllustrationController Illustration:: enterItem() enterItem()
Possible handlers are: -Possible handlers are: -
: Registe
r
enterItem(id,quantity)
: ProcessSaleHa
ndler
enterItem(id,quantity)
Controller: Pro and ConsController: Pro and Cons
Increased potential for reuseIncreased potential for reuse Pluggable interface (i.e. application Pluggable interface (i.e. application
logic is not handled in the interface logic is not handled in the interface layer)layer)
It guarantees that system operations It guarantees that system operations within a use case occur in a legal within a use case occur in a legal sequence (It is possible if the same sequence (It is possible if the same controller is used throughout the use controller is used throughout the use case)case)