0% found this document useful (0 votes)
3 views5 pages

L28 MoreDesignPatterns

The document outlines various design patterns including Factory Method, Adapter, Observer, and Composite, providing examples of their implementation in Java. It discusses the creation of different types of balls and cars using the Factory Method pattern, as well as the Adapter pattern for converting interfaces. Additionally, it covers the Composite pattern for handling collections of components and introduces the Template Method pattern for defining skeleton algorithms.

Uploaded by

jimwilmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

L28 MoreDesignPatterns

The document outlines various design patterns including Factory Method, Adapter, Observer, and Composite, providing examples of their implementation in Java. It discusses the creation of different types of balls and cars using the Factory Method pattern, as well as the Adapter pattern for converting interfaces. Additionally, it covers the Composite pattern for handling collections of components and introduces the Template Method pattern for defining skeleton algorithms.

Uploaded by

jimwilmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Outline

• Factory method
• Adapter
More Design Patterns • Observer
• Composite

CS 3331

Fall 2006

Bouncing Ball Creating Different Balls


public class BouncingBall extends java.applet.Applet {
• To bounce different types of balls private Ball ball;
private static final String BALL_TYPE = “circle”;
1
BouncingBall Ball public BouncingBall() {
draw()
if (“circle”.equals(BALL_TYPE)) {
ball = new Circle();
} else if (“rectangle”.equals(BALL_TYPE)) {
ball = new Rectangle();
} else {
ball = new Circle();
Circle Rectangle }
draw() draw() }
// the rest of code …
}

Q: What’s the problem with this kind 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 …
}
}

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

Bouncing Cars Adapter


How to bounce instances of the class Car? To convert the interface of a class into another
interface that clients expect.
public class BouncingCar extends BouncingBall {

protected Ball createBall() {


return new Car();
BounceableCar(); Target Adaptee
} Client
} operation() operation()

public class BounceableCar extends Car implements Ball {


Adapter
public void draw() { /* … */ }
operation() adaptee.operation();
// the rest of code here …
}
}

Adapter (Cont.) Outline


Factory method
Adapter
Target Adaptee
Client
operation() operation() • Observer
• Composite

Adapter

2
A Closer Look at JButton Static Structure
How the button events (e.g., clicking) are
handled?
listeners 0..*
JButton ActionListener
addActionListener() actionPerformed()
JButton button = new JButton(“Ok”);
removeActionListener()
button.addActionListener(new OkButtonListener());
fireActionPerformed()

private class OkButtonListener implements ActionListener {


OkButtonListener
public void actionPerformed(ActionEvent e) { for each l in listeners
actionPerformed()
System.out.println(“Ok button pressed!”); l.actionPerformed(e);
}
}

Dynamic Behavior Outline


button : JButton l : OkButtonListener Factory method
Adapter
addActionListener(l)

e : ActionEvent
Observer
<<create>>

• Composite
fireActionPerformed(e)

actionPerformed(e)

getSource()

Composite Design Pattern Example


• To allow clients to treat both single components and Composing GUI
collections of components identically
• To define recursive data structures such as trees public class MyApplet extends java.applet.Applet {
public MyApplet() {
add(new Label(“My label”));
uses * add(new Button(“My button”));
Client Component
Panel myPanel = new Panel();
operation() myPanel.add(new Label(“Sublabel”));
myPanel.add(new Button(“Subbutton”));
add(myPanel);
}
}
Leaf Composite
operation() operation()
add(Component)
remove(Component)

3
AWT Components Bouncing Multiple Balls

Using Composite Pattern (Cont.) Iterator Pattern


public class BallGroup extends Ball { • Intent
private Set balls = new HashSet(); // each b in balls is instanceof Ball – To provide a way to access the elements of a
public void add(Ball b) {
collection sequentially
balls.add(b);
} Iterator
Collection
public void remove(Ball b) { hasNext()
iterator()
next()
balls.remove(b);
}
public void paint(Graphics g) {
for (Iterator i = balls.iterator(); i.hasNext(); ) { ConcreteCollection ConcreteIterator
((Ball) i.next()).draw(g); iterator() <<create>> hasNext()
} next()
}
}
return new ConcreteIterator()

Factory Design Pattern Example


• Intent • Complex numbers
– To decouple object creation from its use and to
support different way of creating objects
– To define an interface for creating objects but let ComplexFactory
subclasses decide which class to instantiate and makeComplext()
how Complex

Product
Client AbstractFactory
makeProduct() <<create>>
PolarFactory PolarComplex
makeComplex()

create
ConcreteFactory ConcreteProuct <<create>>
RectangularFactory RectangularComplex
makeProduct()
makeComplex()

4
Template Methods Template Methods (Cont.)
• Intent • Terminology
– To define a skeleton algorithm by – Hook methods: placeholders for the
deferring some steps to subclasses behaviour to be implemented by
– To allow the subclasses to redefine subclasses
certain steps
– Template methods: methods containing
AbstractClass
… hook methods
hookMethod1()
templateMethod() … – Hot spots: changeable behaviours of
hookMethod1() hookMethod2()
hookMethod2() … generic classes represented by hook
methods
ConcreteClass – Frozen spots: fixed behaviours of generic
hookMethod1() classes represented by template methods
hookMethod2()

More Example
• Generic function plotter
– To plot arbitrary single-variable functions
on a two-dimensional space
Applet Plotter
func()
paint() …
plotFunction() func()
plotCoordinates() …

PlotSine PlotCosine
func() func()

You might also like