Introduction to Design Patterns - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Introduction to Design Patterns

Description:

Adapted from Software Design: From Programming to ... Design Purpose ... Our design should be flexible enough to produce any of several kitchen styles. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 38
Provided by: ericb170
Learn more at: http://www-scf.usc.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Design Patterns


1
Introduction to Design Patterns
2
Sample Design Goals and Ways to Accomplish Them
  • Reusability, Flexibility, and Maintainability
  • Reuse flexible designs
  • Keep code at a general level
  • Minimize dependency on other classes
  • Three Types Creational, Structural, or
    Behavioral.
  • Robustness
  • Reuse reliable designs
  • Reuse robust parts
  • Sufficiency / Correctness
  • Modularize design
  • Reuse trusted parts

Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
3
Design Purpose
Factory
Create individual objects in situations where the
constructor alone is inadequate.
Design Pattern Summary
Use methods to return required objects.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
4
Factory Class Model
RequiredClass
Client
create object
Factory design pattern
MyClass createObjectOfRequiredClass()
RequiredClass
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
5
Sequence Diagram for Factory
MyClass
Client
RequiredClass
createObjectOfRequiredClass()
RequiredClass()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
6
Typical Output of E-Mail Generation Example
public static void main( String args )
Customer customer getCustomerTypeFromUser()
//factory call client.sendMessage(customer.getMe
ssage() ) //polymorphism
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
7
The Actual Factory Code
private static Customer getCustomerTypeFromUser()
customerType readFromCustomer() Customer
customerSelected select customerType
"frequent" new Frequent() "returning"
new Returning() "curious" new Curious()
"newbie" new Newbie()
otherwise new Newbie() return
customerSelected
8
Design Goals At Work ? Correctness and
Reusability ?
We want to separate the code common to all types
of customers. We want to separate the
specialized code that generates e-mail for each
type of customer. This makes it easier to check
for correctness and to reuse parts.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
9
Factory Email Generation Example
Application of Factory design pattern
Customer getMessage()
Frequent getMessage()
Returning getMessage()
Curious getMessage()
Newbie getMessage()
Client sendMessage()
setup
MailMessage text
MailGenerationApplication getCustomerTypeFromUser(
)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
10
Design Purpose
Abstract Factory
Provide an interface for creating families of
related or dependent objects without specifying
their concrete classes.
Design Pattern
Capture family creation in a class containing a
factory method for each class in the family.
Gamma et al
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
11
KitchenViewer Interface
Wall cabinet
menu
?
Counter
display area
styles
Floor cabinet
Modern
Classic
Antique
Arts Crafts
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
12
KitchenViewer Example
Wall cabinets
Floor cabinets
Countertop











Modern
Classic
Antique
Arts Crafts
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
13
Selecting Antique Style











