设计模式(3)
2017-08-25 00:00
330 查看
摘要: 行为型(1)
解释器模式可以用在复杂的计算方式, 适合于开发编译器和解释器.
设计模式
行为型模式
解释器模式
Context是一个上下文环境类,
Plus和
Minus分别是用来计算的实现.
解释器模式可以用在复杂的计算方式, 适合于开发编译器和解释器.
interface Expression { int interpret(Context context); } class Context { private int numOne, numTwo; public Context(int numOne, int numTwo) { this.numOne = numOne; this.numTwo = numTwo; } public int getNumOne() { return numOne; } public void setNumOne(int numOne) { this.numOne = numOne; } public int getNumTwo() { return numTwo; } public void setNumTwo(int numTwo) { this.numTwo = numTwo; } } class Minus implements Expression { @Override public int interpret(Context context) { return context.getNumOne() - context.getNumTwo(); } } class Plus implements Expression { @Override public int interpret(Context context) { return context.getNumOne() + context.getNumTwo(); } } public class Main { public static void main(String[] args) { Context context = new Context(10, 5); int result = new Plus().interpret( new Context(new Minus().interpret(context), new Plus().interpret(context))); System.out.println(result); } }
命令模式
将调用者, 接收者, 命令三者互相解除类间的耦合度.interface Command { void exec(); } interface Receiver { void startup(); void shutdown(); } class MySQLReceiver implements Receiver { @Override public void startup() { System.out.println("MySQL startup..."); } @Override public void shutdown() { System.out.println("MySQL shutdown..."); } } class OracleReceiver implements Receiver { @Override public void startup() { System.out.println("Oracle startup..."); } @Override public void shutdown() { System.out.println("Oracle shutdown..."); } } class CommandStartup implements Command { private Receiver receiver; public CommandStartup(Receiver receiver) { this.receiver = receiver; } @Override public void exec() { receiver.startup(); } } class CommandShutdown implements Command { private Receiver receiver; public CommandShutdown(Receiver receiver) { this.receiver = receiver; } @Override public void exec() { receiver.shutdown(); } } class Invoker { private Command command; public void setCommand(Command command) { this.command = command; } public void invoke() { command.exec(); } } public class Main { public static void main(String[] args) { Receiver receiver = new MySQLReceiver(); Command cmdStartup = new CommandStartup(receiver); Command cmdShutdown = new CommandShutdown(receiver); Invoker invoker = new Invoker(); invoker.setCommand(cmdStartup); invoker.invoke(); invoker.setCommand(cmdShutdown); invoker.invoke(); } }
迭代器模式
java集合类中使用的模式, 即使用迭代器对象来迭代容器对象.
import java.util.ArrayList; import java.util.List; interface Fruit { String getName(); } class Apple implements Fruit { @Override public String getName() { return "apple"; } } class Banana implements Fruit { @Override public String getName() { return "banana"; } } class FruitColletion { private List<Fruit> fruitList = new ArrayList<>(); public FruitIterator iterator() { return new FruitIterator(this); } public void add(Fruit fruit) { fruitList.add(fruit); } public void remove(Fruit fruit) { fruitList.remove(fruit); } public List<Fruit> getList() { return fruitList; } } class FruitIterator { private List<Fruit> fruitList; private int pos; public FruitIterator(FruitColletion collection) { fruitList = collection.getList(); } public Fruit prev() { if (pos > 0) { --pos; return fruitList.get(pos); } return null; } public Fruit next() { if (pos < fruitList.size()-1) { ++pos; return fruitList.get(pos); } return null; } public Fruit first() { pos = 0; return fruitList.get(pos); } public boolean hasNext() { if (pos >= fruitList.size()-1) { return false; } return true; } } public class Main { public static void main(String[] args) { Fruit appleOne = new Apple(); Fruit bananaOne = new Banana(); Fruit appleTwo = new Apple(); Fruit bananaTwo = new Banana(); FruitColletion collection = new FruitColletion(); collection.add(appleOne); collection.add(bananaOne); collection.add(bananaTwo); collection.add(appleTwo); FruitIterator iter = collection.iterator(); Fruit firstFruit = iter.first(); System.out.println(firstFruit.getName()); while (iter.hasNext()) { Fruit fruit = iter.next(); System.out.println(fruit.getName()); } } }
备忘录模式
备份状态 - 恢复状态, 常用于保存游戏, 加载存档等.import java.util.ArrayList; import java.util.List; class Computer { private String stat = "shutdown"; public Computer(String stat) { this.stat = stat; } public Memento createMemento() { return new Memento(stat); } public void restoreMemento(Memento memento) { this.stat = memento.getStat(); } public String getStat() { return stat; } public void setStat(String stat) { this.stat = stat; } } class Memento { private String stat; public Memento(String stat) { this.stat = stat; } public String getStat() { return stat; } } class Storage { private List<Memento> mementos = new ArrayList<>(); public void addMemento(Memento memento) { mementos.add(memento); } public void removeMemento(Memento memento) { mementos.remove(memento); } public Memento getMemento(int history) { if (history >= 0 || history < -mementos.size()+1) { history = 0; } int pos = mementos.size()-1 + history; return mementos.get(pos); } } public class Main { public static void main(String[] args) { Computer computer = new Computer("startup"); Memento mementoStartup = computer.createMemento(); computer.setStat("running"); Memento mementoRunning = computer.createMemento(); computer.setStat("shutdown"); Memento mementoShutdown = computer.createMemento(); Storage storage = new Storage(); storage.addMemento(mementoStartup); storage.addMemento(mementoRunning); storage.addMemento(mementoShutdown); computer.restoreMemento(storage.getMemento(0)); System.out.println(computer.getStat()); computer.restoreMemento(storage.getMemento(-1)); System.out.println(computer.getStat()); computer.restoreMemento(storage.getMemento(-2)); System.out.println(computer.getStat()); computer.restoreMemento(storage.getMemento(-3)); System.out.println(computer.getStat()); } }
状态模式
当对象的状态改变时, 同时改变其行为.class State { private String name; public State(String name) { this.name = name; } public void open() { System.out.println("open..."); } public void running() { System.out.println("running..."); } public void close() { System.out.println("close..."); } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Context { private State stat = new State("close"); public void exec() { String statName = stat.getName(); if (statName.equals("close")) { stat.setName("open"); stat.open(); } else if (statName.equals("open")) { stat.setName("running"); stat.running(); } else if (statName.equals("running")) { stat.setName("close"); stat.close(); } } public State getStat() { return stat; } public void setStat(State stat) { this.stat = stat; } } public class Main { public static void main(String[] args) { Context context = new Context(); context.exec(); context.exec(); context.setStat(new State("close")); context.exec(); } }
相关文章推荐
- 基于设计模式的学习之旅-----外观模式(附源码)
- 学习设计模式资料
- ZT 设计模式六大原则(6):开闭原则
- 设计模式---迭代器模式
- 设计模式学习之备忘录模式
- [初]设计模式之单例模式
- 设计模式—工厂模式
- C#设计模式(3)——工厂方法模式
- 设计模式C++实现二 ——策略模式
- java观察者设计模式
- 设计模式
- php 3种基本设计模式的组合使用
- 剖析Node.js异步编程中的回调与代码设计模式
- 设计模式--备忘录模式
- 设计模式(二)抽象工厂模式
- js的命名空间 && 单体模式 && 变量深拷贝和浅拷贝 && 页面弹窗设计
- 步步为营 .NET 设计模式学习笔记 三、Strategy(策略模式)
- 设计模式之State模式(学习笔记)
- 乐在其中设计模式(C#) - 享元模式(Flyweight Pattern)
- PHP设计模式(5)策略模式