article - get started with community ... - software engineering...

37
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU

Upload: others

Post on 20-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Software Engineering IObject-Oriented Design

Software Design RefinementUsing Design Patterns

Instructor: Dr. Hany H. AmmarDept. of Computer Science andElectrical Engineering, WVU

Page 2: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Outlinen The Requirements, Analysis, Design, and Design Refinement Modelsn Design refinementn Class diagram refinement using design patternsn Design patterns examples

– The Facade pattern– The Strategy Pattern– The State Pattern– The Command Pattern– The Observer Pattern– The Proxy Pattern

n Design Patterns Tutorials

Page 3: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The Requirements, Analysis, Design, andDesgin Refiement Models

Static AnalysisDynamic Analysis

Functional/NonfunctionalRequirements

Use Case Diagrams/Sequence Diagrams(the system level)

- Analysis Class Diagrams- State Diagrams/Refined SequenceDiagrams (The objectlevel)

RequirementsElicitationProcess

The AnalysisProcess

Static ArchitecturalDesign

Dynamic Design

The DesignProcess:• Initial Design•DesignRefinement

• Design Class Diagrams• Design Sequence Diagrams

Design Refinement • Refined Design ClassDiagrams

Page 4: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Design Refinement

n It is difficult to obtain a quality design from theinitial design

n The initial design is refined to enhance designquality using the software design criteria ofmodularity, information hiding, complexity,testability, and reusability.

n New components (or new classes) are defined andexisting components (or classes) structures arerefined to enhance design quality

n The design refinement step is an essential stepbefore implementation and testing.

Page 5: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Class Diagram RefinementUsing Design Patternsn Design Class Diagrams are further refined to enhance

design quality (i.e., reduce coupling, increasecohesion, and reduce component complexity) usingdesign patterns

n A design pattern is a documented good designsolution of a design problem

n Repositories of design patterns were developed formany application domains (communication software,agent-based systems, web applications)

n Many generic design patterns were defined and canbe used to enhance the design of systems in differentapplication domains

Page 6: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

What is a Design Pattern

n What is a Design Pattern?A design pattern describes a design problem

which repeatedly occurred in previous designs,and then describes the core of the solution to thatproblem

n Solutions are expressed in terms of classes ofobjects and interfaces (object-oriented designpatterns)

n A design pattern names, abstracts, and identifiesthe key aspects of a high quality design structurethat make it useful for creating reusable object-oriented designs

Page 7: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Defining a Design Patternn Design Patterns are documented in the literature by a

template consisting of the followingA Design Pattern has 5 basic parts:1. Name2. Problem3. Solution4. Consequences and trade-of of application5. Implementation: An architecture using a design class

diagram

Page 8: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Example of Pattern Definition: The Façade Pattern ProvidesAn Interface To a Subsystem

Page 9: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The Facade Pattern

n The class Facade is introduced as aninterface to the whole subsystem.

n Any client class needs a service from any ofthe subsystem classes will be send therequest to the facade class.

n All the subsystem interfaces are combinedin one class

Page 10: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Example of Using the Design PatternDesign Patterns produce quality designs by reducing couplingExample of how a Façade Pattern reduces coupling

Page 11: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Another Example: The Strategy Pattern template

Page 12: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Another Example of Design Patterns

n The Strategy Pattern: lets the algorithm varyindependently from clients that use it

Abstract ClassController Class

Control Strategy A Control Strategy B Control Strategy C

Default controlStrategy

Page 13: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The Strategy Pattern

n The Strategy Pattern Context class has multiplecontrol strategies provided by the concretestrategy classes, or by the abstract strategy (bydefault)

n The pattern lets us vary the algorithm thatimplements a certain function during run timedepending on the conditions of the system

n The Pattern reduces coupling by having the clientclass be coupled only to the context class

Page 14: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe Strategy Pattern

n Example of using the pattern in JAVAAWT GUI components lay out managers

Page 15: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe Strategy Pattern: another example

n Situation: A GUI text component objectwants to decide at runtime what strategy itshould use to validate user input. Manydifferent validation strategies are possible:numeric fields, alphanumeric fields,telephone-number fields, etc.

Page 16: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Exampleof using the StrategyPattern: SpeedControlIs the Strategy class

Page 17: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Another example of using theStrategy Pattern: A Job ApplicationSystem

n The complexity of class JobApplication is reduced by movingthe validate() operationto the Strategy Pattern classes

HostValidator

validate()

(from patterns)

ManagerValidator

validate()

(from patterns)

WaitStaffValidator

validate()

(from patterns)

BartenderValidator

validate()

(from patterns)

BusVal idator

validate()

(from patterns)

JobApplicantClient

main()runTest()

(from common)

ApplicantRuleFactory

getApplicantValidationRule()

(from patterns)JobApplicantForm

JOB_MANAGER : int = 1JOB_WAIT_STAFF : int = 2JOB_BUSSER : int = 3JOB_BARTENDER : int = 4JOB_HOSTER : int = 5position : intname : Stringphone : Stringemail : StringyearsExp : Doublereference1 : Stringreference2 : Stringreference3 : Stringlegal : boolean = false

