Design Patterns

58
Introduction Summary References DESIGN PATTERNS Muhammad Adil Raja Roaming Researchers, Inc. cbna January 29, 2017

Transcript of Design Patterns

Introduction Summary References

DESIGN PATTERNS

Muhammad Adil Raja

Roaming Researchers, Inc.

cbna

January 29, 2017

Introduction Summary References

OUTLINE I

1 INTRODUCTION

2 SUMMARY

3 REFERENCES

Introduction Summary References

INTRODUCTION

When faced with a new problem, where do you look forinspiration?Most people look to solutions of previous problems thathave similar characteristics.This insight is what is behind design patterns; collectionsof proven ways to structure the relationships betweenobjects in the pursuit of a given objective.

Introduction Summary References

INSPIRATION

Design patterns speed the process of finding a solution, byeliminating the need to reinvent well-known and provensolutions.Just as important, design patterns provide a vocabularywith which to discuss the space of possible design choices.This vocabulary is termed a pattern language.

Introduction Summary References

TYPES OF DESIGN PATTERNS

Creational: Patterns provide instantiation mechanisms,making it easier to create objects in a way that suits thesituation.Structural: Patterns generally deal with relationshipsbetween entities, making it easier for these entities to worktogether.Behavioral: Patterns are used in communicationsbetween entities and make it easier and more flexible forthese entities to communicate.

Introduction Summary References

BENEFITS OF USING DESIGN PATTERNS

Analogy from civil engineering.Question: What will a civil engineer do when confrontedwith a problem to cross a river?Answer: Build a tunnel or a bridge.Question: How about preventing rain from entering awindow?Answer: Build a canopy.

They give the developer a selection of tried and testedsolutions to work with.They are language neutral and so can be applied to anylanguage that supports object-orientation.They aid communication by the very fact that they are welldocumented and can be researched if that is not the case.They have a proven track record as they are already widelyused and thus reduce the technical risk to the project.They are highly flexible and can be used in practically anytype of application or domain.They can be used to interconnect various parts of anapplication.They provide an easy way to integrate various applicationstogether.

Introduction Summary References

RELATIONSHIP TO DEPENDENCY AND VISIBILITY

Many patterns are involved with the concepts ofdependency we introduced in the previous chapter.Determining where strong dependencies are necessary,and how to weaken dependencies whenever possible.

Introduction Summary References

A SIMPLE EXAMPLE, THE ADAPTER

An adaptor is used to connect a client (an object thatneeds a service) with a server (an object that provides theservice).The client requires a certain interface, and while the serverprovides the necessary functionality, it does not supportthe interface.The adapter changes the interface, without actually doingthe work.

Introduction Summary References

AN EXAMPLE ADAPTER

EXAMPLE

class MyCol lec t ion implements C o l l e c t i o n {

public boolean isEmpty ( ){ return data . count ( ) == 0; }

public i n t s ize ( ){ return data . count ( ) ; }

public void addElement ( Object newElement ){ data . add ( newElement ) ; }

public boolean containsElement ( Object t e s t ){ return data . f i n d ( t e s t ) != n u l l ; }

public Object f indElement ( Object t e s t ){ return data . f i n d ( t e s t ) ; }

private DataBox data = new DataBox ( ) ;}

ExampleDataBox is some collection that does not support theCollection interface.Adapters are often needed to connect software fromdifferent vendors.

Introduction Summary References

DESCRIBING PATTERNS

Patterns themselves have developed their own vocabulary fordescription:

NAME: Contributes to the pattern vocabulary.SYNOPSIS: Short description of the problem the pattern will

solve.FORCES: Requirements, considerations, or necessary

conditionsSOLUTION: The essense of the solution

COUNTERFORCES: Reasons for not using the pattern.RELATED PATTERNS: Possible alternatives in the design.

Introduction Summary References

EXAMPLE PATTERNS

