35
1 Chapter 22 Chapter 22 Object-Oriented Object-Oriented Design Design

1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

  • View
    256

  • Download
    5

Embed Size (px)

Citation preview

Page 1: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

1

Chapter 22Chapter 22Object-Oriented Object-Oriented

DesignDesign

Page 2: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

2

Object-Oriented Object-Oriented DesignDesign

responsibilitiesdesign

messagedesign

class and objectdesign

subsystemdesign

Page 3: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

3

OOA and OOA and OODOOD

Object-relationship

model

Object-BehaviorModel

CRCIndex Cards

Attributes, operations,collaborators

THE ANALYSIS MODEL

responsibilitiesdesign

messagedesign

Class and objectdesign

subsystemdesign

THE DESIGN MODEL

Use cases

Page 4: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

4

OOA and OOA and OODOOD

classesclassesattributesattributesmethodsmethodsrelationshipsrelationshipsbehaviorbehavior

Analysis ModelAnalysis Model

objectsobjectsdata structuresdata structuresalgorithmsalgorithmsmessagingmessagingcontrolcontrol

Design ModelDesign Model

Page 5: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

5

Design Design IssuesIssues decomposability—the facility with which a design decomposability—the facility with which a design

method helps the designer to decompose a large method helps the designer to decompose a large problem into subproblems that are easier to solve;problem into subproblems that are easier to solve;

composability—the degree to which a design method composability—the degree to which a design method ensures that program components (modules), once ensures that program components (modules), once designed and built, can be reused to create other designed and built, can be reused to create other systems;systems;

understandability—the ease with which a program understandability—the ease with which a program component can be understood without reference to component can be understood without reference to other information or other modules;other information or other modules;

continuity—the ability to make small changes in a continuity—the ability to make small changes in a program and have these changes manifest themselves program and have these changes manifest themselves with corresponding changes in just one or a very few with corresponding changes in just one or a very few modules;modules;

protection—a architectural characteristic that will protection—a architectural characteristic that will reduce the propagation of side affects if an error does reduce the propagation of side affects if an error does occur in a given module.occur in a given module.

Page 6: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

6

Generic Components for Generic Components for OODOOD

Problem domain component—the subsystems that Problem domain component—the subsystems that are responsible for implementing customer are responsible for implementing customer requirements directly;requirements directly;

Human interaction component —the subsystems Human interaction component —the subsystems that implement the user interface (this included that implement the user interface (this included reusable GUI subsystems);reusable GUI subsystems);

Task Management Component—the subsystems Task Management Component—the subsystems that are responsible for controlling and that are responsible for controlling and coordinating concurrent tasks that may be coordinating concurrent tasks that may be packaged within a subsystem or among different packaged within a subsystem or among different subsystems;subsystems;

Data management component—the subsystem that Data management component—the subsystem that is responsible for the storage and retrieval of is responsible for the storage and retrieval of objects.objects.

Page 7: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

7

Process Flow for Process Flow for OODOOD

Page 8: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

8

System Design System Design ProcessProcess• • Partition the analysis model into subsystems.Partition the analysis model into subsystems.

• • Identify concurrency that is dictated by the problem.Identify concurrency that is dictated by the problem.

• • Allocate subsystems to processors and tasks.Allocate subsystems to processors and tasks.

• • Develop a design for the user interface.Develop a design for the user interface.

• • Choose a basic strategy for implementing data Choose a basic strategy for implementing data management.management.

• • Identify global resources and the control Identify global resources and the control mechanisms required to access them.mechanisms required to access them.

• • Design an appropriate control mechanism for the Design an appropriate control mechanism for the system, including task management.system, including task management.

• • Consider how boundary conditions should be Consider how boundary conditions should be handled.handled.

• • Review and consider trade-offs.Review and consider trade-offs.

Page 9: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

9

System System DesignDesign

requestclientsubsystem

contract

contract contract

request

request

serversubsystem

peersubsystem

peersubsystem

Page 10: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

10

Subsystem Subsystem ExampleExample

assign to zonetest status

request for alarm notificationperiodic check-inrequire for configuration update

request for statusControlpanel

subsystem

Sensorsubsystem

Centralcommunication

subsystem

request for system statusspecification of type of alarm

periodic status check

