Design Patterns II - PowerPoint PPT Presentation

1 / 68
About This Presentation
Title:

Design Patterns II

Description:

Ensure a class only has one instance, and provide a global point of access to it ... Convert the interface of a class into another interface clients expect ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 69
Provided by: shyhka
Category:
Tags: class | design | patterns

less

Transcript and Presenter's Notes

Title: Design Patterns II


1
Design Patterns (II)
  • Shyh-Kang Jeng
  • Department of Electrical Engineering
  • National Taiwan University

2
Creating Mazes
3
Creating Mazes
  • Maze MazeGameCreateMaze( )
  • Maze aMaze new Maze
  • Room r1 new Room(1)
  • Room r2 new Room(2)
  • Door theDoor new Door(
  • r1, r2 )
  • aMaze-gtAddRoom( r1 )
  • aMaze-gtAddRoom( r2 )
  • Set sides for each room
  • return aMaze

4
Builder Pattern
  • Separate the construction of a complex object
    from its representation so that the same
    construction process can create different
    representations
  • The algorithm for creating a complex object
    should be independent of the parts that make up
    the object and how they are assembled
  • Gives finer control over the construction process

5
Builder Pattern
6
Builder Pattern
7
Class MazeBuilder
  • class MazeBuilder
  • public
  • virtual void BuildMaze( )
  • virtual void BuildRoom( int room )
  • virtual void BuildDoor(
  • int roomFrom, int roomTo )
  • virtual Maze GetMaze( )
  • return NULL
  • protected
  • MazeBuilder( )

8
Method CreateMaze
  • Maze MazeGameCreateMaze(
  • MazeBuilder builder )
  • builder.BuildMaze( )
  • builder.BuildRoom(1)
  • builder.BuildRoom(2)
  • builder.BuildDoor(1, 2)
  • return builder.GetMaze( )

9
Class StandardMazeBuilder
  • class StandardMazeBuilder
  • public MazeBuilder
  • public
  • StandardMazeBuilder( )
  • virtual void BuildMaze( )
  • virtual void BuildRoom(
  • int room )
  • virtual void BuildDoor(
  • int roomFrom, int roomTo )
  • virtual Maze GetMaze( )
  • private
  • Maze currentMaze

10
Class CountingMazeBuilder
  • class CountingMazeBuilder
  • public MazeBuilder
  • public
  • CountingMazeBuilder( )
  • virtual void BuildMaze( )
  • virtual void BuildRoom(
  • int room )
  • virtual void BuildDoor(
  • int roomFrom, int roomTo )
  • virtual Maze GetCounts( )
  • private
  • int doors, rooms

11
Method BuildRoom
  • CountingMazeBuilder
  • CountingMazeBuilder()
  • rooms 0
  • doors 0
  • void CountingMazeBuilderBuildRoom(
  • int )
  • rooms

12
Invoking MazeBuilder
  • Maze maze
  • MazeGame game
  • StandardMazeBuilder builder1
  • game.CreateMaze( builder1 )
  • maze builder1.GetMaze()
  • int rooms, doors
  • CountingMazeBuilder builder2
  • game.CreateMaze( builder2 )
  • builder2.GetCounts( rooms, doors )

13
Singleton Pattern
  • Ensure a class only has one instance, and provide
    a global point of access to it
  • Provide controlled access to sole instance
  • Reduce name space

14
Singleton Pattern
15
Class MazeFactory
  • class MazeFactory
  • public
  • static MazeFactory Instance( )
  • protected
  • MazeFactory( )
  • private
  • static MazeFactory instance

16
Invoking Singleton Instance
  • MazeFactory
  • MazeFactoryinstance NULL
  • MazeFactory MazeFactoryInstance( )
  • if( instance NULL )
  • instance new MazeFactory
  • return instance

17
Subclass of a Singleton
  • class BombedMazeFactory
  • public MazeFactory

18
Invoking Singleton Instance
  • MazeFactory MazeFactoryInstance( )
  • if( instance ! NULL )
  • const char mazeStyle
  • getenv(MAZESTYLE)
  • if(strcmp(mazeStyle,bombed)0)
  • instance new BomedMazeFactory
  • else if
  • else
  • instance new MazeFactory
  • return instance

19
Class Singleton
  • class Singleton
  • public
  • static void Register( char name,
  • Singleton)
  • static Singleton Instance()
  • protected
  • static Singleton Lookup(
  • const char name)
  • private
  • static Singleton instance
  • static ListltNameSingletonPairgt registry

