Docsity

Design Patterns: Factory Method, Adapter, Observer, Composite and Bouncing Balls, Study notes of Computer Science

Toggle sidebar
bg1
1
More Design Patterns
CS 3331
Fall 2006
Outline
•Factory method
•Adapter
•Observer
•Composite
Bouncing Ball
•To bounce different types of balls
BouncingBall
Circle
draw()
Ball
draw()
Rectangle
draw()
1
Creating Different Balls
Q: What’s the problem with this kind of code?
public class BouncingBall extends java.applet.Applet {
private Ball ball;
private static final String BALL_TYPE = ā€œcircleā€;
public BouncingBall() {
if (ā€œcircleā€.equals(BALL_TYPE)) {
ball = new Circle();
} else if (ā€œrectangleā€.equals(BALL_TYPE)) {
ball = new Rectangle();
} else {
ball = new Circle();
}
}
// the rest of code …
}
A Better Way of Creating Balls
Example (Cont.)
How to bounce a triangle ball?
public class TriangleBouncingBall extends BouncingBall {
protected Ball createBall() {
return new Triangle();
}
private static class Triangle implements Ball {
public void draw() { /* … */ }
// the rest of code here …
}
}
pf3
pf4
pf5

Partial preview of the text

Download Design Patterns: Factory Method, Adapter, Observer, Composite and Bouncing Balls and more Study notes Computer Science in PDF only on Docsity!

More Design Patterns

CS 3331

Fall 2006

Outline

• Factory method

• Adapter

• Observer

• Composite

Bouncing Ball

• To bounce different types of balls

BouncingBall Circle draw() Ball draw () Rectangle draw()

Creating Different Balls

Q: What’s the problem with this kind of code?

public class BouncingBall extends java.applet.Applet {

private Ball ball;

private static final String BALL_TYPE = ā€œcircleā€;

public BouncingBall() {

if (ā€œcircleā€.equals(BALL_TYPE)) { ball = new Circle();

} else if (ā€œrectangleā€.equals(BALL_TYPE)) {

} else {^ ball = new Rectangle();

ball = new Circle();

// the rest of code …

A Better Way of Creating Balls Example (Cont.)

How to bounce a triangle ball?

public class TriangleBouncingBall extends BouncingBall {

protected Ball createBall() { return new Triangle();

private static class Triangle implements Ball { public void draw() { /* … */ }

// the rest of code here …

Factory Method To define an interface for creating an object, but let subclasses to decide which class to instantiate. Product ConcreteProduct AbstractClass factoryMethod operation() () ConcreteClass factoryMethod() (^) return new ConcreteProduct(); p = factoryMethod(); Outline Factory method

  • Adapter
  • Observer
  • Composite Bouncing Cars

How to bounce instances of the class Car?

public class BouncingCar extends BouncingBall {

protected Ball createBall() { return new Car();

public class BounceableCar extends Car implements Ball {

public void draw() { /* … */ }

// the rest of code here …

}^ }

BounceableCar() ;

Adapter

To convert the interface of a class into another

interface that clients expect.

Client operation^ Target () operation()^ Adaptee adaptee.operation(); Adapter operation() Adapter (Cont.) Client operation^ Target () operation()^ Adaptee Adapter Outline Factory method Adapter

  • Observer
  • Composite

AWT Components Bouncing Multiple Balls Using Composite Pattern (Cont.)

public class BallGroup extends Ball {

private Set balls = new HashSet(); // each b in balls is instanceof Ball

public void add(Ball b) {

balls.add(b);

public void remove(Ball b) {

balls.remove(b);

public void paint(Graphics g) {

for (Iterator i = balls.iterator(); i.hasNext(); ) {

((Ball) i.next()).draw(g);

}^ }

Iterator Pattern Collection iterator () Iterator hasNext next ()() ConcreteCollection iterator() ConcreteIterator <>^ hasNext() next() return new ConcreteIterator()

  • Intent
    • To provide a way to access the elements of a collection sequentially Factory Design Pattern AbstractFactory makeProduct () Product ConcreteFactory makeProduct() create ConcreteProuct Client
  • Intent
  • To decouple object creation from its use and to support different way of creating objects
  • To define an interface for creating objects but let subclasses decide which class to instantiate and how Example
  • Complex numbers ComplexFactory makeComplext () (^) Complex PolarFactory makeComplex() RectangularFactory^ <> makeComplex() PolarComplex RectangularComplex <>

Template Methods

  • Intent
    • To define a skeleton algorithm by

deferring some steps to subclasses

  • To allow the subclasses to redefine

certain steps

AbstractClass templateMethod() hookMethod1 () hookMethod2 () ConcreteClass hookMethod1() hookMethod2() … hookMethod1() … hookMethod2() … Template Methods (Cont.)

  • Terminology
    • Hook methods : placeholders for the

behaviour to be implemented by

subclasses

  • Template methods : methods containing

hook methods

  • Hot spots : changeable behaviours of

generic classes represented by hook

methods

  • Frozen spots : fixed behaviours of generic

classes represented by template methods

More Example

  • Generic function plotter
    • To plot arbitrary single-variable functions

on a two-dimensional space

Plotter func paint()() plotFunction() plotCoordinates() PlotSine func() … func() … Applet PlotCosine func()