以下图表使用 ASCIIFlow Infinity 绘制。
模板方法
推迟
+----------------------+ | AbstractClass | +---------------------+ +----------------------+ | def template_method | | template_method() +---+ operation1() | | | | operation2() | | operation1() | | operation3() | | operation2() | | end | | operation3() | +---------------------+ +---------+------------+ ^ | +---------+--------------------------+ | | +------+------------+ +-------+------------+ | ConcreteClass1 | | ConcreteClass2 | +-------------------+ +--------------------+ | operation1() | | operation1() | | operation2() | | operation2() | | operation3() | | operation3() | +-------------------+ +--------------------+
策略模式
策略对象知道一切
+----------------+ | Context | +----------------+ +---------------+ | @strategy +---> | Strategy | +----------------+ +---------------+ | operation() | +-------+-------+ ^ | +----------+---------------+ | | +------+-------+ +-------+------+ | Stragegy1 | | Stragegy2 | +--------------+ +--------------+ | operation() | | operation() | +--------------+ +--------------+
观察器模式
解耦,消息
+--------------------+ | Subject | +--------------------+ +-----------------+ | @observers[] +--> | Observer | +--------------------+ +-----------------+ | add_observer(o) | | update(subject) | | remove_observer(o) | +--------+--------+ | notify_observer() | ^ +--------------------+ | +--------+--------+ | Employee | +-----------------+ | update(subject) | +-----------------+
组合模式
子任务 -> 目标,简单 -> 复杂。
+-------------+ | Component | <---------------------+ +-------------+ | | operation() | | +--------+----+ | ^ | | | +------------+-------------+ | | | | +-----+-------+ +------+-----------+ | | Leaf | | Composite | | +-------------+ +------------------+ | | operation() | | @subcomponents[] +--+ +-------------+ +------------------+ | operation() | +------------------+
命令模式
知道如何完成某件事
+-------------+ | Command | +-------------+ | execute() | +------+------+ ^ | +----------+-----------+ | | +---------+--------+ +---------+--------+ | ConcreteCommand1 | | ConcreteCommand2 | +------------------+ +------------------+ | execute() | | execute() | +------------------+ +------------------+
适配器模式
翻译
+--------+ +--------+ | Client +-------> | Target | +--------+ +--------+ | | | | +--------+ +----+---+ ^ | +----+----+ +---------+ | Adapter +------> | Adaptee | +---------+ +---------+ | | | | +---------+ +---------+
代理模式
控制
+----------------+ | Service | +----------------+ | do_something() | +-------+--------+ ^ | +------------+-------------+ | | +------+---------+ +--------+-------+ | RealService | | ServiceProxy | +----------------+ +----------------+ | do_something() | | @subject | +-------------------------+ +----------------+ +----------------+ | def do_something | | do_something() +-------+ @subject.do_something | +----------------+ | end | +-------------------------+
装饰器模式
叠加
+----------------+ | Component | <--------------+ +----------------+ | | operation() | | +-------+--------+ | ^ | | | +------------+-------------+ | | | | +---------+---------+ +--------+--------+ | | ConcreteComponent | | Decorator | | +-------------------+ +-----------------+ | | operation() | | @component +--+ +-------------------+ +-----------------+ | operation() | | new_operation() | +-----------------+
工厂方法模式
+---------+ +------------------+ | Product | | Creator | +---------+ +------------------+ | | | factory_method() | +----+----+ +-------+----------+ ^ ^ | | +---------+---------+ +---------+----------+ | | | | | +-----+----+ +---------+--------+ | | | Product1 | | ConcreteCreator1 | | | +----------+ +------------------+ | | | | <---+ factory_method() | | | +----------+ +------------------+ | +----+-----+ +---------+--------+ | Product2 | | ConcreteCreator2 | +----------+ +------------------+ | | <-------------------------------------------+ factory_method() | +----------+ +------------------+
抽象工厂模式
共存对象组合
+--------------------+ | AbstractFactory | +--------------------+ | create_procduct1() | | create_procduct2() | +---------+----------+ ^ | +---------+----------+ | | +---------+--------+ +-------+----------+ | ConcreteFactoryA | |ConcreteFactoryB | +------------------+ +------------------+ | create_product1()| |create_product1() | | create_product2()| |create_product2() | +--+--------------++ ++---------------+-+ | | | | v v v v +--------+-+ +---------+-+ +-+---------+ +--+--------+ | Product1 | | ProductA2 | | ProductB1 | | ProductB2 | +----------+ +-----------+ +-----------+ +-----------+
生成器模式
+----------+ +-------------+ | Director +-------> | Builder | +----------+ +-------------+ | add_part1() | | add_part2() | | add_part3() | +---------+ | result() +---> | Product | +-------------+ +---------+
多态生成器
+----------+ +------------------+ | Director +-------> | ComputerBuilder | +----------+ +------------------+ | add_dvd() | | add_cd() | | add_hard_drive() | | computer() | +---------+--------+ ^ | +-------------+------------+ | | +----------+-------+ +--------+---------+ | LaptopBuilder | | DesktopBuilder | +------------------+ +------------------+ | add_dvd() | | add_dvd() | | add_cd() | | add_cd() | | add_hard_drive() | | add_hard_drive() | | computer() | | computer() | +--------+---------+ +----------+-------+ | | v v +--------+---------+ +----------+-------+ | LaptopComputer | | DesktopComputer | +------------------+ +------------------+
解释器模式
专门的语言,AST
+---------+ +--------------------+ | Client | | Expression | +---------+ +--------------------+ | +---------------------> | interpret(context) | <--------------------+ +----+----+ +----------+---------+ | | ^ | | | | v +-------------+------------+ | | | | +---------+ +----------+---------+ +--------+--------------+ | | Context | | TerminalExpression | | NonterminalExpression | | +---------+ +--------------------+ +-----------------------+ | | | | interpret(context) | | @sub_expressions[] +----+ +---------+ +--------------------+ +-----------------------+ | interpret(context) | +-----------------------+