20
Method Instance
  • Singleton SingletonInstance()
  • if( instance NULL )
  • const char singletonName
  • getenv(SINGLETON)
  • instance
  • Lookup( singletonName )
  • return instance

21
Register a Singleton Object
  • class MySingleton public Singleton
  • Static MySingleton theSingleton
  • MySingletonMySingleton()
  • SingletonRegister(MySingleton,
  • this)

22
Drawing Editor
23
Adapter Pattern
  • Convert the interface of a class into another
    interface clients expect
  • Allow classes work together that could not
    otherwise because of incompatible interfaces
  • Clients call operations on an Adapter instance.
    In turn, the adapter calls Adaptee operations
    that carry out the request

24
Adapter Pattern
25
Class Shape
  • class Shape
  • public
  • Shape( )
  • virtual void BoundingBox (
  • Point bottomLeft,
  • Point topRight ) const
  • virtual Manipulator CreateManipulator( )
    const

26
Class TextView
  • class TextView
  • public
  • TextView( )
  • void GetOrigin(Coord x, Coord y) const
  • void GetExtent(Coord width,
  • Coord height) const
  • virtual bool IsEmpty( ) const

27
Class TextShape
  • class TextShape public Shape
  • public
  • TextShape(TextView)
  • virtual void BoundingBox(
  • Point bottomLeft,
  • Point topRight ) const
  • virtual bool IsEmpty( ) const
  • virtual Manipulator CreateManipulator( )
    const
  • private
  • TextView text

28
Method BoundingBox
  • void TextShapeBoundingBox(
  • Point bottomLeft,
  • Point topRight ) const
  • Coord bottom, left, width, height
  • text-gtGetOrigin(bottom, left)
  • text-gtGetExtent(width, height)
  • bottomLeft Point(bottom, left)
  • topRight Point(bottomheight, leftwidth)

29
Compiler System
30
Façade Pattern
  • Provide a unified interface to a set of
    interfaces in a subsystem
  • Define a higher-level interface that makes the
    subsystems easier to use
  • Not prevent applications from using subsystem
    classes if we need to
  • Can choose between ease of use and generality

31
Façade Pattern
32
Class Compiler
  • class Compiler
  • public
  • Compiler( )
  • virtual void Compile(
  • istream, BytecodeStream)

33
Method Compile
  • void CompilerCompile(
  • istream input,
  • BytecodeStream output)
  • Scanner scanner(input)
  • ProgramNodeBuilder builder
  • Parser parser
  • parser.parse(scanner, builder)
  • RISCCodeGenerator generator(output)
  • ProgramNode parseTree builder.GetRootNode(
    )
  • parseTree-gtTraverse(generator)

34
Graphics
35
Proxy Pattern
  • Provide a surrogate or placeholder for another
    object to control access to it
  • Forward requests to real subject when
    appropriate, depending on the kind of proxy
  • Remote proxy can hide the fact that an object
    resides in a different address space
  • Virtual proxy can perform optimizations such as
    creating an object on demand

36
Proxy Pattern
37
Class Graphic
  • class Graphic
  • public
  • virtual Graphic( )
  • virtual void Draw( ) 0
  • virtual void HandleMouse(
  • Event event)0
  • virtual
  • const Point GetExtent( ) 0
  • virtual void Load(istream from) 0
  • virtual void Save(ostream to)
  • 0
  • protected
  • Graphic( )

38
Class Image
  • class Image public Graphic
  • public
  • Image(string file)
  • virtual Image( )
  • virtual void Draw(
  • const Point at)
  • virtual void HandleMouse(
  • Event event)
  • virtual const Point GetExtent( )
  • virtual void Load(istream from)
  • virtual void Save(ostream to)

39
Class ImageProxy
  • class ImageProxy public Graphic
  • public
  • ImageProxy(string file)
  • virtual ImageProxy( )
  • protected
  • Image GetImage( )
  • private
  • Image image
  • Point extent
  • string fileName

40
Method GetImage
  • ImageProxyImageProxy(
  • string fileName)
  • fileName fileName
  • extent PointZero
  • image NULL
  • Image ImageProxyGetImage( )
  • if( image NULL )
  • image new Image(fileName)
  • return image

41
Methods in ImageProxy
  • void ImageProxyDraw(
  • const Point at)
  • GetImage( )-gtDraw(at)
  • void ImageProxyHandleMouse(
  • Event event ) GetImage( )
  • -gtHandleMouse(event)
  • void ImageProxySave(ostream to)
  • to ltlt extent ltlt fileName
  • void ImageProxyLoad(
  • ifstream from)
  • from gtgt extent gtgt fileName