isLegal()setLegal()getPosition()setPosi tion()getName()setName()getPhone()setPhone()getEmail()setEmail()getYearsExp()setYearsExp()getReference1()setReference1()getReference2()setReference2()getReference3()setReference3()validate()

(from patterns)

FormValidator

successMessage : String = "\nThank you for submitting your job application."

isEmpty()validate()basicVal idation()

(from patterns)

FormSuccess

success : boolean = falseresultMessage : String = null

FormSuccess()isSuccess()setSuccess()getResultMessage()setResul tMessage()

(from common)

Page 18: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe State Pattern

n Similar in structure (static) to the Strategypattern but differs in dynamics

n Events are handled based on the currentstate of the object

Page 19: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe State Patternn The State Pattern: is a solution to the problem of how to make

the behavior of an object depend on its state.

MultiStateOb

+CreateInitState()Setstate()

<<interface>>Statestate

ConcreteState 1 concreteState n

Handle(), Oprn()Handle()Opr1(),Opr2()

1..N

……………..N concrete stateclasses

Abstract StateClass

Lets a mutli state class divide itsresponsibilities (Opr1(),Opr2(),and Oprn() on multiple stateclasses.

TheContextClass

For more Info, seehttp://home.earthlink.net/~huston2/dp/state.html

Current StateHandle()Changestate()Deletestate()

Context class

Page 20: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe State Patternn The State pattern is a similar in structure to the Strategy Pattern but

with different behavior or dynamics. the state objects are active oneat a time depending on the actual state of the context object.

n The structure is defined as follows:– Define a "context" class to present a single interface to the

outside world.– Define a State abstract base class.– Represent the different "states" of the state machine as derived

classes of the State base class.– Define state-specific behavior in the appropriate State derived

classes.– Maintain a pointer to the current "state" in the "context" class.– To change the state of the state machine, change the current

"state" pointern State Transitions can be defined for each State classn To be discussed later at length in slides 10 on

Page 21: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design Patterns

n The context class Multistateob would create theinitial state object to provide the services of theinitial state (it will set its current state to its initialstate)

n The initial state object would sense the conditionfor state transition to a new state, when this occursit would then create an object of the new state anddestroy itself

n Each state object implements the transition,actions, and activities in the state it represents

Page 22: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe State Pattern

n TCP connection example

Page 23: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe State Pattern

A Ceiling Fan Pull Chain Example :

Page 24: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Design Patterns Application Example: TheVM Systemn The initial design class diagram of a Vending Machine

Page 25: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The State Diagram of the VMController Class

UserMode

MiantenanceMode

Received inputSignal from WirelessKeyboard

Reset

Interact with the User

Interact with theMaintenance Operator

Page 26: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Design Patterns Application Example: TheVM Systemn The State Pattern Applied to the 2 state VM example

VMContoller

++CreateInitState()Setstate()

<<interface>>VMController Statestate

UserMode MaintenanceMode

+UpdateDatabase()+InsertMoney()+SelectProduc()

2Class of Objects

Abstract StateClass

Concrete State 1 Class Concrete State 2 Class

Changestate()Deletestate()

Page 27: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Recall The Consolidated CollaborationDiagram of the ATM Client Subsystem

The diagram can be easily usedto develop the class diagramof the ATM Client Subsys.

Page 28: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Example: How can we applyThe State Pattern to theATM system usingThis ATM controllerStateChart ?

Page 29: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Types of Design PatternsThe Gang of Four (GoF) Patterns (Gamma et al1995)

Page 30: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Behavioral Design PatternsThe Command Pattern: operator commands or user orcustomer requests are treated as a class of objects

Page 31: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The Command Pattern

n Example of using the Command Pattern in a Menudriven graphics application

Page 32: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Behavioral Design PatternsThe Observer Pattern: Multiple observer objects arenotified when changes of states subjects occur

Page 33: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

The Observer Pattern

Page 34: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Structural Design PatternsThe Composite Pattern

Page 35: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Design PatternsThe Composite Pattern :File System Structure

Page 36: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Examples of Structural Design PatternsThe Proxy Pattern (used heavily incommunication software, CORBA, SOA )

Page 37: Article - Get Started with Community ... - Software Engineering Icommunity.wvu.edu/~hhammar/CU/swe/Slides/sw1 slides 11...Class Diagram Refinement Using Design Patterns nDesign Class

Design Patterns Examples and Tutorialsn Design Refinements Examplesn Two tutorials by John Vlissides

– An Introduction to Design PatternsAlso on the design patterns CD by Gama et al

– Designing with Patternsn Tutorial: More on Design patternsn The VISITOR family of design patternsn TEMPLATE METHOD & STRATEGY Patterns:

:Inheritance vs. Delegationn Pattern-Oriented Software Architectures

Patterns & Frameworks for Concurrent &Distributed Systems by D. C. Schmidt