We will briefly examine a number of common patterns:Iterator.Software Factory.Strategy.Singleton.Composite.Decorator.Double-Dispatch.Flyweight.Proxy.Facade.Observer.

Introduction Summary References

ITERATOR

Problem: How do you provide a client access to elementsin a collection, without exposing the structure of thecollection.Solution: Allow clients to manipulate an object that canreturn the current value and move to the next element inthe collection.Example, Enumerators in Java.

EXAMPLE

in ter face Enumerator {public boolean hasMoreElements ( ) ;public Object nextElement ( ) ;

}

Enumeator e = . . . ;while ( e . hasMoreElements ) {

Object va l = e . nextElement ( ) ;. . .

}

The pattern applies, even if the interface is changed.

Introduction Summary References

SOFTWARE FACTORYINTRODUCTION

Problem: How do you simplify the manipulation of manydifferent implementations of the same interface (i.e.,iterators).Solution: Hide creation within a method, have the methoddeclare a return type that is more general than its actualreturn type.

EXAMPLE

class Sor tedL i s t {. . .Enumerator elements ( ) { return new SortedLis tEnumerator ( ) ; }. . .private class SortedLis tEnumerator implements Enumerator {

. . .}

}

Introduction Summary References

SOFTWARE FACTORY

The method is the “factory” in the name. Users don’t needto know the exact type the factory returns, only thedeclared type.The factory could even return different types, dependingupon circumstances.

Introduction Summary References

SOFTWARE FACTORYANOTHER EXAMPLE

FIGURE: Factory

Introduction Summary References

SOFTWARE FACTORYSTEP 1: CREATE AN INTERFACE.

SHAPE

public inter face Shape {void draw ( ) ;

}

Introduction Summary References

SOFTWARE FACTORYSTEP 2: CREATE CONCRETE CLASSES IMPLEMENTING THE SAME INTERFACE.

RECTANGLE

public class Rectangle implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide Rectangle : : draw ( ) method . " ) ;}

}

SQUARE

public class Square implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide Square : : draw ( ) method . " ) ;}

}

Introduction Summary References

SOFTWARE FACTORY

CIRCLE

public class C i r c l e implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide C i r c l e : : draw ( ) method . " ) ;}

}

Introduction Summary References

SOFTWARE FACTORYSTEP 3: CREATE A FACTORY TO GENERATE OBJECT OF CONCRETE CLASS BASED ON GIVEN

INFORMATION.

SHAPEFACTORY

public class ShapeFactory {

/ / use getShape method to get ob jec t o f type shapepublic Shape getShape ( S t r i n g shapeType ) {

i f ( shapeType == nul l ) {return nul l ;

}i f ( shapeType . equalsIgnoreCase ( "CIRCLE" ) ) {

return new C i r c l e ( ) ;

} else i f ( shapeType . equalsIgnoreCase ( "RECTANGLE" ) ) {return new Rectangle ( ) ;

} else i f ( shapeType . equalsIgnoreCase ( "SQUARE" ) ) {return new Square ( ) ;

}

return nul l ;}

}

Introduction Summary References

SOFTWARE FACTORYSTEP 4: USE THE FACTORY TO GET OBJECT OF CONCRETE CLASS BY PASSING AN

INFORMATION SUCH AS TYPE.

FACTORY PATTERN DEMO

public class FactoryPatternDemo {

public s t a t i c void main ( S t r i n g [ ] args ) {ShapeFactory shapeFactory = new ShapeFactory ( ) ;

/ / get an ob jec t o f C i r c l e and c a l l i t s draw method .Shape shape1 = shapeFactory . getShape ( "CIRCLE" ) ;

/ / c a l l draw method of C i r c l eshape1 . draw ( ) ;

/ / get an ob jec t o f Rectangle and c a l l i t s draw method .Shape shape2 = shapeFactory . getShape ( "RECTANGLE" ) ;

/ / c a l l draw method of Rectangleshape2 . draw ( ) ;

/ / get an ob jec t o f Square and c a l l i t s draw method .Shape shape3 = shapeFactory . getShape ( "SQUARE" ) ;

/ / c a l l draw method of c i r c l eshape3 . draw ( ) ;

}}

