The Decorator Design Pattern - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

The Decorator Design Pattern

Description:

A Graphical version of Bulls and Cows (like Mastermind) ... future extensions to the system unbearable are probably not very good solutions at all... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 16
Provided by: BenSc
Category:

less

Transcript and Presenter's Notes

Title: The Decorator Design Pattern


1
Session 30
  • The Decorator Design Pattern

2
PA07 Posted
  • A Graphical version of Bulls and Cows (like
    Mastermind)
  • Must provide the ability to set the following at
    construction of the game
  • six colors to choose from in building the
    sequence
  • four colors in the code, duplicate colors
    allowed.
  • ten attempts to break the code. 

3
PA07 Posted
  • Your program should also provide as a minimum the
    following features
  • a display of the choices available to the
    code-breaker in constructing the guesses
  • some input mechanism for specifying a guess
  • a display of the guess from the code-breaker
  • a display of the response from the code-maker
  • a display containing all past guesses/responses
  • a way to start a new game and quit the program
  • the use of one Swing element we haven't explored
    yet (eg. JTextField, JTextArea, JCheckbox,
    JRadioButton, JMenu, etc.  Basically ANYTHING
    other than JButton or JScrollBar.

4
PA07 Posted
  • You may elect to work with a single partner
  • Both students must email me by Wednesday,
    November 7th.
  • I will conduct in-person grading after return
    from Thanksgiving break.

5
An Exercise
  • The physics department would like for us to write
    a simple ball world program that it can use to
    teach concepts of friction. In this program, we
    need for some MovableBalls to decelerate. Every
    time one of these decelerating balls moves, its
    speed decreases by 5.
  • Add a DeceleratingBall class to the Ball
    hierarchy for this purpose.

6
Review A Possible Solution
  • public class DeceleratingBall extends MovableBall
  • public DeceleratingBall( int x, int y, int r,
    double dx, double dy )
  • super( x, y, r, dx, dy )
  • public void move()
  • super.move()
  • setMotion( xMotion() 0.95,
  • yMotion() 0.95 )
  • We create a DeceleratingBall just as we would a
    MovableBall
  • DeceleratingBall b new DeceleratingBall( 10,
    15, 5, 5.0, 10.0 )

7
A New Wrinkle
  • Running the program, we realize that some
    decelerating balls need to bounce off the walls
    they hit, too. So we need a class of BoundedBalls
    that decelerate.
  • Can you fix the problem?

8
A New Solution
  • public class DeceleratingBoundedBall extends
    BoundedBall
  • public DeceleratingBoundedBall(int x, int y, int
    r,
  • double dx, double dy, Frame f )
  • super( x, y, r, dx, dy, f )
  • public void move()
  • super.move()
  • setMotion( xMotion() 0.95, yMotion() 0.95
    )

9
How Good is Our Solution?
  • Our approach to this family of problems is
    straightforward implement a decelerating version
    of any Ball class that needs a decelerating
    counterpart.
  • What are the strengths of this approach?
  • It is simple.
  • It is easy to implement right now.

10
How Good is Our Solution?
  • What are the weaknesses of this approach?
  • It is tedious.
  • It repeats codes. The move() methods in the
    class
  • DeceleratingBall and the class Decelerating-Bounde
    dBall are identical!
  • You may be asking yourself, So what? It works.

11
So what? It works.
  • What happens if we need to change the
    deceleration factor, say, from 95 to 80?
  • We must remember to make the change in two
    different classes.
  • What happens if we need to add deceleration
    behavior to other MovableBalls?
  • More subclasses!
  • What happens if we need to add another kind of
    behavior to our ball classes, including the
    decelerating balls?
  • Even more subclasses!
  • Solutions that make future extensions to the
    system unbearable are probably not very good
    solutions at all...

12
The Full Ball Hierarchy
13
An Alternative Solution
  • BoundedBalls respond to the same set of messages
    as MovableBalls. So they are substitutable for
    one another. Can we use this to our advantage?
  • public class DeceleratingBall extends MovableBall
  • private MovableBall workerBall
  • public DeceleratingBall( MovableBall aBall )
  • super()
  • workerBall aBall
  • public void move()
  • workerBall.move()
  • workerBall.setMotion(
  • workerBall.xMotion() 0.95,
  • workerBall.yMotion() 0.95 )

14
An Alternative Solution
  • // ALL OTHER MESSAGES ARE DELEGATED
  • // DIRECTLY TO THE INSTANCE VARIABLE!
  • public void paint( Graphics g )
  • workerBall.paint( g )
  • public void setColor( Color newColor )
  • workerBall.setColor( newColor )
  • protected int radius()
  • return workerBall.radius()
  • ...
  • // -------------------------------------------
  • protected double xMotion()
  • return workerBall.xMotion()
  • ...

15
Using DeceleratingBalls
  • Now, we create a DeceleratingBall by giving it a
    MovableBall to direct
  • DeceleratingBall b
  • new DeceleratingBall(
  • new MovableBall(10, 15, 5, 2.0, 5.0 ) )
  • What is the advantage of this?
Write a Comment
User Comments (0)
About PowerShow.com