Modern
Classic
Antique
Arts Crafts
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
14
KitchenViewer Without Abstract Factory Pattern
Client renderKitchen()
Kitchen
FloorCabinet
WallCabinet
ModernWallCabinet
AntiqueWallCabinet
ModernFloorCabinet
AntiqueFloorCabinet
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
15
Code for renderKitchen()
  • string getKitchenStyleTypeFromUser()
  • if (string "antique")
  • AntiqueWallCabinet awc1 new
    AntiqueWallCabinet() //call constructor directly
  • AntiqueWallCabinet awc2 new
    AntiqueWallCabinet()
  • AntiqueFloorCabinet afc1 new
    AntiqueFloorCabinet ()//call constructor
    directly
  • AntiqueFloorCabinet afc2 new
    AntiqueFloorCabinet ()
  • Kitchen newAntiqueKitchen new Kitchen()
  • newAntiqueKitchen.add(awc1) //aggregates use
    "add"
  • newAntiqueKitchen.add(awc2)
  • newAntiqueKitchen.add(afc1)
  • newAntiqueKitchen.add(afc2)
  • else if (string modern")
  • else //for other types
  • //Now draw it.
  • .

16
Design Goal At Work ? Flexibility ?
Our design should be flexible enough to produce
any of several kitchen styles.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
17
The Abstract Factory Idea
KitchenStyle getWallCabinet() getFloorCabinet()
WallCabinet
FloorCabinet
AntiqueWallCabinet
AntiqueFloorCabinet


ModernKStyle getWallCabinet() getFloorCabinet()
AntiqueKStyle getWallCabinet() getFloorCabinet()
FloorCabinet getFloorCabinet() return new
AntiqueFloorCabinet()
FloorCabinet getFloorCabinet() return new
ModernFloorCabinet()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
18
Abstract Factory Design Pattern Applied to
KitchenViewer
Client renderKitchen( KitchenStyle )
KitchenStyle getWallCabinet() getFloorCabinet()
Kitchen getWallCabinet() getFloorcabinet()
WallCabinet
FloorCabinet
ModernWallCabinet
ModernKStyle getWallCabinet() getFloorCabinet()
AntiqueWallCabinet
ModernFloorCabinet
AntiqueKStyle getWallCabinet() getFloorCabinet()
AntiqueFloorCabinet
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
19
Code for the AntiqueKStyle Factory
  • class AntiqueKStyle
  • getWallCabinet()
  • AntiqueWallCabinet awc new AntiqueWallCabinet
    ()
  • //do whatever you need to do to it.
  • return awc
  • getFloorCabinet()
  • AntiqueFloorCabinet afc new AntiqueWallCabinet
    ()
  • //do whatever you need to do to it.
  • return afc
  • Note No one should be calling the specific
    constructors other than these factory "get"
    methods.

20
What the Application Looks Like
  • public void KitchenGenerationApplication()
  • KitchenStyle myStyle getKitchenStyleTypeFromUse
    r()
  • renderKitchen( myStyle ) // use the correct
    factory via // natural polymorphism

21
How to Get the myStyle Value
  • private static KitchenStyle getKitchenStyleTypeFro
    mUser()
  • Hashtable kitchenStyleTypeTable new
    Hashtable()
  • // Key user input to the corresponding type of
    KitchenStyle
  • kitchenStyleTypeTable.put( "modern", new
    ModernKStyle() )
  • kitchenStyleTypeTable.put( "antique", new
    AntiqueKStyle() )
  • kitchenStyleTypeTable.put( "classic", new
    ClassicKStyle() )
  • kitchenStyleTypeTable.put( "arts crafts", new
    ArtsAndCraftsKStyle() )
  • // Get KitchenStyle type from user
  • String kitchenStyleType getKitchenTypeFromUser(
    )
  • return (KitchenStyle)kitchenStyleTypeTable.get(ki
    tchenStyleType)

22
Factory Code for renderKitchen()
  • renderKitchen(KitchenStyle myStyle)
  • WallCabinet wc1 myStyle.getWallCabinet()
    //ask myStyle factory for wall cab.
  • WallCabinet wc2 myStyle.getWallCabinet()
  • FloorCabinet fc1 myStyle.getFloorCabinet()
    //ask myStyle factory for floor cab.
  • FloorCabinet fc2 myStyle.getFloorCabinet()
  • Kitchen newKitchen new Kitchen() // not
    "newAntiqueKitchen"
  • newKitchen.add(wc1) //aggregates use "add"
  • newKitchen.add(wc2)
  • newKitchen.add(fc1)
  • newKitchen.add(fc2)
  • //Now draw it.
  • .

23
Abstract Factory Design Pattern
Client doOperationOnCollection( Style myStyle )
Style getComponentA() getComponentB()
Collection
ComponentA
ComponentB
Style1ComponentA
Style1 getComponentA() getComponentB()
Style2ComponentA
Style1ComponentB
Style2 getComponentA() getComponentB()
Style2ComponentB
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
24
Think What To Do If Adding a New Style
  • Add the new component classes to the existing
    hierarchy,
  • e.g. FancyWallCabinet and FancyFloorCabinet
  • These methods (obviously) have constructors
  • Add the style to the existing hierarchy
  • e.g. Add FancyKStyle
  • Add factory get methods to the new style
  • e.g. getWallCabinet() and getFloorCabinet()
  • These methods call the right constructors.

25
Seems Complicated?
  • Spend the time to learn the pattern and implement
    it correctly versus
  • Implementing it haphazardly and a lifetime of
    maintenance.
  • Anyone who knows about abstract factories know
    how your code is structured.
  • The pattern becomes a habit for you, another
    member of your professional toolkit.

26
Structural Design Pattern
Facade
Provide an interface to a package of classes
Design Pattern Summary
Define a singleton which is the sole means for
obtaining functionality from the package.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
27
Facade Design Pattern Structure
1
Façade exposed cMethodOfFacade()
Client
2
C not exposed myCMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
28
Sequence Diagram for Façade
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
29
Using Façade to Access Bank Customers
Customer getCustomerName() getNumAccounts() getPer
sonalNote() getAccount( int )
Account getAccountNum() deposit( int
) getBalance()
AccountException
CustomerException
framework
1..n
BankCustomer
BankAccount
IntroMessage
facade BankCustomers doD
eposit( int amt, Customer cust, Account acc
) getBankAccount( Customer cust, int accNum
) getBankCustomer( String custName
) introduceApplication()
Client main()
bankCustomers
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
30
Key Concept ? Behavioral Design Patterns ?
-- to capture behavior among objects.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
31
Behavioral Design Pattern
Mediator
Avoid references between dependent objects.
Design Pattern Summary
Capture mutual behavior in a separate class.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
32
Example of Behavioral Design Goal Port Traffic
obstacles
to drydock ?
berth
berth
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
33
What's the Problem?
  • Ships and Tugboats play different roles.
  • Ships need tugboats to move them around port.
  • Different ships can need different tugboats
    depending on the mission.
  • Try to define the relationship between tugboat
    and ship so that
  • Each is independent of one another.
  • The definition of one doesn't affect the other.
  • Maybe the mission can be the linchpin (mediator).

34
Avoiding Dependencies
?
Harbor application
1..n
1
Ship
Tugboat
class Ship //code for this dependency Tugboat
tugs //tugboats I can use ...
"Customs" application reuse Ship alone without
any Tugboat relationship
Ship
Longshoreman
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
35
Core Mediator Concept Applied to The Harbor
Problem
Ship
LeavingPort estimateTime()
Tugboat
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
36
Applying the Mediator Design Pattern to The
Harbor Problem
PortMission estimateTime()
Vessel
Mediator base class
Ship
Tugboat
EnteringPort estimateTime()
LeavingPort estimateTime()
BeingMaintained estimateTime()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
37
Summary of This Chapter
  • Design Patterns are recurring designs satisfying
    recurring design purposes.
  • Classified as Creational, Structural, or
    Behavioral.
  • Can simplify design, but requires the overhead of
    KNOWING about them.

Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
Write a Comment
User Comments (0)
About PowerShow.com