Introduction Summary References

SOFTWARE FACTORYSTEP 5: VERIFY THE OUTPUT.

FACTORY PATTERN DEMO

Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.

Introduction Summary References

STRATEGY

Problem: Allow the client the choice of many alternatives,but each is complex, and you don’t want to include code forall.Solution: Make many implementations of the sameinterface, and allow the client to select one and give it backto you.Example: The layout managers in the AWT.Several different layout managers are implemented, andthe designer selects and creates one.Gives the designer flexibility, keeps the code size down.

Introduction Summary References

STRATEGYAN EXAMPLE

FIGURE: Strategy

Introduction Summary References

STRATEGYSTEP 1: CREATE AN INTERFACE.

STRATEGY

public inter face Stra tegy {public i n t doOperation ( i n t num1, i n t num2 ) ;

}

Introduction Summary References

STRATEGYSTEP 2: CREATE CONCRETE CLASSES IMPLEMENTING THE SAME INTERFACE.

OPERATION ADD

public class OperationAdd implements Stra tegy {@Overridepublic i n t doOperation ( i n t num1, i n t num2) {

return num1 + num2 ;}

}

OPERATION SUBTRACT

public class Operat ionSubst rac t implements Stra tegy {@Overridepublic i n t doOperation ( i n t num1, i n t num2) {

return num1 − num2 ;}

}

Introduction Summary References

STRATEGY

OPERATION MULTIPLY

public class Opera t i onMu l t i p l y implements Stra tegy {@Overridepublic i n t doOperation ( i n t num1, i n t num2) {

return num1 ∗ num2 ;}

}

Introduction Summary References

STRATEGYSTEP 3: CREATE CONTEXT CLASS.

CONTEXT

public class Context {private Stra tegy s t r a tegy ;

public Context ( S t ra tegy s t r a te gy ) {th is . s t r a t egy = s t ra tegy ;

}

public i n t executeStrategy ( i n t num1, i n t num2 ) {return s t r a t egy . doOperation (num1, num2 ) ;

}}

Introduction Summary References

STRATEGYSTEP 4: USE THE CONTEXT TO SEE CHANGE IN BEHAVIOUR WHEN IT CHANGES ITS

STRATEGY.

STRATEGY PATTERN DEMO

public class StrategyPatternDemo {public s t a t i c void main ( S t r i n g [ ] args ) {

Context con tex t = new Context (new OperationAdd ( ) ) ;System . out . p r i n t l n ( " 10 + 5 = " + contex t . executeStrategy (10 , 5 ) ) ;

con tex t = new Context (new Operat ionSubst rac t ( ) ) ;System . out . p r i n t l n ( " 10 − 5 = " + contex t . executeStrategy (10 , 5 ) ) ;

con tex t = new Context (new Opera t i onMu l t i p l y ( ) ) ;System . out . p r i n t l n ( " 10 ∗ 5 = " + contex t . executeStrategy (10 , 5 ) ) ;

}}

Introduction Summary References

STRATEGYSTEP 5: VERIFY THE OUTPUT.

FACTORY PATTERN DEMO

10 + 5 = 1510 - 5 = 510 * 5 = 50

Introduction Summary References

SINGLETON

Problem: You want to ensure that there is never more thanone instace of a given class.Solution: Make the constructor private, have a method thatreturns just one instance, which is held inside the classitself.

EXAMPLE

class Sing le tonClass {public :

s t a t i c Sing le tonClass ∗ oneAndOnly ( ) { return theOne ; }private :

s t a t i c Sing le tonClass ∗ theOne ;Sing le tonClass ( ) { . . . }

} ;

/ / s t a t i c i n i t i a l i z a t i o nSing le tonClass ∗ Sing le tonClass : : theOne = new Sing le tonClass ( ) ;

