You are on page 1of 29

Log in 

Search... Search

Download presentation

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

Des gnButtons:
patterns
• What s a pattern?
2
– Pattern descr bes a problem wh ch occurs
over and over aga n n our env ronment, and
Cancel Download
then descr bes the core of the solut on to that
problem, n such a way that you can use th s
solut on a m ll on t mes over , . . .

Chr stopher Alexander


(arch tecture theor st)

Department of Informat on Eng neer ng 3

1 / 209

 Similar presentations
Department of Information Engineering 395
Design patterns What is a pattern? –Pattern
describes a problem which occurs over and
over again in our environment,
Published by Michael Haskell Modi ed over 5 years ago
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
 Embed

 Download presentation

Download presentation

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
Presentation on theme: "Department of Information
Buttons: patterns What is a pattern? –Pattern
Engineering 395 Design
describes a problem which occurs over and over again in our
environment,"— Presentation transcript:
2
1 Department of
Information Engineering 395 Design patterns What is a pattern? –
Pattern describes a problem which occurs over and over again in our
Cancel Download
environment, and then describes the core of the solution to that
problem, in such a way that you can use this solution a million times
over,... Christopher Alexander (architecture theorist)

2 Department of Information Engineering 396 Patterns are


common Design catalogs –Script writers –Fashion designers –Interior
designers –Architects Patterns are common tricks used by the
profession –Don’t reinventing the wheel –Simply copy a proven design

3 Department of Information Engineering 397 “Design Pattern” by


Gamma et al 23 common design patterns divided into 3 groups 1.How
to create objects (creational patterns) 2.How to group them (structural
patterns) 3.How to use them (behavior patterns) –Two general
approaches in patterns –Class inheritance –Object delegation

4 Department of Information Engineering 398 Each pattern has –


Name –Problem statement –Solution –Consequences Importance of
the name –To facilitate communication between developers

5 Department of Information Engineering 399 Exercises Simple


quiz –http://home.earthlink.net/~huston2/dp/patterns_quiz.html A list
of open-end questions –
http://www.industriallogic.com/papers/learning.html#QUESTI ONS

6 Department of Information Engineering 400 Two fundamental


principles in OO Programming Programming to an Interface, not an
Implementation –so that you can change the implementation easily in
future, the interface is just like a socket Favour object composition over
class inheritance –To promote more exible reuse –Don’t overuse
inheritance (only for classes that share the same behavior)

7 Department of Information Engineering 401 Example: Maze


game
To make thisTo writework,
website a maze
we logadventure game
user data and shareThe game
it with –A maze
processors. has-a
To use this website, you must agree to our Privacy Policy, including cookie
number of rooms –A room has four sides (North, policy. South,    West) –A
East,I agree. /
player enters a side If the side is a door, it leads to another room If the
side is a wall, the player is hurt –The player wins if a way leaving the
maze is found

Download presentation
8 Department of Information Engineering 402 The objects
MazeGame, Maze, Room, Door, Side Polymorphism –Group Room,
Door and Side underWethethink
abstract base liked
you have class this
MapSite Base classIf virtual
presentation. you wish to
function –Enter() How to build the
download maze recommend
it, please using OO concepts?
it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
9 Department of Information Engineering 403 Is-a relation A Room
is-a kind of MapSite Buttons:
A Door is-a kind of MapSite A Wall is-a kind of
MapSite Is-a suggests class inheritance MapSite RoomDoor Wall Is-a

10 Department of Information Engineering 404 Is-a relation


