略读设计模式(四)

命令模式

abstract class Command {
    public abstract void execute();
}

class Invoker {
    private Command command;
    public Invoker(Command command) {
        this.command = command;
    }
    public void setCommand(Command command) {
        this.command = command;
    }
    public void call() {
        command.execute();
    }
}

class ConcreteCommand extends Command {
    private Receiver receiver;
    public void execute() {
        receiver.action();
    }
}

class Receiver {
    public void action() {
    }
}

迭代器模式:

interface Iterator {
    public void first();
    public void next();
    public boolean hasNext();
    public Object currentItem();
}

class ConcreteIterator implements Iterator {
    private ConcreteAggregate objects;
    private int cursor;
    public ConcreteIterator(ConcreteAggregate objects) {
        this.objects=objects;
    }
    public void first() {}
    public void next() {}
    public boolean hasNext() {}
    public Object currentItem() {}
}

interface Aggregate {
    Iterator createIterator();
}

class ConcreteAggregate implements Aggregate {
    public Iterator createIterator() {
        return new ConcreteIterator(this);
    }
}

书籍地址:http://gof.quanke.name/

0

一个有关“略读设计模式(四)”的想法

    1. 我靠,你这话说的。最近工作朝九晚六,晚上没事干,就只能看点书了。。。。

      0

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.