29
2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207 [email protected] ://www-public.it-sudparis.eu/~gibson/Teaching/CSC73 Design Patterns Revisited …/~gibson/Teaching/CSC7322/L13-DesignPatterns-2.pdf

CSC 7322 : Object Oriented Development J Paul Gibson, A207 [email protected]

  • Upload
    teal

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 7322 : Object Oriented Development J Paul Gibson, A207 [email protected] http://www-public. it-sudparis.eu /~gibson/Teaching/CSC7322/. Design Patterns Revisited …/~ gibson / Teaching /CSC7322/L13-DesignPatterns-2.pdf. Learning by PBL – the patterns selected. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.1

CSC 7322 : Object Oriented Development

J Paul Gibson, A207

[email protected]

http://www-public.it-sudparis.eu/~gibson/Teaching/CSC7322/

Design Patterns Revisited

…/~gibson/Teaching/CSC7322/L13-DesignPatterns-2.pdf

Page 2: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.2

1. Singleton - creational2. Iterator – behavioural3. Visitor – behavioural4. Proxy - structural5. Factory - creational6. Decorator – structural7. Facade - structural8. Adapter - structural9. Chain Of Responsibility - behavioural10. MVC – a composite pattern (Strategy, Observer, Composite)

Learning by PBL – the patterns selected

Page 3: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.3

Decorator Pattern

See - http://sourcemaking.com/design_patterns/decorator

• Intent

• Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

• Client-specified embellishment of a core object by recursively wrapping it.

• Wrapping a gift, putting it in a box, and wrapping the box.

• Problem

• You want to add behavior or state to individual objects at run-time.

• Inheritance is not feasible because it is static and applies to an entire class.

Page 4: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.4

Decorator Pattern

See - http://sourcemaking.com/design_patterns/decorator

Relation to other patterns

Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.

Adapter changes an object’s interface, Decorator enhances an object’s responsibilities. Decorator is thus more transparent to the client. As a consequence, Decorator supports recursive composition, which isn’t possible with pure Adapters.

Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.

A Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn’t intended for object aggregation.

Page 5: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.5

Decorator Pattern

See - http://sourcemaking.com/design_patterns/decorator

Relation to other patterns

Decorator is designed to let you add responsibilities to objects without subclassing. Composite’s focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.

Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition.

Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.

Decorator lets you change the skin of an object. Strategy lets you change the guts.

Most of these patterns use a form of delegation (which is not usually considered a pattern in its own right)

Page 6: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.6

Decorator Pattern

Class Diagram UMLThe following diagram shows structure of typical decorator pattern, the diagram was taken dofactory.com

Page 7: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.7

Decorator Pattern