Page 11: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

11

Subsystem Design Subsystem Design CriteriaCriteria• The subsystem should have a well-defined

interface through which all communication with the rest of the system occurs.

• With the exception of a small number of “communication classes,” the classes within a subsystem should collaborate only with other classes within the subsystem.

• The number of subsystems should be kept small.

• A subsystem can be partitioned internally to help reduce complexity.

Page 12: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

12

Subsystem Collaboration Subsystem Collaboration TableTable

Page 13: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

13

Object Object DesignDesign A A protocol descriptionprotocol description establishes the establishes the

interface of an object by defining each interface of an object by defining each message that the object can receive and the message that the object can receive and the related operation that the object performs related operation that the object performs

An An implementation descriptionimplementation description shows shows implementation details for each operation implementation details for each operation implied by a message that is passed to an implied by a message that is passed to an object. object.

information about the object's private partinformation about the object's private part internal details about the data structures that describe internal details about the data structures that describe

the object’s attributesthe object’s attributes procedural details that describe operationsprocedural details that describe operations

Page 14: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

14

Design Design PatternsPatterns

... you’ll find recurring patterns of classes and communicating objects in many object-oriented systems. These patterns solve specific design problems and make object-oriented design more flexible, elegant, and ultimately reusable. They help designers reuse successful designs by basing new designs on prior experience. A designer who is familiar with such patterns can apply them immediately to design problems without having to rediscover them.

Gamma and his colleagues [GAM95]

Page 15: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

15

Design Pattern Design Pattern AttributesAttributes

The design pattern name is an abstraction that The design pattern name is an abstraction that conveys significant meaning about it applicability conveys significant meaning about it applicability and intent. and intent.

The problem description indicates the environment The problem description indicates the environment and conditions that must exist to make the design and conditions that must exist to make the design pattern applicable. pattern applicable.

The pattern characteristics indicate the attributes The pattern characteristics indicate the attributes of the design that may be adjusted to enable the of the design that may be adjusted to enable the pattern to accommodate into a variety of pattern to accommodate into a variety of problems. problems.

The consequences associated with the use of a The consequences associated with the use of a design pattern provide an indication of the design pattern provide an indication of the ramifications of design decisions.ramifications of design decisions.

Page 16: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

16

ExExamampleple Miter joint – Dovetail joint in windowMiter joint – Dovetail joint in window

Miter Miter Simpler lightweight inconspicuousSimpler lightweight inconspicuous

Dovetail Dovetail More complex, impervious temperature and humidityMore complex, impervious temperature and humidity Independent of fastening systemIndependent of fastening system Beautiful when made wellBeautiful when made well

Page 17: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

17

FacFacadade e

PatPatterternn

Name - FacadeName - Facade ProblemProblem

You need to use only a subset of a complex system. You need to use only a subset of a complex system. Or you need to interact with the system in a Or you need to interact with the system in a particular way.particular way.

SolutionSolution Define a new class (or classes) That has the required Define a new class (or classes) That has the required

interfaceinterface Have this new class use the existing system.Have this new class use the existing system.

Page 18: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

18

FaçFaçadade e

(co(cont.)nt.)

Trade-offsTrade-offs The facade simplifies the use of the required The facade simplifies the use of the required

subsystem. However, since the facade is not subsystem. However, since the facade is not complete, certain functionality may be unavailable to complete, certain functionality may be unavailable to the clientthe client

Page 19: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

19

FaFacacadedeSubsystem

Facade

Page 20: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

20

SuSummmamary – ry –

Applies whenApplies when You do not need to use all the functionality of a complex You do not need to use all the functionality of a complex

systemsystem You want to encapsulate or hide the original systemYou want to encapsulate or hide the original system You want to use the functionality of the original system You want to use the functionality of the original system

and want to add some new functionality as welland want to add some new functionality as well The cost of writing this new class is less than the cost of The cost of writing this new class is less than the cost of

everybody learning how to use the original system or is everybody learning how to use the original system or is less than you would spend on maintenance in the futureless than you would spend on maintenance in the future

Page 21: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

21

AdAdaptapterer Name – Adaptor (wrapper)Name – Adaptor (wrapper)

ProblemProblem A system has the right data and behavior but the A system has the right data and behavior but the

