1 / 19

CS 350 – Software Design Putting Patterns Together

CS 350 – Software Design Putting Patterns Together. Let’s put a lot of patterns together. We need a lot of ducks! public interface Quackable { public void quack(); } Some ducks that implement Quackable . public class MallardDall implements Quackable { public void quack() {

kfontanez
Download Presentation

CS 350 – Software Design Putting Patterns Together

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. CS 350 – Software DesignPutting Patterns Together Let’s put a lot of patterns together. We need a lot of ducks! public interface Quackable { public void quack(); } Some ducks that implement Quackable. public class MallardDall implements Quackable { public void quack() { System.out.println(“Quack”); } } public class RedheadDuck implements Quackable { public void quack() { System.out.println(“Quack”); } }

  2. CS 350 – Software DesignPutting Patterns Together More quackable ducks. public class DuckCall implements Quackable { public void quack() { System.out.println(“Kwak”); } } public class RubberDuck implements Quackable { public void quack() { System.out.println(“Squeak”); } }

  3. CS 350 – Software DesignPutting Patterns Together public class DuckSimulator { public static void main(String[] args) { DuckSimulatorsimultor = new DuckSimulator(); simulator.simulate(); } void simulator() { QuackablemallardDuck = new MallardDuck(); QuackableredheadDuck = new RedheadDuck(); QuackableduckCall = new DuckCall(); QuackablerubberDuck = new RubberDuck(); System.out.println(“\nDuck Simulator”); simulate(mallardDuck); simulate(redheadDuck); simulate(duckCall); simulate(rubberDuck); void simulate(Quackable duck) { duck.quack(); } }

  4. CS 350 – Software DesignPutting Patterns Together public class Goose { public void honk() { System.out.println(“Honk”); } } public class Witch{ public void Complains() { System.out.println(“I am not a witch, they dressed me like this!”); } }

  5. CS 350 – Software DesignPutting Patterns Together We can use an adapter public class GooseAdapter implements Quackable { Goose goose; public GooseAdapter(Goose goose) { this.goose = goose; } public void quack() { goose.honk(); } } public class WitchAdapter implements Quackable { Witch witch; public WitchAdapter(Witch witch) { this.witch = witch; } public void quack() { witch.complain(); } }

  6. CS 350 – Software DesignPutting Patterns Together Let’s add a goose to our simulator public class DuckSimulator { public static void main(String[] args) { DuckSimulatorsimultor = new DuckSimulator(); simulator.simulate(); } void simulate() { QuackablemallardDuck = new MallardDuck(); QuackableredheadDuck = new RedheadDuck(); QuackableduckCall = new DuckCall(); QuackablerubberDuck = new RubberDuck(); QuackablegooseDuck = new GooseAdapater(new Goose()); System.out.println(“\nDuck Simulator”); simulate(mallardDuck); simulate(redheadDuck); simulate(duckCall); simulate(rubberDuck); simulate(gooseDuck); } void simulate(Quackable duck) { duck.quack(); } }

  7. CS 350 – Software DesignPutting Patterns Together How many quacks do we have? We can decorate the ducks so they can be counted. public class QuackCounter implements Quackable { Quackable duck; static int numberOfQuacks; public QuackCounter (Quackable duck) { this.duck = duck; } public void quack() { duck.quack(); numberOfQuacks++; } public static int getQuacks() { return numberofQuacks; } } }

  8. CS 350 – Software DesignPutting Patterns Together Too many ducks, let’s create a factory! public abstract class AbstractDuckFactory { public abstract QuackablecreateMallardDuck(); public abstract QuackablecreateRedheadDuck(); public abstract QuackablecreateDuckCall(); public abstract QuackablecreateRubberDuck(); public class DuckFactory extends AbstractDuckFactory { public QuackablecreateMallardDuck() { return new MallardDuck(); } public QuackablecreateRedheadDuck() { return new RedheadDuck(); } public QuackablecreateDuckCall() { return new DuckCall(); } public QuackablecreateRubberDuck() { return new RubberDuck(); }

  9. CS 350 – Software DesignPutting Patterns Together What about the countable quackable ducks? public class CountingDuckFactory extends AbstractDuckFactory { public QuackablecreateMallardDuck() { return newQuackCounter(new MallardDuck())); } public QuackablecreateRedheadDuck() { return newQuackCounter(new RedheadDuck()); } public QuackablecreateDuckCall() { return newQuackCounter(new DuckCall()); } public QuackablecreateRubberDuck() { return newQuackCounter(new RubberDuck()); }

  10. CS 350 – Software DesignPutting Patterns Together Let’s alter the simulator to use the factory public class DuckSimulator { public static void main(String[] args) { DuckSimulatorsimultor = new DuckSimulator(); AbstractDuckFactoryduckFactory = new CountingDuckFactory(); Simulator.simulate(duckFactory); } void simulate(AbstractDuckFactoryduckFactory) { QuackablemallardDuck = duckFactory.createMallardDuck(); QuackableredheadDuck = duckFactory.createRedheadDuck(); QuackableduckCall = duckFactory.createDuckCall(); QuackablerubberDuck = duckFactory.createRubberDuck(); QuackablegooseDuck = new GooseAdapater(new Goose()); System.out.println(“\nDuck Simulator”); simulate(mallardDuck); simulate(redheadDuck); simulate(duckCall); simulate(rubberDuck); simulate(gooseDuck); } void simulate(Quackable duck) { duck.quack(); } }

  11. CS 350 – Software DesignPutting Patterns Together Managing this many ducks can be burdensome: QuackablemallardDuck = duckFactory.createMallardDuck(); QuackableredheadDuck = duckFactory.createRedheadDuck(); QuackableduckCall = duckFactory.createDuckCall(); QuackablerubberDuck = duckFactory.createRubberDuck(); QuackablegooseDuck = new GooseAdapater(new Goose()); simulate(mallardDuck); simulate(redheadDuck); simulate(duckCall); simulate(rubberDuck); simulate(gooseDuck);

  12. CS 350 – Software DesignPutting Patterns Together Let’s create a flock of ducks. public class Flock implements Quackable{ ArrayListquackers = new ArrayList(); public void add(Quackablequacker) { quackers.add(quacker); } public void quack() { Iteratoriterator = quackers.iterator(); while (iterator.hasNExt() { Quack quacker = (Quackable)iterator.next(); quacker.quack(); } } } Shows two patterns that we didn’t previously mention, Composite & Iterator.

  13. CS 350 – Software DesignPutting Patterns Together Let’s alter the simulator to a flock public class DuckSimulator { // other stuff void simulate(AbstractDuckFactoryduckFactory) { QuackablemallardDuck = duckFactory.createMallardDuck(); QuackableredheadDuck = duckFactory.createRedheadDuck(); QuackableduckCall = duckFactory.createDuckCall(); QuackablerubberDuck = duckFactory.createRubberDuck(); QuackablegooseDuck = new GooseAdapater(new Goose()) System.out.println(“\nDuck Simulator: With Compisite - Flocks”); Flock flockOfDucks = new Flock(); flockOfDucks.add(redheadDuck); flockOfDucks.add(duckCall); flockOfDucks.add(rubberDuck); flockOfDucks.add(gooseDuck); Flock flock of Mallards = new Flock(); Quack mallardOne = duckFactory.createMallardDuck(); Quack mallardTwo = duckFactory.createMallardDuck(); Quack mallardThree = duckFactory.createMallardDuck(); Quack mallardFour = duckFactory.createMallardDuck(); flockOfMallards.add(mallardOne);} flockOfMallards.add(mallardTwo);} flockOfMallards.add(mallardThree);} flockOfMallards.add(mallardFour);} void simulate(Quackable duck) { duck.quack(); } }

  14. CS 350 – Software DesignPutting Patterns Together What if we also want to observer individual duck behavior? public interface QuackObservable { public void registerObeserver(Observer observer); public void notifyObservers(); } However, we coded all the Quackables differently, so what to do? What to do?

  15. CS 350 – Software DesignPutting Patterns Together What if we also want to observer individual duck behavior? public interface QuackObservableextends Quackable { public void registerObeserver(Observer observer); public void notifyObservers(); } However, we coded all the Quackables differently, so what to do? What to do? public interface QuackObserverextends Quackable { public void quack(); }

  16. CS 350 – Software DesignPutting Patterns Together Implementing the concrete classes public class Observable implements QuackObserable { ArrayList observers = new ArrayList(); QuackObservable duck; public Observable(QuackObservable duck) { this.duck = duck; } public void registerObserver(Observer observer) { observers.add(observer); } public void notifyObservers() { Iteratoriterator = observers.iterator(); while (iterator.hasNext()) { Observer observer = (Observer) iterator.next(); observer.update(duck); } } }

  17. CS 350 – Software DesignPutting Patterns Together Changing the Mallard Duck class public class MallardDall implements Quackable { Observable observable; public MallardDuck() { observable = new Observable(this); } public void quack() { System.out.println(“Quack”); notifyObservers(); } public void registerObserver(Observer observer) { observable.registerObserver(observer); } public void notifyObservers() { observable.notifyObservers(); } }

  18. CS 350 – Software DesignPutting Patterns Together From the Observer side public interface Observer { public void update(QuackObservable duck); } public class Quackologist implements Observer { public void update(QuackObservable duck) { System.out.println(“Quackologist: “ + duck + “ just quacked.”); } }

  19. CS 350 – Software DesignPutting Patterns Together The updated simulator public class DuckSimulator { // other stuff void simulate(AbstractDuckFactoryduckFactory) { // create duck factories and flock here System.out.println(“\nDuck Simulator: With Observer”); Quackologistquackologist = newQuackologist(); flockOfDucks.registerObserver(quackologist); simulate(flockOfDucks); System.out.println(“\nThe ducks quacked “ + QuackCounter.getQuacks() + “ times”); void simulate(Quackable duck) { duck.quack(); } }

More Related