Introduction Summary References

COMPOSITE

Problem: How do you facilitate creation of complexsystems from simple parts?Solution: Provide a few simple components, and a systemto compose components (simple or otherwise) into newcomponents.Regular expressions are an example, are type systems, orthe nesting of panels within panels in the Java AWT API.

Introduction Summary References

COMPOSITEAN EXAMPLE

FIGURE: Composite

Introduction Summary References

COMPOSITESTEP 1: CREATE EMPLOYEE CLASS HAVING LIST OF EMPLOYEE OBJECTS.

EMPLOYEE

import java . u t i l . A r r a y L i s t ;import java . u t i l . L i s t ;

public class Employee {private S t r i n g name ;private S t r i n g dept ;private i n t sa la ry ;private L i s t <Employee> subord inates ;

/ / cons t r uc to rpublic Employee ( S t r i n g name, S t r i n g dept , i n t sa l ) {

th is . name = name ;th is . dept = dept ;th is . sa la r y = sa l ;subord inates = new Ar rayL i s t <Employee > ( ) ;

}

public void add ( Employee e ) {subord inates . add ( e ) ;

}

public void remove ( Employee e ) {subord inates . remove ( e ) ;

}

public L i s t <Employee> getSubordinates ( ) {return subord inates ;

}

public S t r i n g t o S t r i n g ( ) {return ( " Employee : [ Name : " + name + " , dept : " + dept + " , sa la ry : " + sa la ry + " ] " ) ;

}}

Introduction Summary References

COMPOSITESTEP 2: USE THE EMPLOYEE CLASS TO CREATE AND PRINT EMPLOYEE HIERARCHY.

COMPOSITE PATTERN DEMO

public class CompositePatternDemo {public s t a t i c void main ( S t r i n g [ ] args ) {

Employee CEO = new Employee ( " John " , "CEO" , 30000);

Employee headSales = new Employee ( " Robert " , "Head Sales " , 20000);

Employee headMarketing = new Employee ( " Michel " , "Head Market ing " , 20000);

Employee c le rk1 = new Employee ( " Laura " , " Market ing " , 10000);Employee c le rk2 = new Employee ( "Bob" , " Market ing " , 10000);

Employee salesExecut ive1 = new Employee ( " Richard " , " Sales " , 10000);Employee salesExecut ive2 = new Employee ( "Rob" , " Sales " , 10000);

CEO. add ( headSales ) ;CEO. add ( headMarketing ) ;

headSales . add ( salesExecut ive1 ) ;headSales . add ( salesExecut ive2 ) ;

headMarketing . add ( c le rk1 ) ;headMarketing . add ( c le rk2 ) ;

/ / p r i n t a l l employees of the o rgan i za t i onSystem . out . p r i n t l n (CEO) ;

for ( Employee headEmployee : CEO. getSubordinates ( ) ) {System . out . p r i n t l n ( headEmployee ) ;

for ( Employee employee : headEmployee . getSubordinates ( ) ) {System . out . p r i n t l n ( employee ) ;

}}

}}

Introduction Summary References

DECORATOR I

Filter, wrapper.

Problem: Allow functionally to be layered around anabstraction, but still dynamically changeable.

Solution: Combine inheritance and composition.

By making an object that both subclasses from anotherclass and holds an instance of the class, can add newbehaviour while referring all other behaviour to the originalclass.

Example Input Streams in the Java I/O System.

Introduction Summary References

DECORATOR II

EXAMPLE

/ / a bu f fe red i npu t stream is−an in pu t streamclass Buf feredInputStream extends InputStream {

public Buf feredInputStream ( InputStream s ) { data = s ; }. . .

/ / and a bu f fe red i np u t stream has−an i npu t streamprivate InputStream data ;

}

An instance of BufferedInputStream can wrap around any othertype of InputStream, and simply adds a little bit newfunctionality.

Introduction Summary References

DOUBLE DISPATCH (MULTIPLE POLYMORPHISM) I