Class Diagram UMLThe following diagram shows a concrete example of an Account(from http://blog.decarufel.net/2009/09/using-decorator-or-wrapper-design.html)

Page 8: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.8

Decorator Pattern

Sequence Diagram UMLThe following diagram shows a concrete example of Account dynamic behaviour

Page 9: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.9

Decorator Pattern

Sequence Diagram UML:explanation

1. Call Bank.CreateAccount. 2. The Bank instantiates an Account class. 3. The Bank creates an AccountDepositValidator and wrap Account with it 4. The Bank return an instance of IAccount. 5. Deposit is called on IAccount which is an instance of AccountDepositValidator 6. AccountDepositValidator calls Validate 7. AccountDepositValidator call GetValidations to retrieve the list of validation to

evaluate 8. An AmountGreaterThaZeroValidation is created 9. IsValid returns true 10. AccountDepositValidator call base Deposit method 11. Balance value is updated

Page 10: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.10

Decorator Problem : The classic Christmas tree (revisited)

Problem: Examine the Java code in the package p_decorator in the folder Patterns (~gibson/Teaching/CSC7322/Code/Decorator.zip)

Page 11: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.11

Decorator – Christmas Tree

package specifications;

public interface BranchOfTreeSpecification {

public void animate(); public String getDecorations();

}

Page 12: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.12

Decorator – Christmas Tree

package specifications;

public abstract class BranchDecoratorSpecifiation implements BranchOfTreeSpecification{

protected BranchOfTreeSpecification decoratedBranch;

public BranchDecoratorSpecifiation(BranchOfTreeSpecification branchToDecorate){decoratedBranch= branchToDecorate;}

public void animate(){decoratedBranch.animate(); }

}

Page 13: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.13

Decorator - Christmas Tree

package models;

import specifications.BranchOfTreeSpecification;

public class UndecoratedBranch implements BranchOfTreeSpecification{

public void animate(){}

public String getDecorations(){return "";}

}

Page 14: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.14

Decorator - Christmas Treepackage models;import specifications.BranchDecoratorSpecification;import specifications.BranchOfTreeSpecification;

public class BallDecorator extends BranchDecoratorSpecification{

boolean spinning;

public BallDecorator(BranchOfTreeSpecification branchOfTree){ super(branchOfTree); spinning = true; }

public String getDecorations(){ String str = decoratedBranch.getDecorations(); if (spinning) str = str +" Spinning"; str = str+ " Ball."; return str; } public void animate(){ decoratedBranch.animate(); spinning = !spinning; } }

Page 15: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.15

Decorator– Christmas Treepackage models;import specifications.BranchDecoratorSpecifiation;import specifications.BranchOfTreeSpecification;public class LightsDecorator extends BranchDecoratorSpecifiation{

String colour;public LightsDecorator(BranchOfTreeSpecification branchOfTree){

super(branchOfTree);colour = "red";}

public String getDecorations(){ String str = decoratedBranch.getDecorations(); str =str+ " colour = "+colour;

return str;}

public void animate(){ decoratedBranch.animate(); if (colour.equals("red")) colour ="white"; else if (colour.equals("white")) colour ="blue"; else colour = "red"; } }

Page 16: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.16

Decorator– Christmas Treepublic class TestBallDecorator {public static void main (String [] args){

BranchOfTreeSpecification plainBranch = new UndecoratedBranch();BranchOfTreeSpecification decoratedBranch = new BallDecorator( plainBranch);BranchOfTreeSpecification reDecoratedBranch = new BallDecorator( new BallDecorator( plainBranch));

System.out.println("plainBranch.getDecorations() ="+plainBranch.getDecorations());System.out.println("decoratedBranch.getDecorations() ="+decoratedBranch.getDecorations());System.out.println("reDecoratedBranch.getDecorations() =" +reDecoratedBranch.getDecorations());

plainBranch.animate();System.out.println("plainBranch.animate.getDecorations() ="+plainBranch.getDecorations());

decoratedBranch.animate();System.out.println("decoratedBranch.animate.getDecorations() ="+decoratedBranch.getDecorations());

reDecoratedBranch.animate();System.out.println("reDecoratedBranch.animate.getDecorations() =" +reDecoratedBranch.getDecorations());}}

Page 17: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.17

Decorator - Christmas Tree

Page 18: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.18

Decorator – Christmas Tree

TO DO:

• Test Lights Decoration• Test Lights with Balls Decoration• Add new Decoration (Stars, eg)• Test 3 Decorations together

Create XMAS tree with 4 branches:• 2 Lights and 2 Balls• 2 Lights and 3 Stars • 2 Stars and 3 Balls• 2 Lights and 2 Stars and 2 Balls

PROBLEM: Ensure that the state of decorations on the same branch of tree is co-ordinated (but not necessarily on different branches)

OPTION: Maximum number of decorations on any branch is 6

Page 19: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.19

Decorator– Christmas Tree

What to learn from the problem:

If you really need multiple instances of same decoration then this pattern is probably not correct.

The following 4 sentences seem equivalent but may lead to different designs/implementations:

The branch « is a » branch with lights and starsThe branch « is a » branch with lights and « is a » branch with stars

The branch « has » lights and starsThe branch « has » lights and the branch « has » stars

Knowing which design is best depends on knowing precisely what is the intended/required behaviour.

The decorator is not intended to allow statements like:The branch « is a » branch with lights and « is a » branch with lights

Page 20: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.20

Decorator – Christmas TreeSolving the problem:

If we really need multiple decorations of the same type on a single branch do not use the decorator pattern in this way

We should fix the decorator pattern so that decorating a branch with ball behaviour that has already been decorated with ball behaviour is either:

• Handled by an exception, or• Ignored

TO DO – implement one of these solutions, and retest the new behaviour

NOTE: As requirements change you may be tempted to compromise your system by holding on to a pattern which is no longer appropriate

Page 21: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.21

Facade versus Adapter

Intuitively, what are the differences?

Page 22: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.22

Facade versus Adapter

Page 23: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.23

Facade versus Adapter

Page 24: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.24

Facade - provides a unified interface to a set of interfaces in a subsystem

Identify a simpler, unified interface for the subsystem/component.

Design a ‘wrapper’ class that encapsulates the subsystem.

The facade/wrapper captures the complexity and collaborations of the component, and delegates to the appropriate methods.

The client uses (is coupled to) the Facade only.

Facade defines a new interface, whereas Adapter uses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.

Page 25: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.25

Adapter - lets classes work together that couldn’t otherwise because of incompatible interfaces

Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee).

Identify the interface that the client requires.

Design a “wrapper” class that can “impedance match” the adaptee to the client.

The adapter/wrapper class “has a” instance of the adaptee class.

The adapter/wrapper class “maps” the client interface to the adaptee interface.

The client uses (is coupled to) the new interface

Page 26: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.26

Realation to Other Design Patterns

Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. While Facade routinely wraps multiple objects and Adapter wraps a single object; Facade could front-end a single complex object and Adapter could wrap several legacy objects.

Adapter makes things work after they’re designed; Bridge makes them work before they are. Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together.

Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.

Page 27: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.27

The Rectangle Problem

A legacy Rectangle component’s display() method expects to receive “x, y, w, h” parameters. But the client wants to pass “upper left x and y” and “lower right x and y”. This mismatch can be reconciled by using a design pattern.

Question: is this suitable for an adapter or a facade (or something else)

Draw the UML design and implement in Java

Page 28: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.28

Chain of responsibility

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Page 29: CSC 7322 : Object  Oriented Development J  Paul  Gibson, A207 paul.gibson@int-edu.eu

2013: J Paul Gibson TSP: Software Engineering CSC7322/DesignPatterns.29

Chain of responsibility

Note: Chain of Responsibility can use Command to represent requests as objects

THIS MAY BE USEFUL IN YOUR OO PROJECT