190 likes | 296 Views
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() {
E N D
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”); } }
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”); } }
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(); } }
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!”); } }
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(); } }
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(); } }
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; } } }
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(); }
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()); }
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(); } }
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);
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.
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(); } }
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?
CS 350 – Software DesignPutting Patterns Together What if we also want to observer individual duck behavior? public interface Quackable extends QuackObservable { public void registerObeserver(Observer observer); public void notifyObservers(); } However, we coded all the Quackables differently, so what to do? What to do? public interface Quackable extends QuackObserver { public void quack(); }
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); } } }
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(); } }
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.”); } }
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(); } }