wrong interface. Often used when you have to make wrong interface. Often used when you have to make something a derivative of an abstract class we are something a derivative of an abstract class we are defining or already havedefining or already have

Page 22: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

22

SolSolutiutionon Provide a wrapper with the desired Provide a wrapper with the desired

interfaceinterface Object adapter – Object adapter –

Adapter contains the adaptee (pointer?)Adapter contains the adaptee (pointer?) Use inline functions to forwardUse inline functions to forward

Class adapterClass adapter Use multiple inheritance – privateUse multiple inheritance – private

Page 23: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

23

AdAdapaptortor

Client Target

Adaptor

Adaptee

Page 24: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

24

TraTrade de offsoffs Efficiency issuesEfficiency issues

Writing function forwardingWriting function forwarding NoteNote

Facade often hides several classes but may be 1Facade often hides several classes but may be 1 Adapter often hides 1 but may be severalAdapter often hides 1 but may be several

Page 25: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

25

FinFind d

whwhat at varvaries ies anand d

encencapsapsulaulate te itit

Abstract classAbstract class Core – defines Core – defines commonalitycommonality

Commonality Commonality Which abstract Which abstract classes to useclasses to use

VariationsVariations Derivations of Derivations of abstract classesabstract classes

SpecificationSpecification Interface of classesInterface of classes

Page 26: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

26

AbsAbstratract ct claclass ss

Core concept - commonalityCore concept - commonality Variations are derivation of an abstract Variations are derivation of an abstract

classclass

Page 27: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

27

SpSpecifecificaticationion, ,

IntInterferfaceace

, , ImImpleplemementantatiotionn

Specification – conceptual, identifies Specification – conceptual, identifies interfaceinterface

Specification implementation - given Specification implementation - given specification how can I implement this specification how can I implement this particular variationparticular variation

Page 28: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

28

BriBridgdge e

PatPatterternn

Name - bridgeName - bridge Problem the derivations of an abstract Problem the derivations of an abstract

class must use multiple implementations class must use multiple implementations without causing an explosion in the without causing an explosion in the number of classesnumber of classes

Page 29: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

29

SolSolutiutionon Define an interface for all implementations Define an interface for all implementations

to use and have the derivations of the to use and have the derivations of the abstract class use that.abstract class use that.

Page 30: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

30

BriBridgdge e

PaPattetternrn

Abstraction Implementor

Refined Abstraction Concrete

Implementor A

Concrete

Implementor B

Page 31: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

31

TraTradedeoffs offs The decoupling of the implementation The decoupling of the implementation

from the objects that use them increases from the objects that use them increases extensibility. extensibility.

Client objects are not aware of Client objects are not aware of implementation issues.implementation issues.

Variations of shape are encapsulated in Variations of shape are encapsulated in shape classshape class

Variations in drawing are encapsulated in Variations in drawing are encapsulated in drawing classdrawing class

Page 32: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

32

AbsAbstratract ct

FacFactortoryy

Name: Abstract FactoryName: Abstract Factory Problem: Families of related objects need to be Problem: Families of related objects need to be

instantiated.instantiated. Solution: The abstract factory defines the interface Solution: The abstract factory defines the interface

for how to create each member of the family. Each for how to create each member of the family. Each family is created by having its own unique concrete family is created by having its own unique concrete factory.factory.

Page 33: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

33

TraTradedeoffsoffs Isolates concrete classesIsolates concrete classes

Makes exchanging product families easyMakes exchanging product families easy Promotes consistency among productsPromotes consistency among products Hard to add new kinds of products Hard to add new kinds of products

Page 34: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

34

AbAbstrstract act FaFactoctoryry

Client

Abstract Product A

Abstract Product B

Abstract

Factory

A1 A2 B1 B2

Page 35: 1 Chapter 22 Object-Oriented Design. 2 Object-Oriented Design

35

SuSummmamaryry

First, identify the rules for instantiation and define First, identify the rules for instantiation and define an abstract class with an interface that has a an abstract class with an interface that has a method for each object that needs to be method for each object that needs to be instantiatedinstantiated

Implement concrete classes from this class for each Implement concrete classes from this class for each familyfamily

The client object uses this factory object to create The client object uses this factory object to create the server objects that it needsthe server objects that it needs