Problem: You have variation in two or more polymorphicvariables.

Solution: Make each a receiver in turn, each message tiesdown one source of variation.

Example, suppose we have a hierarchy of Shapes(Triangle, Square) and Device (Printer, Terminal).

Two variables, one a Shape and one a Device.

First, pass a message to the device, passing the shape asargument:

Introduction Summary References

DOUBLE DISPATCH (MULTIPLE POLYMORPHISM) II

EXAMPLE

Shape aShape = . . . ;Device aDevice = . . . ;

aDevice . d i sp lay ( aShape ) ;

f u n c t i o n P r i n t e r . d i sp lay ( Shape aShape )begin

aShape . d i sp layOnPr in te r ( s e l f ) ;end ;

f u n c t i o n Terminal . d i sp lay ( Shape aShape )begin

aShape . displayOnTerminal ( s e l f ) ;end ;

One message fixes the device, but how to fix the shape?Each subclass of Shape must implement methods for eachoutput device:

Introduction Summary References

DOUBLE DISPATCH (MULTIPLE POLYMORPHISM) III

EXAMPLE

class Tr iang le : public Shape {public :

T r i ang le ( Point , Point , Po in t ) ;/ / . . .

v i r t u a l void d isp layOnPr in te r ( P r i n t e r ) ;v i r t u a l void displayOnTerminal ( Terminal ) ;

/ / . . .private :

Po in t p1 , p2 , p3 ;} ;

void Tr iang le . d i sp layOnPr in te r ( P r i n t e r p ) {/ / p r i n t e r−s p e c i f i c code to/ / d i sp lay t r i a n g l e/ / . . .

}

void Tr iang le . d isplayOnTerminal ( Terminal t ) {/ / te rm ina l−s p e c i f i c code to/ / d i sp lay t r i a n g l e/ / . . .

}

Introduction Summary References

PROXY

Problem: How to hide unimportant communication details,such as a network, from the client.Solution: A proxy uses the interface that the client expects,but passes messages over the network to the server, getsback the response, and passes it to the client.The client is therefore hidden from the network details.

FIGURE: Proxy

Similar in some ways to adaptor, but here the intermediary andthe server can have the same interface.

Introduction Summary References

PROXYAN EXAMPLE

FIGURE: Proxy

Introduction Summary References

PROXYSTEP 1: CREATE AN INTERFACE.

IMAGE

public inter face Image {void d i sp lay ( ) ;

}

Introduction Summary References

PROXYSTEP 2: CREATE CONCRETE CLASSES IMPLEMENTING THE SAME INTERFACE.

REAL IMAGE

public class RealImage implements Image {

private S t r i n g f i leName ;

public RealImage ( S t r i n g f i leName ) {th is . f i leName = fi leName ;loadFromDisk ( f i leName ) ;

}

@Overridepublic void d i sp lay ( ) {

System . out . p r i n t l n ( " D isp lay ing " + f i leName ) ;}

private void loadFromDisk ( S t r i n g f i leName ) {System . out . p r i n t l n ( " Loading " + f i leName ) ;

}}

Introduction Summary References

PROXY

PROXY IMAGE

public class ProxyImage implements Image {

private RealImage realImage ;private S t r i n g f i leName ;

public ProxyImage ( S t r i n g f i leName ) {th is . f i leName = fi leName ;

}

@Overridepublic void d i sp lay ( ) {

i f ( real Image == nul l ) {real Image = new RealImage ( f i leName ) ;

}realImage . d i sp lay ( ) ;

}}

Introduction Summary References

PROXYSTEP 3: USE THE PROXYIMAGE TO GET OBJECT OF REALIMAGE CLASS WHEN REQUIRED.

PROXY PATTERN DEMO

