1 / 35

More Design Patterns

More Design Patterns. CS 3331 Fall 2009. GoF Patterns. Creational Structural Behavioral Abstract Factory Adapter Chain of Responsibility Builder Bridge Command Factory Method Composite Interpreter Prototype Decorator Iterator Singleton Façade Mediator

fai
Download Presentation

More Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Design Patterns CS 3331 Fall 2009

  2. GoF Patterns Creational Structural Behavioral Abstract FactoryAdapter Chain of Responsibility Builder Bridge Command Factory MethodComposite Interpreter Prototype DecoratorIterator Singleton Façade Mediator Flyweight Memento Proxy Observer State Strategy Template Method Visitor E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns, Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995.

  3. Outline • Factory method • Adapter • Observer • Composite

  4. Ball draw() Circle draw() Rectangle draw() Bouncing Ball Revisited • To bounce different types of balls 1 BouncingBall Q: What design pattern is used?

  5. Creating Different Balls 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 … } Q: What’s the problem with this kind of code?

  6. A Better Way of Creating Balls public class BouncingBall extends java.applet.Applet { private Ball ball; public BouncingBall() { ball = createBall(); } protected Ball createBall() { return new Circle(); } // the rest of code … } Q: Why is this code better?

  7. 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 … } }

  8. ConcreteProduct Product ConcreteClass factoryMethod() return new ConcreteProduct(); p = factoryMethod(); AbstractClass factoryMethod() operation() Factory Method To define an interface for creating an object, but let subclasses to decide which class to instantiate. Q: Have we seen a similar design pattern before?

  9. Exercise Refactor the following applet to apply Factory Method. public class ConfigurableClock extends java.applet.Applet { private ClockDisplay display; public ConfigurableClock() { display = new DigitalDisplay(); } // the rest of code here … } Also, define a subclass that uses an AnalogDisplay.

  10. Outline • Factory method • Adapter • Observer • Composite

  11. BounceableCar(); public class BounceableCar extends Car implements Ball { public void draw() { /* … */ } // the rest of code here … } } Bouncing Cars How to bounce instances of the class Car? public class BouncingCar extends BouncingBall { protected Ball createBall() { return new Car(); } }

  12. Client Adaptee operation() Target operation() Adapter operation() Adapter To convert the interface of a class into another interface that clients expect. adaptee.operation();

  13. Adapter Client Adaptee operation() Target operation() Adapter (Cont.)

  14. Teacher +Teacher(n: String) +getName(): String Student +Student(n: String) +getName(): String Staff Client +doIt(s: Student) Exercise Define an adapter class so that the class Teacher can also work with the client.

  15. Outline • Factory method • Adapter • Observer • Composite

  16. A Closer Look at JButton How the button events (e.g., clicking) are handled? JButton button = new JButton(“Ok”); button.addActionListener(new OkButtonListener()); … private class OkButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println(“Ok button pressed!”); } }

  17. ActionListener actionPerformed() OkButtonListener actionPerformed() JButton addActionListener() removeActionListener() fireActionPerformed() for each l in listeners l.actionPerformed(e); Static Structure listeners 0..*

  18. l : OkButtonListener button :JButton e : ActionEvent Dynamic Behavior addActionListener(l) <<create>> fireActionPerformed(e) actionPerformed(e) getSource()

  19. Observer Pattern • Intent • To define relationship between a group of objects such that whenever one object is updated all others are notified automatically. • Context • Multiple objects depend on the state of one object. • Set of dependent objects may change at runtime. • Solution • Allow dependent objects to register with object of interest, and notify them of updates when state changes.

  20. Structure Subject Observer observers attach(Observer) 0..* detach(Observer) update() notifyChange() public void update() { public void notifyChange() { ... subject.getState() ... for each o in observers } o.update(); } ConcreteSubject ConcreteObserver subject getState() update() setState() ConcreteObserver may depend on ConcreteSubject, but not the other way around!

  21. Participants • Subject • Knows its observers • Provides interface for attaching and detaching observers • Observer • Defines an updating interface • ConcreteSubject • Stores state of interest • Sends notification to observers on state change • ConcreteObserver • Maintains a reference to a concrete subject. • Stores state that should stay consistent with the subject’s. • Implements the updating interface to keep its state consitent with the subject’s.

  22. o1 : ConcreteObserver o2 : ConcreteObserver s :ConcreteSubject attach(o1) attch(o2) setState() notifyChange() update() getState() update() getState() detach(o1) detach(o2) Collaborations

  23. Example – Observable Counters public class Counter { private int cnt; private List observers = new ArrayList(); public int val() { return cnt; } public void incr() { cnt++; for (Iterator i = observers.iterator(); i.hasNext(); ) { ((CounterObserver) i.next()).valueChanged(); } } public void addObserver(CounterObserver o) { observers.add(o); } } public interface CounterObserver { void valueChanged(); }

  24. incr 12 Example (Cont.) public class CounterApplet extends java.applet.Applet implements CounterObserver { private Counter counter = new Counter(); private JTextField display = new JTextField(4); public CounterApplet() { add(display); JButton incr = new JButton(“incr”); incr.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { counter.incr(); } }); add(incr); counter.addObserver(this); } public void valueChanged() { display.setText(Integer.toString(counter.val())); } }

  25. Exercise Make the following class observable. public class Suspect { public void meetWith(Person p) { /* … */ } }

  26. Outline • Factory method • Adapter • Observer • Composite

  27. Component operation() Leaf operation() Client Composite operation() add(Component) remove(Component) Composite Design Pattern • To allow clients to treat both single components and collections of components identically • To define recursive data structures such as trees uses *

  28. Example Composing GUI public class MyApplet extends java.applet.Applet { public MyApplet() { add(new Label(“My label”)); add(new Button(“My button”)); Panel myPanel = new Panel(); myPanel.add(new Label(“Sublabel”)); myPanel.add(new Button(“Subbutton”)); add(myPanel); } } Q: How can we add any widgets including panels to applets (i.e., panels)?

  29. AWT Components

  30. Ball draw() Circle draw() Rectangle draw() private Ball ball; public void paint(Graphics g) { // … ball.draw(g); // … } Bouncing Multiple Balls 1 BouncingBall

  31. Ball draw() Circle draw() Rectangle draw() private Set balls; public void paint(Graphics g) { // … for (Iterator i = balls.iterator(); i.hasNext(); ) { ((Ball) i.next()).draw(g); } // … } Bouncing Multiple Balls (Cont.) 1..* BouncingBall

  32. Ball draw(Graphics) 1..* Circle draw(Graphics) Rectangle draw(Graphics) BallGroup add(Ball) remove(Ball) draw(Graphics) Using Composite Pattern 1 BouncingBall

  33. 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); } } }

  34. Expression evaluate() Number value() evaluate() 2 BinaryExpression left() right() evaluate() Exercise How to represent an expression, E, where E is a number or E + E, e.g., 1, 1 + 2, 3 + 4 + 5, etc?

  35. Exercise (Cont.) Write the classes Expression, Number, and BinaryExpression of the previous example.

More Related