一、备忘录模式
用一个中介对象封装一系列交互的对象,使得各个对象不需要显示的交互引用,松散耦合,属于行为模式。

发起方:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能。
- class MAP{
- String longitude;
- String latitude;
-
- public String getLongitude() {
- return longitude;
- }
-
- public void setLongitude(String longitude) {
- this.longitude = longitude;
- }
-
- public String getLatitude() {
- return latitude;
- }
-
- public void setLatitude(String latitude) {
- this.latitude = latitude;
- }
-
- public Memento addSign(){
- return new Memento(longitude, latitude);
- }
-
- public void recover(Memento memento){
- this.latitude = memento.getLatitude();
- this.longitude = memento.getLongitude();
- }
- }
备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
- class Memento{
- String longitude;
- String latitude;
-
- public Memento(String longitude, String latitude) {
- this.longitude = longitude;
- this.latitude = latitude;
- }
-
- public String getLongitude() {
- return longitude;
- }
-
- public void setLongitude(String longitude) {
- this.longitude = longitude;
- }
-
- public String getLatitude() {
- return latitude;
- }
-
- public void setLatitude(String latitude) {
- this.latitude = latitude;
- }
- }
管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
- class CareTaker{
- List<Memento> mementoList = new ArrayList<>();
-
- public void addMemento(Memento memento){
- mementoList.add(memento);
- }
-
- public Memento getMemento(int index){
- return mementoList.get(index);
- }
- }
客户端调用:
- MAP map = new MAP();
- map.setLatitude("108");
- map.setLongitude("72");
-
- CareTaker careTaker = new CareTaker();
- careTaker.addMemento(map.addSign());
-
- map.setLatitude("0");
- map.setLongitude("0");
- careTaker.addMemento(map.addSign());
-
- Memento memento = careTaker.getMemento(0);
- map.recover(memento);
二、中介者模式
又叫调停模式,它定义了一个中介角色来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。

Mediator: 中介者的接口,由具体中介者角色来实现。
MediatorStruct: 实现中介者接口。
Consumer:同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。
RichPeople,PoorPeople: 抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。
- abstract class Consumer{
- protected String name;
- protected Mediator mediator;
-
- public Consumer(String name, Mediator mediator){
- this.name = name;
- this.mediator = mediator;
- }
- }
-
- class PoorPeople extends Consumer{
- public PoorPeople(String name, Mediator mediator) {
- super(name, mediator);
- }
-
- public void contact(){
- this.mediator.contact("穷人", this);
- }
- }
-
- class RichPeople extends Consumer{
- public RichPeople(String name, Mediator mediator) {
- super(name, mediator);
- }
-
- public void contact(){
- this.mediator.contact("富人", this);
- }
- }
- abstract class Mediator{
- abstract void contact(String meaasge, Consumer consumer);
- }
-
- class MediatorStruct extends Mediator{
- PoorPeople poorPeople;
- RichPeople richPeople;
-
- public void setPoorPeople(PoorPeople poorPeople) {
- this.poorPeople = poorPeople;
- }
-
- public void setRichPeople(RichPeople richPeople) {
- this.richPeople = richPeople;
- }
-
- @Override
- void contact(String meaasge, Consumer consumer) {
- if(consumer == richPeople){
- System.out.println("富人发来了消息");
- }else {
- System.out.println("穷人发来了消息");
- }
- }
- }
客户端调用
- MediatorStruct mediatorStruct = new MediatorStruct();
- PoorPeople poorPeople = new PoorPeople("张三", mediatorStruct);
- RichPeople richPeople = new RichPeople("李四", mediatorStruct);
-
- mediatorStruct.setPoorPeople(poorPeople);
- mediatorStruct.setRichPeople(richPeople);
-
- poorPeople.contact();
- richPeople.contact();
三、解释器模式
给定一个语言表达式,定义它的文法,并定义一个解释器,使用该解释器解释语言中的句子。

AbstractExpression: 抽象表达式接口。
Variable: 终结符表达式类。返回具体字符对应的值。
Plus: 非终结符表达式。用于计算两个变量进行相加的操作。
Context: 上下文环境。Map集合里面存储的就是变量以及变量对应的值。此外,我们还在该类里面定义了两个方法,一个是assign,该方法是用来向Map集合里面添加变量及其对应的值的;一个是getValue,该方法就是根据变量来获取变量对应的值的。
- interface AbstractExpression {
- int interpret(Context context);
- }
-
- class Plus implements AbstractExpression{
- AbstractExpression left;
- AbstractExpression right;
-
- @Override
- public int interpret(Context context) {
- return left.interpret(context) + right.interpret(context);
- }
-
- public Plus(AbstractExpression left, AbstractExpression right) {
- this.left = left;
- this.right = right;
- }
- }
-
- class Variable implements AbstractExpression{
- String name;
- @Override
- public int interpret(Context context) {
- return context.getValue(this);
- }
-
- public Variable(String name) {
- this.name = name;
- }
- }
- class Context{
- private Map<AbstractExpression, Integer> map = new HashMap<>();
-
- public void assign(AbstractExpression variable, Integer number){
- map.put(variable, number);
- }
-
- public Integer getValue(AbstractExpression variable){
- return map.get(variable);
- }
- }
客户端调用:
- Context context = new Context(); // 下面的操作是添加映射关系
- Variable a = new Variable("a");
- Variable b = new Variable("b");
- Variable c = new Variable("c");
- context.assign(a, 1);
- context.assign(b, 2);
- context.assign(c, 3);
-
- Plus plus = new Plus(c, new Plus(a, b));
- int interpret = plus.interpret(context);
- System.out.println(interpret);
四、状态模式
用来解决对象在多个状态之间进行转换,对外界输出不同的行为状态,显著的优点是方便维护并且符合开闭原则。

BoxState: 抽象类定义了各个状态的变化操作,并提供一个set方法。
BoxOpen: 盒子打开的状态,继承并重写状态对应的方法。
BoxClose: 盒子关闭的状态,继承并重写状态对应的方法。
Boxer:状态模式里面的环境角色,state变量是用来记录当前的状态。
- abstract class BoxState{
- Boxer boxState;
- public void setBoxState(Boxer boxer){
- this.boxState = boxer;
- }
- abstract void open();
- abstract void close();
- }
-
- class BoxOpen extends BoxState{
- @Override
- public void open() {
- System.out.println("打开盒子");
- }
-
- @Override
- public void close() {
- this.boxState.setState(Boxer.CLOSE_STATE);
- this.boxState.close();
- }
- }
-
- class BoxClose extends BoxState{
- @Override
- public void open() {
- this.boxState.setState(Boxer.OPENING_STATE);
- this.boxState.open();
- }
-
- @Override
- public void close() {
- System.out.println("关上盒子");
- }
- }
- class Boxer{
- public final static BoxOpen OPENING_STATE = new BoxOpen();
- public final static BoxClose CLOSE_STATE = new BoxClose();
- BoxState state;
-
- public BoxState getState() {
- return state;
- }
-
- public void setState(BoxState state) {
- this.state = state;
- this.state.setBoxState(this);
- }
-
- public void open(){
- this.state.open();
- }
-
- public void close(){
- this.state.close();
- }
- }
客户端调用:
- Boxer boxer = new Boxer();
- boxer.setState(new BoxOpen());
- boxer.open();
- boxer.close();