public class ProxyPatternDemo {

public s t a t i c void main ( S t r i n g [ ] args ) {Image image = new ProxyImage ( " test_10mb . jpg " ) ;

/ / image w i l l be loaded from d iskimage . d i sp lay ( ) ;System . out . p r i n t l n ( " " ) ;

/ / image w i l l not be loaded from d iskimage . d i sp lay ( ) ;

}}

Introduction Summary References

PROXYSTEP 4: VERIFY THE OUTPUT.

FACTORY PATTERN DEMO

Loading test_10mb.jpgDisplaying test_10mb.jpgDisplaying test_10mb.jpg

Introduction Summary References

FACADE

Problem: Actual work is performed by two or more objects,but you want to hide this level of complexity from the client.Solution: Create a facade object that receives themessages, but passes commands on to the workers forcompletion.

FIGURE: Facade

Also similar to adapter and proxy.

Introduction Summary References

FACADEANOTHER EXAMPLE

FIGURE: Facade

Introduction Summary References

FACADESTEP 1: CREATE AN INTERFACE.

SHAPE

public inter face Shape {void draw ( ) ;

}

Introduction Summary References

FACADESTEP 2: CREATE CONCRETE CLASSES IMPLEMENTING THE SAME INTERFACE.

RECTANGLE

public class Rectangle implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide Rectangle : : draw ( ) method . " ) ;}

}

SQUARE

public class Square implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide Square : : draw ( ) method . " ) ;}

}

Introduction Summary References

FACADE

CIRCLE

public class C i r c l e implements Shape {

@Overridepublic void draw ( ) {

System . out . p r i n t l n ( " I ns ide C i r c l e : : draw ( ) method . " ) ;}

}

Introduction Summary References

FACADESTEP 3: CREATE A FACADE CLASS.

SHAPE MAKER

public class ShapeMaker {private Shape c i r c l e ;private Shape rec tang le ;private Shape square ;

public ShapeMaker ( ) {c i r c l e = new C i r c l e ( ) ;rec tang le = new Rectangle ( ) ;square = new Square ( ) ;

}

public void drawCirc le ( ) {c i r c l e . draw ( ) ;

}public void drawRectangle ( ) {

rec tang le . draw ( ) ;}public void drawSquare ( ) {

square . draw ( ) ;}

}

Introduction Summary References

FACADESTEP 4: USE THE FACADE TO DRAW VARIOUS TYPES OF SHAPES.

FACADE PATTERN DEMO

public class FacadePatternDemo {public s t a t i c void main ( S t r i n g [ ] args ) {

ShapeMaker shapeMaker = new ShapeMaker ( ) ;

shapeMaker . drawCirc le ( ) ;shapeMaker . drawRectangle ( ) ;shapeMaker . drawSquare ( ) ;

}}

Introduction Summary References

FACADESTEP 5: VERIFY THE OUTPUT.

FACADE PATTERN OUTPUT

Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.

Introduction Summary References

OBSERVER

Problem: How do you dynamically (at run time) add andremove connections between objects.Solution: An Observer Manager implements the followingprotocol:“I Want to Observe X” – the OM will keep track of who iswatching who.“Tell Everybody who is Observing Met that I haveChanged” – the OM can then tell everybody that an objecthas changed.In this way neither the observer nor the observed objectneed know the existance of the other.

Introduction Summary References

PATTERNS AND FRAMEWORKS

Both are ways of describing and documenting solutions tocommon problems.Frameworks are more “shrinkwrapped”, ready forimmediate use.Patterns are more abstract – many patterns are involved inthe solution of one problem.

Introduction Summary References

SUMMARY

In this chapter we have examined a variety of topicsrelated to dependency.How classes and objects can depend upon each other.How methods within a class can depend upon each other.How to control visibility, as a means of controllingdependency.How strong dependency can be weakened by usingalternative designs.

Introduction Summary References

REFERENCES

Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.An Introduction to Object Oriented Programming, TimothyA. Budd.Some examples and text has been used from Tutsplus.Some examples have been taken from Tutorialspoint.Code Project.This presentation is developed with Beamer:

Darmstadt, crane.