复合模式
可以说是MVC模式(model-view-controller)通俗讲就是运用了前面所学的设计模式的一个集成体。
鸭子叫声示例
/**
* 鸭子接口
*
* @author NNroc
* @date 2020/8/19 12:14
*/
public interface Quackable extends QuackObservable {
public void quack();
}
/**
* 绿头鸭
*
* @author NNroc
* @date 2020/8/19 12:21
*/
public class MallardDuck 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();
}
}
/**
* 鸭鸣器
*
* @author NNroc
* @date 2020/8/19 12:18
*/
public class DuckCall implements Quackable {
Observable observable;
public DuckCall() {
observable = new Observable(this);
}
public void quack() {
System.out.println("Kwak");
notifyObservers();
}
public void registerObserver(Observer observer) {
observable.registerObserver(observer);
}
public void notifyObservers() {
observable.notifyObservers();
}
}
/**
* 红头鸭
*
* @author NNroc
* @date 2020/8/19 12:22
*/
public class RedheadDuck implements Quackable {
Observable observable;
public RedheadDuck() {
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();
}
}
/**
* 橡皮鸭
*
* @author NNroc
* @date 2020/8/19 12:20
*/
public class RubberDuck implements Quackable {
Observable observable;
public RubberDuck() {
observable = new Observable(this);
}
public void quack() {
System.out.println("squeak");
notifyObservers();
}
public void registerObserver(Observer observer) {
observable.registerObserver(observer);
}
public void notifyObservers() {
observable.notifyObservers();
}
}
/**
* 鹅
*
* @author NNroc
* @date 2020/8/19 12:26
*/
public class Goose {
public void honk() {
System.out.println("Honk");
}
}
/**
* 鹅适配成鸭子
*
* @author NNroc
* @date 2020/8/19 12:27
*/
public class GooseAdapter implements Quackable {
Observable observable;
Goose goose;
public GooseAdapter(Goose goose) {
this.goose = goose;
}
public void quack() {
goose.honk();
notifyObservers();
}
public void registerObserver(Observer observer) {
}
public void notifyObservers() {
}
}
观察者
/**
* 观察者接口,鸭鸣观察
*
* @author NNroc
* @date 2020/8/19 16:56
*/
public interface QuackObservable {
public void registerObserver(Observer observer);
public void notifyObservers();
}
/**
* 观察者接口(可以看到,观察者中update需要一个Quackable对象,这就是为什么需要QuackObservableAbstract抽象类的原因)
*
* @author NNroc
* @date 2020/8/19 19:30
*/
public interface Observer {
public void update(QuackObservable duck);
}
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author NNroc
* @date 2020/8/19 17:00
*/
public class Observable implements QuackObservable {
ArrayList observers = new ArrayList();
QuackObservable duck;
public Observable(QuackObservable duck) {
this.duck = duck;
}
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void notifyObservers() {
Iterator iterator = observers.iterator();
while (iterator.hasNext()) {
Observer observer = (Observer) iterator.next();
observer.update(duck);
}
}
}
/**
* 观察者,哪只鸭子叫了
*
* @author NNroc
* @date 2020/8/19 19:32
*/
public class Quackologist implements Observer {
public void update(QuackObservable duck) {
System.out.println("Quackologist: " + duck + " just quacked.");
}
}
import java.util.ArrayList;
import java.util.Iterator;
/**
* @author NNroc
* @date 2020/8/19 16:34
*/
public class Flock implements Quackable {
ArrayList quackers = new ArrayList();
public void add(Quackable quacker) {
quackers.add(quacker);
}
/**
* 迭代器模式
*/
public void quack() {
Iterator iterator = quackers.iterator();
while ((iterator.hasNext())) {
Quackable duck = (Quackable) iterator.next();
duck.quack();
}
}
@Override
public void registerObserver(Observer observer) {
Iterator iterator = quackers.iterator();
while ((iterator.hasNext())) {
Quackable duck = (Quackable) iterator.next();
duck.registerObserver(observer);
}
}
@Override
public void notifyObservers() {
}
}
装饰者
/**
* 装饰者 记录叫声次数
*
* @author NNroc
* @date 2020/8/19 12:31
*/
public class QuackCounter implements Quackable {
public Quackable duck;
public static int numberOfQuacks;
public QuackCounter(Quackable duck) {
this.duck = duck;
}
public void quack() {
duck.quack();
numberOfQuacks++;
}
public static int getQuacks() {
return numberOfQuacks;
}
@Override
public void registerObserver(Observer observer) {
duck.registerObserver(observer);
}
@Override
public void notifyObservers() {
duck.notifyObservers();
}
}
工厂
/**
* @author NNroc
* @date 2020/8/19 12:40
*/
public abstract class AbstractDuckFactory {
public abstract Quackable createMallardDuck();
public abstract Quackable createRedheadDuck();
public abstract Quackable createDuckCall();
public abstract Quackable createRubberDuck();
}
/**
* @author NNroc
* @date 2020/8/19 16:29
*/
public class CountingDuckFactory extends AbstractDuckFactory {
public Quackable createMallardDuck() {
return new QuackCounter(new MallardDuck());
}
public Quackable createRedheadDuck() {
return new QuackCounter(new RedheadDuck());
}
public Quackable createDuckCall() {
return new QuackCounter(new DuckCall());
}
public Quackable createRubberDuck() {
return new QuackCounter(new RubberDuck());
}
}
/**
* @author NNroc
* @date 2020/8/19 12:42
*/
public class DuckFactory extends AbstractDuckFactory{
public Quackable createMallardDuck() {
return new MallardDuck();
}
public Quackable createRedheadDuck() {
return new RedheadDuck();
}
public Quackable createDuckCall() {
return new DuckCall();
}
public Quackable createRubberDuck() {
return new RubberDuck();
}
}
/**
* 鸭子模拟器
*
* @author NNroc
* @date 2020/8/19 12:23
*/
public class DuckSimulator {
public void simulate(AbstractDuckFactory duckFactory) {
// 鸭子和鸭子工厂
Quackable redheadDuck = duckFactory.createRedheadDuck();
Quackable duckCall = duckFactory.createDuckCall();
Quackable rubberDuck = duckFactory.createRubberDuck();
Quackable gooseDuck = new GooseAdapter(new Goose());
System.out.println("Duck Simulator: With Abstract Factory");
Flock flockOfDucks = new Flock();
flockOfDucks.add(redheadDuck);
flockOfDucks.add(duckCall);
flockOfDucks.add(rubberDuck);
flockOfDucks.add(gooseDuck);
Flock flockOfMallards = new Flock();
Quackable mallard1 = duckFactory.createMallardDuck();
Quackable mallard2 = duckFactory.createMallardDuck();
Quackable mallard3 = duckFactory.createMallardDuck();
Quackable mallard4 = duckFactory.createMallardDuck();
flockOfMallards.add(mallard1);
flockOfMallards.add(mallard2);
flockOfMallards.add(mallard3);
flockOfMallards.add(mallard4);
flockOfDucks.add(flockOfMallards);
System.out.println("With Observer");
// 创建一个 Quackologist 把它注册成一个群的观察者
Quackologist quackologist = new Quackologist();
flockOfDucks.registerObserver(quackologist);
System.out.println("Whole Flock Simulation");
simulate(flockOfDucks);
System.out.println("Mallard Flock Simulation");
simulate(flockOfMallards);
System.out.println("Ducks quacked " + QuackCounter.getQuacks() + " times");
}
public void simulate(Quackable quack) {
quack.quack();
}
public static void main(String[] args) {
DuckSimulator simulator = new DuckSimulator();
AbstractDuckFactory duckFactory = new CountingDuckFactory();
new DuckSimulator().simulate(duckFactory);
}
}