42
Invoking ImageProxy
  • class TextDocument
  • public
  • TextDocument( )
  • Void Insert(Graphic)
  • TextDocument text
  • new TextDocument
  • Text-gtInsert( new ImageProxy(anImageFileName))

43
Font Dialog Box
44
Mediator Pattern
  • Define an object that encapsulates how a set of
    objects interact
  • Promote loose coupling by keeping objects from
    referring to each other explicitly
  • Let the client vary interactions of objects
    independently
  • Centralize control

45
Mediator Pattern
46
Mediator Pattern
47
Mediator Pattern
48
Class DialogDirector
  • class DialogDirector
  • public
  • virtual DialogDirector( )
  • virtual void ShowDialog( )
  • virtual void WidgetChanged(Widget) 0
  • protected
  • DialogDirector( )
  • virtual void CreateWidgets( ) 0

49
Class Widget
  • class Widget
  • public
  • Widget(DialogDirector)
  • virtual void Changed( )
  • virtual void HandleMouse(
  • MouseEvent event)
  • private
  • DialogDirector director

50
Class ListBox
  • class ListBox public Widget
  • public
  • ListBox(DialogDirector)
  • virtual string GetSelection( )
  • virtual void SetList(
  • Listltstringgt listItems)
  • virtual void HandleMouse(
  • MouseEvent event)

51
Class FontDialogDirector
  • class FontDialogDirector
  • public DialogDirector
  • public
  • FontDialogDirector( )
  • virtual FontDialogDirector( )
  • virtual void WidgetChanged(Widget)
  • protected
  • virtual void CreateWidgets( )
  • private
  • ListBox fontList

52
Presentations of Data
53
Observer Pattern
  • Define a one-to-many dependency between objects
    so that when one object changes state, all its
    dependents are notified and updated automatically

54
Observer Pattern
55
Observer Pattern
56
Class Observer
  • class Subject
  • class Observer
  • public
  • virtual Observer( )
  • virtual void Update(
  • Subject theChangedSubject) 0
  • protected
  • Observer( )

57
Class Subject
  • class Subject
  • public
  • virtual Subject( )
  • virtual void Attach(Observer)
  • virtual void Detach(Observer)
  • virtual void Notify( )
  • protected
  • Subject( )
  • private
  • ListltObservergt observers

58
Methods in Subject
  • void SubjectAttach(Observer o)
  • observers-gtAppend(o)
  • void SubjectNotify( )
  • ListIteratorltObservergt i(observers)
  • for(i.First( ) !i.IsDone( ) i.Next( ))
  • i.CurrentItem( )-gtUpdate(this)

59
TCP Connection
60
State Pattern
  • Allow an object to alter its behavior when its
    internal state changes
  • Localize state-specific behavior and partition
    behavior for different states
  • Make state transitions explicit
  • State objects can be shared

61
State Pattern
62
Class TCPConnection
  • class TCPState
  • class TCPConnection
  • public
  • TCPConnection( )
  • void ActiveOpen( )
  • private
  • friend class TCPState
  • void ChangeState(TCPState)
  • private
  • TCPState state

63
Class TCPState
  • class TCPState
  • public
  • virtual void ActiveOpen(TCPConnection)
  • virtual void Synchronize(TCPConnection)
  • virtual void Acknowledge(TCPConnection)
  • virtual void Send(TCPConnection)
  • protected
  • void ChangeState(
  • TCPConnection,TCPState)

64
State Classes
  • class TCPListen public TCPState
  • public
  • static TCPState Instance( )
  • virtual void Send(TCPConnection)
  • class TCPClosed public TCPState
  • public
  • static TCPState Instance( )
  • virtual void ActiveOpen(TCPConnection)

65
Document-Application Application Framework
66
Template Method Pattern
  • Define the skeleton of an algorithm in an
    operation, deferring some steps to subclasses
  • Let subclasses redefine certain steps of an
    algorithm without changing the algorithms
    structure
  • Hook operations

67
Template Method Pattern
68
Methods in View
  • void ViewDisplay( )
  • SetFocus( )
  • DoDisplay( )
  • ResetFocus( )
  • void ViewDoDisplay( )
  • void MyViewDoDisplay( )
Write a Comment
User Comments (0)
About PowerShow.com