2
Skeleton code class MapSite { //ABSTRACT class public virtual void
Enter(){} } class Room : MapSite { //CONCRETE class public override void
Enter(){…} //implementation } Room.Enter() - enter a room Door.Enter()
- enter the room beyond the door Wall.Enter() - get hurt, stay atCancel
the Download
same room

11 Department of Information Engineering 405 Has-a relation A


maze has-a number of rooms A room has four sides A room may have
several smaller rooms Has-a suggests object composition

12 Department of Information Engineering 406 A UML diagram


describing the maze structure Maze AddRoom() RoomNo() ArrayList
_rooms MapSite Enter() Room Enter() SetSide() GetSide() RoomNo Wall
Enter() Door Enter() rooms Recursion. Known as Composite pattern
Has-a

13 Department of Information Engineering 407 Object


composition Maze is composed of (has-a) Rooms Room is composed of
Doors, Walls and Rooms. class Maze { private ArrayList _rooms=new
ArrayList(); //a typeless container that holds the rooms } class Room :
MapSite { public override void Enter() {//...} private MapSite[] _sides =
new MapSite[4]; }

14 Department of Information Engineering 408 Wall, Door class


Wall : MapSite { public override void Enter() {...} } class Door : MapSite {
private Room _room1; private Room _room2; public Door(Room r1,
Room r2) {...} public override void Enter() {...} } static void Main() {...
MazeGame game = new MazeGame(); _aMaze = game.CreateMaze();
//create the maze... }

15 Department of Information Engineering 409


MazeGame.CreateMaze() creates a simple maze N Room 1 Room 2
_aMaze _rooms r1 _side[0] _side[1] _side[2] _side[3] r2... aWall
aDoor(r1,r2) aWall Objects structure

16 Department of Information Engineering 410 Maze


To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
MazeGame.CreateMaze() { Maze aMaze = new Maze(); //create a maze
policy. I agree.      /
object Room r1 = new Room(1); //create rooms Room r2 = new
Room(2); Door theDoor = new Door(r1, r2); //create door
r1.SetSide(North, new Wall()); //add wall to room r1.SetSide(East,
theDoor()); //add door to room r1.SetSide(South, new Wall()); 
Download presentation
r1.SetSide(West, new Wall()); //... init r2 like r1
aMaze.AddRoom(r1);//add to container aMaze.AddRoom(r2); return
aMaze; }
We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
17 Departmentsystem.
of Information Engineering 411 What is wrong with
Share buttons are a little bit lower. Thank you!
CreateMaze() ? MazeGame Maze MazeGame.CreateMaze() { Maze
aMaze = new Maze();Buttons:
Room r1 = new Room(1); Room r2 = new Room(2);
Door theDoor = new Door(r1, r2);... return aMaze; }

18 Department of Information Engineering 412 What if... in future,


we would like –to CREATE a di erent kind of room? –to CREATE a 2
di erent kind of door? –to CREATE a di erent kind of wall? We need to
change the code in CreateMaze()
Cancel Download
19 Department of Information Engineering 413 This is what we
have to do – break the old code ! MazeGame Maze
MazeGame.CreateMaze() { Maze aMaze = new Maze(); Room r1 = new
EnchantedRoom(1); Room r2 = new EnchantedRoom(2); Door theDoor
= new DoorNeedSpell(r1, r2); return aMaze; }

20 Department of Information Engineering 414 Solutions? Class


inheritance –Factory method Object delegation –Abstract factory –
Builder –Prototype

21 Department of Information Engineering 415 Factory method


Replace new by virtual function –Virtual function can be overrided by
the subclass, so that di erent objects can be created MazeGame Maze
MazeGame.CreateMaze() { Room r1 = MakeRoom(1); //... } //base class
default MakeRoom() Room MazeGame.MakeRoom(int n) { return new
Room(n); }

22 Department of Information Engineering 416 Let subclass to


override the factory method MazeGame Maze
MazeGame.CreateMaze() { Room r1 = MakeRoom(1); }
EnchantedMazeGame //override base class MakeRoom() Room
EnchantedMazeGame.MakeRoom(int n) { return new
EnchantedRoom(n, SpellDoor()) }

23 Department of Information Engineering 417 Change one line


and have a di erent MazeGame public class Form1 : Form Maze
_aMaze; static void Main() { using (Form1 form = new Form1()) {
form.InitializeGame(); Application.Run(form); } private void
InitializeGame() { //MazeGame game = new MazeGame(); MazeGame
game = new EnchantedMazeGame(); _aMaze = game.CreateMaze();
//create the maze } //... code for the event handlers }

24 Department
To make this website work, we of
logInformation Engineering
user data and share 418 Factory
it with processors. To use Method
this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
De ne an interface (the virtual functions) for creating an object, but let
subclasses decide which class to instantiate. Factory Method lets a class
defer instantiation to subclasses. Also known as virtual constructor

Download presentation
25 Department of Information Engineering 419 Framework, toolkit
and application What is the di erence between framework, toolkit and
application? –Framework has the highest degree of reuse, and is the
We think you have liked this presentation. If you wish to
most exible –Application is the least exible
download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
26 Department of Information Engineering 420 What is a
Buttons:
framework? A framework is an abstract type of software that captures
the design decisions that are common to its application domains –e.g.
what is the similarity between a circuit design editor, and musical score
editor? –Both manipulate graphical objects An abstract software
(framework) that can support both applications? 2

27 Department of Information Engineering 421 A general


framework for graphical processing AbstractFramework NewDoc(){Cancel Download
AbstractGraphic g; g = CreateGraphic(); } MyGraphicApplication
CreateGraphic() //factory method { return new MyGraphic(); }
AbstractGraphic Move(); Scale(); MyGraphic Framework

28 Department of Information Engineering 422 A framework for


graphical processing The concrete MyGraphic object can be any graphic
symbols, e.g. musical notes, circuit symbols These objects can be
manipulated by a uniform set of operations (e.g. Move(), Scale(), etc)
Code in framework knows how to manipulate the abstract base class
AbstractGraphic object The concrete subclass object ( MyGraphic ) is
created di erently for di erent application

29 Department of Information Engineering 423 A musical score


editor AbstractFramework NewDoc(){ AbstractGraphic g; g =
CreateGraphic(); } MyMusicScoreApplication CreateGraphic() //factory
method { return new MyMusicScore(); } AbstractGraphic Move(); Scale();
MyMusicScore Framework

30 Department of Information Engineering 424 A electronic circuit


editor AbstractFramework NewDoc(){ AbstractGraphic g; g =
CreateGraphic(); } MyCircuitApplication CreateGraphic() //factory
method { return new MyCircuit(); } AbstractGraphic Move(); Scale();
MyCircuit Framework

31 Department of Information Engineering 425 Thinking in two


levels –Abstract level (framework level) I (framework) don’t care what
the symbols (objects) are about ! My job is just to move, to scale, to
draw, and to print the symbols How to draw/print the symbols? That is
the job of the symbols themselves (think like a boss!) –Concrete level All
the (dirty) real work is done by me (concrete objects)

32 Department of Information Engineering 426 What is a


framework?
To make this websiteFramework emphasizes
work, we log user design
data and share reuse
it with rather
processors. To than
use thiscode
website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
reuse Framework contains the main program, users customize the
framework by providing the concrete subclasses Modern systems have
layers of frameworks that cooperate with each other

Download presentation
33 Department of Information Engineering 427 Toolkit Toolkit is
just a set of reusable classes, providing the functionality Emphasize
code reuse Users write the main program, toolkit provides reusable
We think you have liked this presentation. If you wish to
code Framework is harder to code than toolkit Toolkit is harder to code
download it, please recommend it to your friends in any social
than application
system. Share buttons are a little bit lower. Thank you!

34 DepartmentButtons:
of Information Engineering 428 Pros and cons of
class inheritance Pros –Simpler than object delegation, codes are easier
to follow Cons –Static binding, xed at compilation time, cannot be
changed at run-time –Need to create new subclass Ways to reduce the
number of subclasses? 2

35 Department of Information Engineering 429 Parameterized


factory methods The method takes a parameter that identi es Cancel
the kind Download
of product Parameterized factory methods reduce the number of
subclasses that are needed Use –In document applications, di erent
document type has di erent le extent (.doc,.xml,.txt,...)

36 Department of Information Engineering 430 Parameterized


factory methods class Creator { public virtual Product Create(int id){} }
class ConcreteCreator : Creator { public override Product Create(int id) {
if (id==A) return new ProductA; if... return base.Creator(id); }

37 Department of Information Engineering 431 Use Generic to


avoid subclassing Problem with factory methods –For every new
product, you need to subclassing the Creator for creating the product
Use Generics to void subclassing –The code for the subclass is
automatically generated by the compiler In the following example,
GenericCreator is just a template, compiler uses it to generate the code
38 Department of Information Engineering 432 Use Generic to
avoid subclassing class Creator { public virtual Product Create(){} } class
GenericCreator : Creator { public override Product Create(){ return new
T(); } Main() { Creator myCreator = new GenericCreator (); Product p =
myCreator.Create(); //factory method }

39 Department of Information Engineering 433 The code


generated class GenericCreator : Creator { public override Product
Create(){ return new ProductA(); }

40 Department of Information Engineering 434 Create objects by


Object Delegation MazeGame CreateMaze(f) { //f is a factory object
Maze aMaze = new Maze(); Room r1 = f.MakeRoom(1); Room r2 =
f.MakeRoom(2); //... return aMaze; } AbstractFactory MakeRoom(int n)
ConcreteFactory MakeRoom(int n) { return new Room(n); } delegate
MakeRoom is a factory method

41
To make this website work, we of
Department logInformation
user data and share it with processors.
Engineering To use this
435 Abstract website,
Factory
policy. I agree.     
Inyou must agree to our Privacy Policy, including cookie
/
many applications, we don’t change just one type of object, but, rather,
a family of objects This can be done by using a factory object To select
a di erent game at run-time –Just change the (concrete) factory object f
Abstract Factory provides an interface for creating families of related or 
Download presentation
dependent objects without specifying their concrete classes

42 Department of Information Engineering 436 Example - the


We think you have liked this presentation. If you wish to
Look-and-Feel Problem How toit,change
download pleasethe look and feel
recommend it toof thefriends
your desktopin any social
at run-time? –e.g. change to MotifWindow, MSWindow, AppleWindow
system. Share buttons are a little bit lower. Thank you!
Solution –Choose a concrete factory object MotifFactory, MSFactory, or
AppleFactory –The factory object creates the concrete products
Buttons:
MSWindow, MSScrollBar,...) –but returns them as abstract products
Window, ScrollBar

43 Department of Information Engineering 437 Abstract Factory:2


client uses an abstract factory GUIFactory to produce abstract products
GUIFactory CreateScrollBar() CreateWindow() MotifFactory
CreateScrollBar() CreateWindow() client Window Cancel Download
MSWindowMotifWindow ScrollBar MSScrollBarMotifScrollBar
MSFactory CreateScrollBar() CreateWindow() delegate Is-a

44 Department of Information Engineering 438 Abstract Factory


GUIFactory guifactory; //choose a concrete factory object if (strcmp(
inputName, “Motif”) == 0 ) { guifactory = new MotifFactory(); } else {
guifactory = new MSFactory(); } //factory returns abstract product
Window w = guifactory.CreateWindow(); w.Draw(); Di erent factory
creates di erent concrete product Return the abstract product,
upcasting, polymorphism

45 Department of Information Engineering 439 Builder Problem –


To build a complex object, e.g. a building, an aircraft The complex
object has di erent representations –i.e. we can look at the object at
di erent ways –e.g. di erent representations of an aircraft Its look Its
total weight Its total cost

46 Department of Information Engineering 440 Builder What to


reuse? –The construction of the object is complex –Want to reuse the
construction process so as to create di erent representations Solution?
–Separate the construction of a complex object from its representation
so that the same construction process can create di erent
representations

47 Department of Information Engineering 441 Builder MazeGame


CreateMaze(builder) { builder.BuildMaze(); builder.BuildRoom(1);
builder.BuildRoom(2); //... return builder.GetMaze(); } AbstractBuilder
buildRoom(int n) ConcreteBuilder buildRoom(int n) { return new
Room(n); } delegate Construction Representation

48 Department of Information Engineering 442 Builder Change the


concrete Builder, get a di erent product Example –CountBuilder To
count
To make thisthe number
website of log
work, we parts
user(rooms, doors)
data and share used
it with in the maze
processors. To use–this website, you must agree to our Privacy Policy, including cookie
SimpleBuilder Return a simpli ed graphical maze object
policy. that    
I agree. is good /
for low-end mobile phone

49 Department of Information Engineering 443 Intent and its


importance AbstractDownload
Factory andpresentation
Builder are di erent patterns for 
solving di erent problems But they have the same code structure !! –
They have the same UML diagrams Code is not everything –Should also
know the intent of the
Wecode
think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
50 Department of Information
system. Engineering
Share buttons are a 444
littleDibiterence
lower. Thank you!
between Abstract Factory and Builder Their intent are di erent
Buttons:
Abstract Factory –Provide an interface for creating families of related or
dependent objects without specifying their concrete classes Builder –
Separate the construction of a complex object from its representation
so that the same construction process can create di erent
representations
2

51 Department of Information Engineering 445 Abstract Factory


Cancel
and Builder Abstract Factory focuses on building a families of parts Download
(either simple or complex), and returns the part immediately –Client
assembles the parts into complex object Builder returns the product as
a nal step –The concrete parts in the complex object is encapsulated
inside the Builder –Parts have di erent representations that are hidden
from the client Appearance, weight, cost,... –Change Builder, change
representation

52 Department of Information Engineering 446 Problem To build a


maze that allows player to choose the composition of the maze at run-
time e.g. the maze can be made up of EnchantedRoom, DoorWithSpell
and BombedWall The problem is like creating an ice cream object with
di erent combination of favours Use a factory to build these parts so
that player can choose the composition dynamically

53 Department of Information Engineering 447 Problem with


subclassing AbstractFactory Factory1 Door Wall Room Factory2
DoorWithSpell Wall Room Factory3 Door BombedWall Room Factory4
Door Wall EnchantedRoom Factory5 DoorWithSpell BombedWall
Room... Factory8 DoorWithSpell BombedWall EnchantedRoom

54 Department of Information Engineering 448 Combinatorial


problem If we have 5 di erent types of rooms, doors and walls, we
have 5*5*5 di erent combinations –Need to create 125 subclasses ! –
Class explosion, a common problem with subclassing A better
approach - composition –Construct a prototype object at run-time –The
prototype is composed of EnchantedRoom, DoorWithSpell and
BombedWall –Prototype is a factory object that manufactures the parts
by cloning

55 Department of Information Engineering 449 The Prototype


MazePrototypeFactory factory class MazePrototypeFactory :
MazeFactory { public MazePrototypeFactory(Maze m, Wall w, Room r,
Door
To make d);
this public
website virtual
work, Maze
we log MakeMaze();
user data and share it public virtual To
with processors. Room
use this website, you must agree to our Privacy Policy, including cookie
MakeRoom(int i); public virtual Wall MakeWall();policy. publicI agree.
virtual    Door /
MakeDoor(Room r1, Room r2); private Maze _prototypeMaze; private
Room _prototypeRoom; private Wall _prototypeWall; private Door
_prototypeDoor; }

Download presentation
56 Department of Information Engineering 450 To create the
prototype object int Main() { MazeGame game; MazePrototypeFactory
factory(new Maze(), We
newthink
BombedWall(), new this
you have liked EnchantedRoom(),
presentation. Ifnew you wish to
DoorWithSpell()); Maze maze =it,
download game.CreateMaze(factory);...
please recommend it to your } factory
friends in any social
Maze _maze = new Maze()
system.Wall _wall
Share = new are
buttons BombedWall() RoomThank
a little bit lower. _roomyou!
= new EnchantedRoom() Door _door = new DoorWithSpell() Construct a
Prototype factory object
Buttons:

57 Department of Information Engineering 451 The prototype


constructor Create the prototype object by composition
MazePrototypeFactory(Maze m, Wall w, Room r, Door d) { _maze = m;
2
//point to an concrete object _wall = w; _room = r; _door = d; } Has-a
relation (a prototype has-a maze, wall, room and door) Also known as
composition Cancel Download

58 Department of Information Engineering 452 How to use it? We


use the prototype factory object like an Abstract Factory object Maze
MazeGame.CreateMaze(MazeFactory factory) { Maze aMaze =
factory.MakeMaze(); Room r1 = factory.MakeRoom(1);... } So what is
inside factory.MakeRoom() ?

59 Department of Information Engineering 453 How to use it?


factory.MakeRoom() uses the existing Room object to clone a new
EnchantedRoom createMaze(factory) r1=factory.MakeRoom(1)
MazeFactory MazePrototypeFactory private Room _room; Room
MakeRoom(int i) { r=_room.Clone(); r.Number = i; return r; }
EnchantedRoom Room Clone() { Room r=new EnchantedRoom(); //copy
attributes from this return r; } A clone of EnchantedRoom Room Room
Clone() {}

60 Department of Information Engineering 454 Summary –


Creational Patterns Factory Method De ne an interface for creating an
object, but let subclasses decide which class to instantiate. Factory
Method lets a class defer instantiation to subclasses Abstract Factory
Provide an interface for creating families of related or dependent
objects without specifying their concrete classes Builder Separate the
construction of a complex object from its representation so that the
same construction process can create di erent representations
Prototype Specify the kinds of objects to create using a prototypical
instance, and create new objects by copying this prototype

61 Department of Information Engineering 455 Singleton How to


create one and only one instance of a class, –e.g. a single print queue
shared by many printer –a single window server for the user interface
The single object must be easily accessible Intent –Ensure a class only
has one instance, and provide a global point of access to it

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
62 Department of Information Engineeringpolicy. 456 The consideration
I agree.      /
To make the object easily accessible –Use a global/static object
Drawback –No guarantee that there is only one instance of the class,
e.g. static Singleton object_1 = new Singleton(); static Singleton object
_2 = new Singleton(); Problem - two objects of the same class Solution - 
Download presentation
use a private constructor !

63 Department of Information Engineering 457 Use private


We think you have liked this presentation. If you wish to
constructor and public methodit,sealed
download pleaseclass Singletonit{to
recommend protected
your friends in any social
Singleton(); private static Singleton _instance=null; public static
system. Share buttons are a little bit lower. Thank you!
Singleton Instance() { if (_instance==null) { _instance = new Singleton(); }
return _instance; } Main() {//to use a Singleton Singleton server1 =
Buttons:
Singleton.Instance(); Singleton server3 = new Singleton(); //won’t
compile, constructor is protected }

64 Department of Information Engineering 458 Singleton() is 2


private; object can only be created via the function Instance() Instance()
controls the number of objects created But Instance() is an object level
method –With a private constructor, we have a problem in creating the
Cancel Download
rst object –Without an object, we cannot invoke Instance() Instance()
must therefore be a class level ( static ) method

65 Department of Information Engineering 459 Creational


patterns –Create objects exibly Structural patterns –Group objects
together to form more complex structure

66 Department of Information Engineering 460 Adapter Problem –


A bank is developing a new system with a new interface, how to reuse
the old (legacy) system ? client INewSystem NewRequest() OldSystem
OldRequest()

67 Department of Information Engineering 461 Solution New Old


ClassAdapter New Old ObjectAdapter Class Adapter Object Adapter

68 Department of Information Engineering 462 Class Adapter - by


class inheritance client INewSystem NewRequest() OldSystem
OldRequest() ClassAdapter NewRequest() { OldRequest() }

69 Department of Information Engineering 463 Class Adapter


public class ClassAdapter : OldSystem, INewSystem { public void
INewSystem.Request() { base.OldRequest(); //base refers to OldSystem
} Main() { INewSystem sys = new ClassAdapter(); }

70 Department of Information Engineering 464 Problems Modern


OO languages (C#, Java) only support single implementation
inheritance Cannot hide the old implementation –Interface in
OldSystem can be exposed by casting class ClassAdapter : private
OldSystem, INewSystem Object Adapter does not have these problems
Ideal, but not supported in C#

71 Department of Information Engineering 465 Object Adapter


To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
client NewSystem NewRequest() Object Adapterpolicy. NewRequest()
I agree.      /
adaptee->OldRequest() OldSystem OldRequest() adaptee

72 Department of Information Engineering 466 Object Adapter


public class ObjectAdapter : NewSystem { private OldSystem _oldSys = 
Download presentation
new OldSystem(); public void NewRequest() { _oldSys.OldRequest();
//delegation } Single implementation inheritance Interface of the old
system is hidden from client
We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
73 Department of Information
system. Engineering
Share buttons are a 467
littleAdapter Also
bit lower. Thank you!
Known as –Wrapper Intent –Convert the interface of a class into
Buttons:
another interface clients expect. –Adapter lets classes work together
that couldn’t otherwise because of incompatible interfaces

74 Department of Information Engineering 468 Problem –To


2
support a computer game for two di erent hardware platforms
Solution by subclassing Game XBoxGamePSGame abstraction
implementations
Cancel Download
75 Department of Information Engineering 469 Bridge Problem
with extending the abstraction –To add a platform (Nokia phone), need
to port the implementation of the game to the new platform A better
solution –Separate the platform-speci c code from the abstraction (of
the game)

76 Department of Information Engineering 470 Bridge Decouple


(separate) an abstraction from its implementation so that the two can
vary independently Abstraction Implementor Operation() Abstract_1
Abstract_2 Imp_1 Imp_2Imp_3 imp imp.Operation()

77 Department of Information Engineering 471 Game DrawText()


DrawRect() GameImp imp ConcreteGame1 DrawBorder() DrawRect()
DrawText() GameImp DevDrawText() DevDrawLine() XBoxImp PSImp
imp imp.DevDrawLine() Abstraction Implementation

78 Department of Information Engineering 472 Bridge Intent


Decouple an abstraction from its implementation so that the two can
vary independently The Graphics object in GDI+ is an example of Bridge
pattern –GDI+, platform speci c code is separated and encapsulated in
a Graphics object; –Di erent Graphics object can be used to represent
di erent graphic card

79 Department of Information Engineering 473 Composite A


graphics application has primitives (line, circle), which can be grouped
to form a larger component –Component is just a container –
Components can be grouped to form an even larger components
Problem –How to treat primitives and components uniformly –e.g. if g
is a line, g.Draw() will draw a line –if g is a component, g.Draw() will
draw all the composition recursively

80 Department of Information Engineering 474 Composite


To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
Uniform interface - suggest the use of a base class Graphic Draw()
policy. I agree.      /
Picture Draw() Circle Draw() Line Draw()

81 Department of Information Engineering 475 Composite A


Picture is a Composite (has-a pictures/primitives) 
Download presentation –Recursion Graphic
Draw() Picture Draw() Circle Draw() Line Draw()

82 DepartmentWe of Information
think you haveEngineering
liked this 476 Recursive If
presentation. object
you wish to
structure aPicture aLineaCircle
download it, aPicture
please aLineaCircle
recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
83 Department of Information Engineering 477 Composite code
Buttons:
example class Graphic { public virtual void Draw(); //... } class Picture :
Graphic { private ArrayList graphics; //container public void Draw() {
foreach (object o in ArrayList) { Graphic g = (Graphic) o; g.Draw(); //if g
is-a line, draw line } //if g is-a picture, recursion }
2

84 Department of Information Engineering 478 Composite Intent –


Compose objects into tree structures to represent part-whole
Cancel Download
hierarchies. Composite let clients treat individual objects and
compositions of objects uniformly Note –Part-whole hierarchies are
extremely common, making Composite one of the most useful pattern

85 Department of Information Engineering 479 Decorator Problem


–How to create a reader object that can Read a stream from the le
Decrypt the stream Perform a formatted read (read an integer, double,
etc, from byte stream) –How to build this reader object exibly?

86 Department of Information Engineering 480 Class inheritance


approach Create more specialized objects by subclassing The concrete
BinaryRead object –use FileStream code to read the le –use
CryptoStream code to decrypt the le –use its own code to do
formatted read FileStream CryptoStream BinaryReader Read le
inputDecryptFormatted read

87 Department of Information Engineering 481 Class inheritance


approach Combinatorial problem, need many subclasses Static binding
– can’t change at run-time Object delegation –More exible than by
subclassing –Concept is similar to piping in Unix, a complex object is
formed by joining several objects together Structure of a decorator

88 Department of Information Engineering 482 Decorator To


create new object by composition and perform a chain of actions –
FileStream.Read() then CryptoStream.Read() then
BinaryReader.ReadXxx() BinaryReader ReadXxx() Stream _next
CryptoStream Read() Stream _next FileStream Read() Stream _next A
decorator object

89 Department of Information Engineering 483 Decorator object


diagram BinaryReader Stream _next; ReadXxx() { if (!_next) _next.Read();
//…format data } CryptoStream Stream _next; Read() { if (!_next)
_next.Read(); //…decrypt data } FileStream Read() { //read data } Has-a
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
relationship Software piping by delegation

90 Department of Information Engineering 484 Decorator class


diagram To support Download
compositionpresentation
of inde nite number of objects 
Stream Read(byte[] bu er, int o set, int count) ConcreteStream
Read(...) Stream _next Has-a 1
We think you have liked this presentation. If you wish to
91 Departmentdownload
of Information Engineering
it, please 485 Class
recommend diagram
it to your of in any social
friends
System.IO in C# Stream CryptoStream
system. FileStream
Share buttons TextWriter
are a little bit lower. Thank you!
StreamWriterStringWriter BinaryWriter Bu erStream NetworkStream
Reader has the sameButtons:
class diagram as Writer Note the decorator
pattern

92 Department of Information Engineering 486 The structure of


2
the IO design TextWriter class –For writing characters –StreamWriter –
for writing a inde nite sequence of characters BinaryWriter class –Class
for writing a binary stream Stream class –For adding di erent features
exibly using the decorator pattern Cancel Download

93 Department of Information Engineering 487 Example An object


that formats, encrypts, and writes to le using System; using System.IO;
using System.Security.Cryptography; static void Main(string[] args) {
//initialization code skipped FileStream fout = new FileStream(...);
CryptoStream cs = new CryptoStream(fout,... ); BinaryWriter bw = new
BinaryWriter(cs); bw.Write("hello world");//format, encrypted, write to
le bw.Close(); cs.Close(); fout.Close();

94 Department of Information Engineering 488 Decorator Also


known as –Wrapper Intent –Attach additional responsibilities to an
object dynamically. –Decorators provide a exible alternative to
subclassing for extending functionality Known Uses –I/O objects in C#,
Java –graphical user interface (see text)

95 Department of Information Engineering 489 Façade Problem –


Objects have too many dependency, how to reduce the dependency?
Subsystem

96 Department of Information Engineering 490 Façade Solution –


Create Subsystem with a single and uni ed interface Facade Subsystem
97 Department of Information Engineering 491 Façade Intent –
Provide a uni ed interface to a set of interfaces in a subsystem. Façade
de nes a higher-level interface that makes the subsystem easier to use
–Façade objects are often Singletons C# internal keyword –A subsystem
has public classes and encapsulated private classes –Use internal
keyword for encapsulating classes within an assembly, and public
keyword for the Façade public class

98 Department of Information Engineering 492 Proxy Problem –


Some objects are expensive to create e.g. a large document with many
objects (text, images) –Create all the objects at once takes too long
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
Solution –Create a proxy object quickly –Only create the objects when
policy. I agree.      /
they are needed (visible in the document) Proxy

99 Department of Information Engineering 493 Proxy ImageProxy


is an object that canDownload
be created very cheaply, it maintains a reference 
presentation
to the image on disk aTextDocument image anImageProxy leName
anImage data in memory on disk
We think you have liked this presentation. If you wish to
100 Department of Information
download Engineering
it, please recommend 494itImage Proxy
to your friends in any social
client Graphic Draw()system.
ImageProxy
ShareDraw() Image
buttons are aimage Image
little bit Draw()
lower. Thank you!
realSubject //draw only on-demand if(image == 0) {
image=LoadImage( Buttons:
leName); } image.Draw() (has-a)

101 Department of Information Engineering 495 Proxy server


example client WebServer Get(url)... WebProxy Get(url)... OriginServer
2
Get(url)... //get over the net only on-demand if( url_not_in_cache ) {
url=server.Get(url); } return url

102 Cancel
Department of Information Engineering 496 Proxy application Download
virtual proxy (as in our example) –creates expensive objects on demand
–e.g. virtual memory in an OO Unix remote proxy –provides a local
representative for an object in a di erent address space (e.g CORBA,
RMI,.NET) protection proxy –control access to the original object
(authentication) smart reference –replacement of a bare pointer that
performs additional actions, e.g. web page ltering

103 Department of Information Engineering 497 Flyweight A


pattern for system that has a large number of tiny objects Scenario –In
a word processing application, we have row objects column objects
character objects –A column may have several rows, and each row may
have many character objects –Use Composite Pattern to model this
structure

104 Department of Information Engineering 498 Flyweight


Composite Pattern –All objects are treated uniformly by the functions
provided in base class Glyph –Captures the tree-structure of the
document Glyph Rowcolumn

105 Department of Information Engineering 499 Flyweight


Problem –Large number of small objects ! –One page ~ 500 words ~
2500 characters (objects)

106 Department of Information Engineering 500 Flyweight


Solution –Each character object is a singleton shared in a pool

107 Department of Information Engineering 501 An object has


Sharable attributes –e.g. the character code Non-sharable attributes –
e.g. font type and font size Sharable attributes are stored in Singleton
objects, located in a sharable yweight pool The non-sharable
attributes are stored in a context object ( GlyphContext ) Example of
non-sharable context –font size, font type
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
108 Department of Information Engineering 502 Represent the
context by a table The table can be implemented using Dictionary that
supports the IDictionary interface Dictionary has a collection of
key/value pairs –KeyDownload
= position, string=context 
presentationint(key)string(value) 1
Times 24 //1 char 2 Times 12 //next 100 char 102 Times-Italic 12 //next
6 char 108 Times 12 //next 194 char...
We think you have liked this presentation. If you wish to
109 Department of Information
download Engineering
it, please recommend 503itFlyweight To
to your friends in any social
draw a Character object –Glyph.Draw()
system. is not
Share buttons areenough
a little Character
bit lower. Thank you!
contains only the sharable attributes, need the GlyphContext object to
Buttons:
get the context –Glyph.Draw(GlyphContext) complete information =
Glyph + GlyphContext

110 Department of Information Engineering 504 Creational


2
patterns –How to create objects exibly Structural patterns –How to
group objects together Behavioral patterns –How to use objects in a
collaborative way –The key - identify the responsibilities of the objects
Cancel Download
111 Department of Information Engineering 505 Course
Registration System Design a course registration system to support –
Course browsing –Course registration –Constraint checking (e.g. pre-
requisite) –Course functions (e.g. print course list) Problem –What is the
object structure that we should use?

112 Department of Information Engineering 506 Arrange the


courses by a tree structure university Law Engineering Science CSE IE
IEG3080 composite leaf

113 Department of Information Engineering 507 Composite


pattern Node string _name; NodeList //name=Erg,Sci,... Course
//name=IEG3080...

114 Department of Information Engineering 508 Problem - GUI


framework to support course browsing? Course InfoRegistration Menu
bar ALL ART MED SCI ERG ACE CE CS EE SEEM IE Menu item Menu
IEG1110 IEG2220... IEG3080

115 Department of Information Engineering 509 Browsing The


menu Menu item –ALL, ART, ERG,..., IE,..., IEG3080 Each item has a
di erent Action –ALL will list all courses provided –ART will list all
courses in ART faculty –IEG3080 will list course info in IEG3080 ALL ART
ERG MED SCI

116 Department of Information Engineering 510 A GUI framework


can only provide the following abstract widgets –Menu –MenuItem A
Menu has-a number of MenuItems –Select a MenuItem, an action will
be executed How to associate the MenuItem with your action?
MenuItem Your action GUI framework

117 Department of Information Engineering 511 By class


To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
inheritance MenuItem is an abstract base class policy. that has an Execute()
I agree.      /
Subclass implements Execute() Simple, static binding MenuItem
Execute() MyMenuItem Execute() Action() Menu Abstract framework

118 Department of Information Engineering 512 By object


Download presentation

delegation MenuItem Command c Clicked() Command CommandA
Execute() CommandB Execute() c->Execute() Action1() Action2() Menu
Abstract framework
We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
119 Department of Information
system. Engineering
Share buttons 513bit
are a little Command object
lower. Thank you!
Put a Command object inside a MenuItem object –
Buttons:
aMenuItem.StoreCommand(Command c) –If MenuItem is clicked,
c.Execute() is called A di erence between inheritance and delegation –
Inheritance binded MenuItem with the action –Delegation decouples
the two The decoupling leads to a better reuse of the Command object
(it can be used by more than one MenuItem ) 2

120 Department of Information Engineering 514 How to support


Undo /Redo ? Each command object supports Execute() and Cancel Download
UnExecute() –Clone a command object –Put the cloned object in a
command history list Redo => clonedObject.UnExecute() present past
commands

121 Department of Information Engineering 515 Command Intent


–Encapsulate a request as an object Applications –Each MenuItem has
a command object e.g. new MenuItem(new IEG3080()) –The.NET’s
delegate is an example of command

122 Department of Information Engineering 516 Problem To


check whether a student can register to a course –need to check the
course, department, faculty, and university constraints Example of
constraints –credit exceeded, pre-requisites not met Problem –Which
object is responsible in doing the checking?

123 Department of Information Engineering 517 Chain of


Responsibility A simple solution –Pass the request along a chain of
responsibility –Each object on the chain shares a common interface It is
like getting a form signed but you don’t know who should sign it –So
pass the form around until someone could handle it

124 Department of Information Engineering 518 Chain of


Responsibility Often applied in conjunction with Composite (i.e. tree
structure) client aHandler IEHandler successor ErgHandler successor
UniHandler successor SciHandler successor CSHandler successor

125 Department of Information Engineering 519 Chain of


Responsibility Handler Request() ConcreteHanlder1 Request()
ConcreteHanlder1 Request() successor client

126 Department of Information Engineering 520 Chain of


Responsibility Intent –More than one object may handle a request, and
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
the handler is not known –You want to issue a request to one of several
policy. I agree.      /
objects without specifying the receiver explicitly The structure is similar
to a composite pattern –But composite pattern is focused on the
structure of the objects –Chain of Responsibility is focused on the
behavior of the objects 
Download presentation

127 Department of Information Engineering 521 Problem –


multicast relationship
WeHow toyou
think de have
ne a one-to-many dependency
liked this presentation. If you wish to
between objects so that when one
download objectrecommend
it, please changes state,it toall its friends in any social
your
dependents are notisystem.
ed andShare
updated automatically
buttons are a littlee.g.
bit10:00am
lower. ThankHK you!
time 2:00am London time 10:00pm New York time Central timer
Central Timer changes state Notify all other clocks
Buttons:

128 Department of Information Engineering 522 Observer Pattern


Key abstraction –Subject (e.g. central timer) –Observers (e.g. di erent
clock) Whenever the Subject changes its state, it noti es (Update()) all
2
the Observers Each Observer responds by getting the up-to- date
information (GetState()) from the Subject A new Observer can a ttach
itself to the container of the Subject Cancel Download

129 Department of Information Engineering 523 Observer Pattern


Subject Attach(Observer) Detach(Observer) Notify() observers Observer
Update(subject) ConcreteSubject GetState();... ConcreteObserver
Update(subject) Foreach o in observers { o.Update(this); }
observerState = subject.GetState(); 1 *

130 Department of Information Engineering 524 The multicast


protocol Subject has a list of Observers An Observer uses Attach() and
Detach() to add itself to Subject’s list Subject uses Update() to send
noti cation to Observers –Pass itself to Observers as parameters –
Observers use the passed Subject to get the new state information

131 Department of Information Engineering 525 GUI problem


User uses a mouse to click a button on the screen Window manage
detects the mouse click event But how could the manager notify the
button handler? Mouse click MyButtonHandler Update() Window
manager Button ?

132 Department of Information Engineering 526 Class inheritance


Used by early version of MFC and Java (ver 1.0) Mouse click
MyButtonHandler Update() Window manager Button Update()

133 Department of Information Engineering 527 Class inheritance


User clicks Button, the manager uses the current mouse position to
locate Button Manager calls Button.Update() MyButtonHandler inherits
the Button interface, MyButtonHandler.Update() is called Limitation –
Do not support multicast –Static binding, relationship is xed at
compile time, cannot attach or detach at run time

134 Department of Information Engineering 528 Object delegation


Used by current version of Java (the name Listener is used instead of
Observer)
To make Advantages
this website work, we log–Support multicast
user data and andprocessors.
share it with dynamicTo binding
use this (so thatyou must agree to our Privacy Policy, including cookie
website,
policy. I agree.      /
user can attach and detach observer at run time) Mouse click Window
manager Button Update() Listener[] list MyButtonHanlder Update()
Observer Update()

Download presentation
135 Department of Information Engineering 529 Further
improvement on the Observer pattern Problem with Observer –Every
event handler must inherit the Observer base class (which provides the
We think you have liked this presentation. If you wish to
Update() interface) –The Subject passes itself as argument to the
download it, please recommend it to your friends in any social
Observer How to specify the type of the Subject exibly? Delegate –An
system. Share buttons are a little bit lower. Thank you!
improved version of Observer –Use a Command object to encapsulate
a pointer to the handler, eliminate Update() and the base class
Buttons:
Observer

136 Department of Information Engineering 530 Callback by


delegation (in L3 notes) class MyWindow : Form { static void Main() { 2
Application.Run(new MyWindow()); } public MyWindow() { this.MouseUp
+= new EventHandler(this.Handler()); } //MouseUp is a container private
void Handler(object sender,System.EventArgs e){ Cancel Download
Console.WriteLine(“call from {0}”,sender.ToString()); } function pointer
Handler function Command object

137 Department of Information Engineering 531 C# delegate User


can also de ne a delegate with signature –More exible, can pass type-
safe information –public delegate void MyHandler(Student s); Delegate
= Command + Observer pattern Window manager Button Update()
Listener[] list MyHandler Handler() Command C# delegate object
Command has a pointer to Handler()

138 Department of Information Engineering 532 Mediator


Problem –OO encourages distribution of responsibility, but this
frequently leads to many connections between objects –The system is
di cult to change because each object depends on many other objects
139 Department of Information Engineering 533 Mediator
Solution –Promotes loose coupling between objects via a central
mediator mediator

140 Department of Information Engineering 534 Mediator Intent –


De ne an object that encapsulates how a set of objects interact –
Mediator promotes loose coupling by keeping objects from referring to
each other explicitly, and it lets you vary their interaction independently
–Replace many-to-many interactions with one- to-many interactions –If
an object is changed, only need to change the Mediator, but not the
rest of the objects

141 Department of Information Engineering 535 Example – dialog


box A dialog box has a number of widgets –Buttons, menus, and entry
elds

142 Department of Information Engineering 536 Dependencies


between widgets Select an option in one widget causes changes in
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
other widgets

143 Department of Information Engineering 537 Without


mediator –If a widget is changed,presentation
need to notify other widgets –Creates 
Download
dependency between widgets, tight coupling, makes future change
di cult With mediator –If a widget is changed, only need to notify the
Mediator –Loose coupling,
We thinklocalized
you havechange
liked –The Mediator centralizes
this presentation. If you wish to
control, to change the behaviorit,only
download requires
please subclassing
recommend the Mediator
it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
144 Department of Information Engineering 538 Mediator
Buttons:
145 Department of Information Engineering 539 How to test your
code A common generic pattern in testing –Set up the environment –
Do some work –Tear down TestCase() { SetUp(); while ( HasMore() ) {
2
Run(); } TearDown(); }

146 Department of Information Engineering 540 Template


Method We have a xed sequence of steps which are invariant,Cancel
though Download
the implementation of each step may change We may carry out
di erent testing, but the sequence remains the same Template pattern
encapsulates the sequence of business logic

147 Department of Information Engineering 541 Template


Method Base class de nes the template of an algorithm, deferring the
implementation to subclasses Subclasses rede ne certain steps of an
algorithm without changing the algorithm’s structure TestCase() {
SetUp();... } SetUp() {... }

148 Department of Information Engineering 542 Note The


template method TestCase() is non- virtual –Cannot be overrided by the
subclass –So that the xed sequence cannot be changed Methods
inside the template are protected –Intended to be overrided by the
subclasses, but are not opened to public

149 Department of Information Engineering 543 Template


Method We have seen it before in creating a maze Maze
MazeGame.CreateMaze() { Maze aMaze = makeMaze(); Room r1 =
makeRoom(1); Room r2 = makeRoom(2); Door theDoor = makeDoor(r1,
r2); aMaze.addRoom(r1); aMaze.addRoom(r2);... } A template of
creating a maze

150 Department of Information Engineering 544 TCP


(transmission control protocol) implementation Problem –How to
implement TCP? TCP is complex !! –A connection can be in one of the
many states e.g. Listen, Established, SynSent –A connection may receive
di erent requests e.g. active open, close, acknowledge –Di erent state
responds to the request di erently

151 Department of Information Engineering 545 TCP state


diagram
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
152 Department of Information Engineering 546 State Listen,
Established, Closed is-a kind of State TCPState SynSent
EstablishedClosed

Download presentation
153 Department of Information Engineering 547 State Each
TCPConnection has-a State TCPConnection TCPState _state TCPState
SynSent EstablishedClosed
We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
154 system.
Department Share buttons
of Information are a little
Engineering 548bit lower. Thank you!
State
TCPConnection may receive many requests –e.g. connect, close,
Buttons:
acknowledge TCPConnection Connect() Close() Acknowledge() TCPState
_state TCPState SynSent EstablishedClosed

155 Department of Information Engineering 549 Should 2


TCPConnection handle the requests? No, requests are state-dependent
Delegate the requests to the TCPState objects TCPConnection
Connect()... TCPState _state TCPState Connect()... SynSent Connect()...
Cancel Download
Closed Connect()...

156 Department of Information Engineering 550 How to handle a


request? TCPConnection receives a “connect” request –TCPConnection
object delegates the request to _ state (the current TCPState object) –
No ugly if-then-else block

157 Department of Information Engineering 551 How to handle a


request? class TCPConnection { private TCPState _state;//current state
void Connect() { _state.Connect(this); //delegate } public void
ChangeState(TCPState nextState) { _state = nextState; }

158 Department of Information Engineering 552 To change the


state of TCPConnection The concrete object TCPState does the work,
then use ChangeState() to change _state in TCPConnection to next state
class TCPClosed : TCPState { void ActiveOpen(TCPConnection t) { //...
send SYN t.ChangeState(TCPSynSent.Instance); } Static property
returning a Singleton of TCPSynSent

159 Department of Information Engineering 553 State For each


concrete TCPState, only implement the functions that is meaningful –
Meaningless requests can be ignored –Base class TCPState provides
the dummy default A do-nothing dummy default public override void
TCPState.Connect(TCPConnection t){}

160 Department of Information Engineering 554 Singleton None


of the TCPState subclass has local state variables ! If the objects are
stateless (have no memory) –Only need one object for each TCPState is
enough –Singleton –Singleton object can be accessed directly

161 Department of Information Engineering 555 Singleton socket


TCPConnection socket TCPConnection socket TCPConnection TCPState
TCPListen TCPClosed... Singleton, shared by all connections
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
162 Department of Information Engineering 556 Problem How to
encapsulate an algorithm so that your software can select between
di erent algorithms exibly? e.g. –In word processing, choose between
the di erent output Download
formatter (pdf, postscript, rtf) –In action game, 
presentation
ghters can have di erent algorithm for di erent ghting level

163 DepartmentWeofthink
Information
you have Engineering 557 Solution Ifbyyou wish to
liked this presentation.
subclassing But subclassing
download makes it di recommend
it, please cult for an object to change
it to your friends in any social
the algorithm at runsystem.
time The algorithm
Share buttons is statically binded
are a little to the
bit lower. Thank you!
concrete object Base Class void Algo(); Derived Class A void Algo();
Derived Class B voidButtons:
Algo();

164 Department of Information Engineering 558 Strategy Object


delegation –the base class object is free to change the strategy object2
at run time Base Strategy _s; void Algo() {_s->Algo();} Strategy void
Algo(); Derived class A void Algo(); Derived class B void Algo();

165 Department of Information Engineering 559 State andCancel Download

Strategy Same class diagram, same pattern of code ! –Di ered by their
intent and dynamics State’s intent –Allow an object to alter its behavior
when its internal state changes Strategy’s intent –De ne a family of
algorithm, encapsulate each one, and make them interchangeable

166 Department of Information Engineering 560 Problem How to


add new functionality to a system? –e.g to add a print option to the
registration system Solution –Add Print() to the base class Node
Search() Print() NodeList Search() Print()... Course Search() Print()...

167 Department of Information Engineering 561 Problem Modify


the base class, violate the open-close principle! Solution –Visitor Pattern
–Visitor lets you de ne a new operation without changing the classes of
the elements on which it operates

168 Department of Information Engineering 562 How it works


Accept() is a general-purpose method that can accept a Visitor object
Node Accept(Visitor v) NodeList Accept(Visitor v) Node[] _list Course
Accept(Visitor v)

169 Department of Information Engineering 563 Visitor Pattern A


node accepts a visitor object –Node.Accept(Visitor v); Node.Accept()
immediately returns the call to Visitor and pass itself as the argument –
v.Visit(this); //this=the current node Visitor.Visit(Node n) receives the
node and does something about it –e.g. print the node, search the
node

170 Department of Information Engineering 564 Code example If


the concrete node is a Course object Course.Accept(Visitor v) {
v.Visit(this);//callback } If the concrete node is a NodeList object
NodeList.Accept(Visitor v) { v.Visit(this); //process the list foreach (Node
n in _list) { n.Accept(v); //call each node in list }
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
171 Department of Information Engineering 565 Node
Accept(Visitor v) NodeList Course Visitor Visit(Node n) SearchVisitor
Visit(Node n) //doSomething PrintVisitor Visit(Node n) //doSomething
client 
Download presentation

172 Department of Information Engineering 566 Note Node


usually has a tree (composite) structure
We think you –Thethis
have liked visitor object transverses
presentation. If you wish to
all nodes in the tree download
–Each nodeit, callbacks to the visitor
please recommend ; visitor
it to your friends in any social
provides the new functionality To add
system. Share new are
buttons functionality
a little bit–simply add a you!
lower. Thank
new subclass of visitor, –no need to modify the object structure ( Node
) Buttons:

173 Department of Information Engineering 567 Visitor Pattern


Re nement –Both Course and NodeList make a callback to the same 2
visitor(Node n) method –Sometime we would like to treat callback from
Course and NodeList di erently, –e.g. PrintCourseName visitor If
callback from a Course node, print the course name If callback from a
NodeList, do nothing Cancel Download

174 Department of Information Engineering 568 Visitor Pattern


Solution by function overloading –Overload the callback function Visit(),
so as to treat Course and NodeList di erently class Visitor { public void
Visit(Course n) {//...} public void Visit(NodeList n){//...} }

175 Department of Information Engineering 569 Code example If


the concrete node is a Course object Course.Accept(Visitor v) {
v.Visit(this);//call Visit(Course n) } If the concrete node is a NodeList
object NodeList.Accept(Visitor v) { v.Visit(this); //call Visit(NodeList n)... }

176 Department of Information Engineering 570 Double dispatch


What we have just seen is an example of double dispatch implemented
using the Visitor pattern Single dispatch – Call a function based on Type
of one receiver (the object) Name of the function –e.g. obj.Foo() Type of
object is known Function name

177 Department of Information Engineering 571 Double dispatch


Call a function based on –Type of two receivers –Name of the function –
e.g. obj.Foo(obj2) C# and Java support only single dispatch –
Polymorphism is limited to the type of one object Type of two receivers
of polymorphic type

178 Department of Information Engineering 572 Example of


double dispatch Shape class –Base class: Shape –Sub-class: Rectangle,
Circle A function to check whether two Shape objects intersect –class
Shape { bool virtual Intersect(Shape s) {...} }

179 Department of Information Engineering 573 Why single


dispatch is not enough Shape s1 = new Rectangle(); Shape s2 = new
Circle(); bool ag = s1.Intersect(s2); What we want –To invoke
Rectangle.Intersect(Circle s) What we get –Invoke
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
Rectangle.Intersect(Shape s) –Intersect(Circle s) is not called because s2
policy. I agree.      /
is upcasted to Shape Problem –Need to nd out the type of s

180 Department of Information Engineering 574 Solution 1: if-


then-else block public class Rectangle : Shape { public bool 
Download presentation
Intersect(Shape s) { //if s is a rectangle, call Intersect(Rectangle s) //else
if s is a circle, call Intersect(Circle s) } public bool Intersect(Circle s) {...} }
If-then-else block is a bad thing in OO, because your code knows too
We think you have liked this presentation. If you wish to
much about the Shape Languages that support double dispatch can
download it, please recommend it to your friends in any social
check the type of s2 dynamically and call Intersect(Circle s), but C# (and
system. Share buttons are a little bit lower. Thank you!
Java) only supports single dispatch
Buttons:
181 Department of Information Engineering 575 Solution 2: Visitor
class Shape { public bool virtual Intersect(Shape s); } class Rectangle :
Shape { public bool override Intersect(Shape s) {...}... } s1.Intersect(s2) –
s1 is a rectangle, Rectangle.Intersect(Shape) is called – 2
Rectangle.Intersect(Shape s) { s.Intersect(this); //visitor callback } –s is a
Circle, this is a Rectangle (function overloading),
Circle.Intersect(Rectangle) is called Cancel Download

182 Department of Information Engineering 576 Visitor pattern


Shape Intersect(Shape s); Rectangle Intersect(Shape s);
Intersect(Rectangle s); Intersect(Circle s); Circle Intersect(Shape s);
Intersect(Rectangle s); Intersect(Circle s); return s.Intersect(this)
s1.Intersect(s2)

183 Department of Information Engineering 577 Multiple dispatch


Can extend the idea to triple or quadruple dispatch; this is known as
multiple dispatch There are languages that support multiple dispatch –
CLOS, MultiJava Also known as multiple polymorphism

184 Department of Information Engineering 578 Inheritance


versus delegation Inheritance –separation of interface and
implementation –abstract programming depends on it But Design
Pattern favors object composition or delegation over class inheritance
Why delegate? –Class should have a well de ned responsibility –If a
class has too many responsibilities, then we should move some
responsibilities to another classes –The simpli cation reduces the size
of the class, making it easier to maintain and to reuse

185 Department of Information Engineering 579 An example from


a newsgroup public class HashTable { //... public void
addManyElements(Element[] e) { //... } public void addElement(Element
e) { //... } } How to have a HashTable that counts the number of its
elements?

186 Department of Information Engineering 580 Solution by


inheritance public class CountedHashTable : HashTable { //... private int
count; public void addManyElements(Element[] e) { base(e); count =
count + e.length; } public void addElement(Element e) { base(e); count =
count + 1; } } Later it is found that the counter double counted when
addManyElements()
To make this website work, weis
logcalled, why?
user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
187 Department of Information Engineering 581 Solution by
inheritance This problem is caused by the implementation of
addManyElements() in HashTable public void
addManyElements(Element[] e) {presentation
for (int i = 0; i < e.length; i++) { 
Download
this.addElement(e[i]); } } This example shows that it is impossible for
subclasses to inherit from a class without knowing at least part of its
implementation, Therefore
We think inheritance breaks
you have liked thisencapsulation
presentation.!!If you wish to
download it, please recommend it to your friends in any social
188 Department of Information
system. Engineering
Share buttons 582bit
are a little Solution
lower.by
Thank you!
delegation / composition A better solution is to use interface and
Buttons: IHash { public
composition public interface
addManyElements(Element[] e); public addElement(Element e); } public
class HashTable : IHash { //... public void addManyElements(Element[]
e) { //... } public void addElement(Element e) { //... } }
2

189 Department of Information Engineering 583 Solution by


delegation / composition public class CountedHashTable : IHash {
private HashTable hashTable; //has-a private int count; public Cancel Download
addManyElements(Element[] e) { hashTable.addManyElements(e);
//delegate count = count + e.Length; } public addElement(e) {
hashTable.addElement(e); count = count + 1; } }

190 Department of Information Engineering 584 Inheritance vs


delegation The example shows that –Inheritance is White-Box reuse –
Delegation is Black-Box reuse White box reuse –To reuse it, you need to
open the box and know details about the code –Break encapsulation
Black box reuse –Closer to our understanding of reusing a component

191 Department of Information Engineering 585 Inheritance vs


delegation What the example tells us –Avoid using inheritance for
implementation reuse –To correctly reuse an implementation by
inheritance, needs to examine the super class –Nobody wants to do
this in the industry! –Much safer to reuse implementation by delegation
(black box reuse)

192 Department of Information Engineering 586 Inheritance vs


delegation So what is the role of inheritance? –To provide a
generalization of many concrete classes –E.g. USB and concrete devices
From the point of view of the concrete devices –They inheritance the
interface (of USB), not the implementation Common sense, right?

193 Department of Information Engineering 587 Case study A


multi-players network game –Each player has a ghter object –Player
sees through the eyes of the ghter, so that each player has a di erent
view How to write the game? –No need to reinvent the wheel, standard
solution exists

194 Department of Information Engineering 588 The nature of the


problem –Multiple views, common data A classic problem –A document
with di erent views (word doc, bar chart); if document is changed, all
views
To make thisare updated
website –Alog
work, we trading room
user data withitmany
and share terminals,
with processors. each
To use thishas a you must agree to our Privacy Policy, including cookie
website,
policy. I agree.      /
di erent presentation of the same data A classic solution –MVC pattern
–www.wikipedia.comwww.wikipedia.com

195 Department of Information Engineering 589 MVC pattern


Download presentation

Decompose an application into three parts –Controller (input handler) –
View (output handler, the presentation) –Model (internal data and
processing) View Controller
We thinkModel event
you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
196 Department of Information
system. Engineering
Share buttons 590bit
are a little MVC model
lower. Input
Thank you!
is managed by the Controller Output is managed by the View State is
Buttons:
managed by the Model The MOST important pattern in software
architecture –Decouple the input, output, and the data, so that each
part can be changed independent from others

2
197 Department of Information Engineering 591 Patterns in MVC
Controller –Strategy pattern –Take care of the UI, di erent UI uses
di erent controller Model-View –Observer pattern –model is the
subject, views are the observers, View –Composite pattern –a Cancel Download
CompositeView may contain more Views

198 Department of Information Engineering 592 3 main patterns –


Strategy, Observer, Composite user controller view model 2. change 4.
get state 1.event5.draw 3.notify StrategyComposite Observer

199 Department of Information Engineering 593 Class diagram


Model Observers[] o Observer Update(subject) View Controller 1 * 1
Subject Attach(observer) Detach(observer) Notify() Strategy Observer
Composite

200 Department of Information Engineering 594 Structure of a


ghting game Model has a container for all objects –Objects can be
ghters, bullets, or simply the background –Responsible for collision
detection The Model object noti es the View objects whenever it is
changed –In simple game, like our project, there is only one view, so
each object ( ghter, bullet) can have-a view object directly –Complex
game, use Observer pattern

201 Department of Information Engineering 595 Scenario – Fire a


bullet If a player res a shot, the controller handles this event
Controller changes the state of the Model by adding a new bullet object
to Model’s container –Model checks for collision –If bullet hits an object,
the object’s state is updated Model then noti es the View objects that
the state has been changed –Each View gets its state from Model and
redraws itself

202 Department of Information Engineering 596 Responsibility


Suppose a bullet hit an object (e.g. a ghter), –Let’s say the bullet
explodes and some blood is spilled Who has the responsibility to
explode and make some noise? –Bullet Who has the responsibility to
spill blood? –Fighter has the responsibility to know how many cc of
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
blood and in what color that should be spilt

203 Department of Information Engineering 597 Responsibility


The most importantDownload
role of a software designer is to nd the right 
presentation
objects and to assign suitable responsibility to the objects e.g. CRC
Cards
We think you have liked this presentation. If you wish to
204 Department of Information
download Engineering
it, please recommend 598itAtofew words
your on in any social
friends
your project The MODEL is simply
system. Share abuttons
smart container
are a little–The container
bit lower. Thank you!
only knows abstract game objects, not the concrete subtypes (e.g.
Buttons:
arrows and balls) –Main responsibility is COLLISION DETECTION
container

205 Department of Information Engineering 599 Collision


2
detection Each game object has-a rectangle –Two objects are collided if
their rectangles intersect with each other –Easy to code ! Use the C#
Rectangle class, which has the intersectWith() method Even better, each
Cancel
game object has-a Shape –And use Shape.intersectWith() –The concrete Download
object can have any kind of Shape object, which can be changed at run
time

206 Department of Information Engineering 600 What to do with


the collided objects? Who should hangle the collision? –NOT the
responsibility of the container –Handled by the game objects,
distributed responsibility Example –If a GameObject obj is hit, calls
obj.collide() –The collision is handled by the concrete object –If obj is-a
Arrow, Arrow.collide() do nothing –If obj is-a Ball, Ball.collide() breaks
into smaller balls

207 Department of Information Engineering 601 To update the


motion of the objects Use a real-time clock –Generates interrupts, say,
every 0.1 second to the container –The container asks each of the game
objects to update themselves obj.update() Each concrete object knows
its motion vector, so it can calculate the new position, and draw itself –
Don’t overload the container

208 Department of Information Engineering 602 How to draw the


objects? In this simple game, each game object can have a draw(),
which knows how to draw itself –But what if we use a di erent display
system, the game object has to be modi ed For complex system, better
use the observer pattern –The display is handled by the view objects –
The game objects in the container callback to the view objects, notifying
them that something is changed –View objects get data from model
(that has the game objects), and redraw themselves –Decouple the
presentation and the model

209 Department of Information Engineering 603 GameObject


intersect() collide() update() draw() Arrow Ball Shape intersect()
Rectangle Circle Container detectCollision() update() draw() Abstract
Game Engine Controller keyInput() ConcreteController To have a
di erent
To make game,
this website change
work, we log the
user concrete objects
data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /
Download ppt "Department of Information Engineering 395 Design
patterns What is a pattern? –Pattern describes a problem which occurs 
Download presentation
over and over again in our environment,"

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
 Similar presentations
system. Share buttons are a little bit lower. Thank you!

Buttons:

Cancel Download

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /

Download presentation

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

Buttons:

Cancel Download

© 2020 SlidePlayer.com Inc. Feedback Do Not Sell About project


All rights reserved.
Privacy Policy My Personal SlidePlayer
Feedback Information Terms of Service

Search... Search

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /

Download presentation

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

Buttons:

Cancel Download

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.      /

You might also like