1.中介者模式
package com.jmj.pattern.mediator;public abstract class Mediator {public abstract void constact(String message,Person person);
}
package com.jmj.pattern.mediator;public class MediatorStructure extends Mediator{private HouseOwner houseOwner;private Tenant tenant;public HouseOwner getHouseOwner() {return houseOwner;}public void setHouseOwner(HouseOwner houseOwner) {this.houseOwner = houseOwner;}public Tenant getTenant() {return tenant;}public void setTenant(Tenant tenant) {this.tenant = tenant;}@Overridepublic void constact(String message, Person person) {if (person==houseOwner){tenant.getMessage(message);}else{houseOwner.getMessage(message);}}}
package com.jmj.pattern.mediator;public class Person {protected String name;protected Mediator mediator;public Person(String name, Mediator mediator) {this.name = name;this.mediator = mediator;}
}
package com.jmj.pattern.mediator;public class HouseOwner extends Person{public HouseOwner(String name, Mediator mediator) {super(name, mediator);}public void constact(String message){mediator.constact(message,this);}public void getMessage(String message){System.out.println("房主"+name+"获取到的信息是: "+message);}
}
package com.jmj.pattern.mediator;public class Tenant extends Person{public Tenant(String name, Mediator mediator) {super(name, mediator);}//和中介联系public void constact(String message){mediator.constact(message,this);}public void getMessage(String message){System.out.println("租房者"+name+"获取到的信息是: "+message);}
}
package com.jmj.pattern.mediator;public class Client {public static void main(String[] args) {MediatorStructure mediatorStructure = new MediatorStructure();Tenant tenant = new Tenant("李四", mediatorStructure);HouseOwner houseOwner = new HouseOwner("张三", mediatorStructure);mediatorStructure.setTenant(tenant);mediatorStructure.setHouseOwner(houseOwner);tenant.constact("我要租三室的房子");houseOwner.constact("我这里有三室的房子,你要租吗?");}
}
2.迭代器模式
3.访问者模式
4.备忘录模式
5.解释器模式
触碰到编译原理,和二叉树先序遍历
package com.jmj.pattern.interpreter;public abstract class AbstractExpression {public abstract int interpret(Context context);
}
package com.jmj.pattern.interpreter;import java.util.HashMap;
import java.util.Map;public class Context {private Map<Variable, Integer> map = new HashMap<>();public void assign(Variable var, Integer value) {map.put(var, value);}public int getValue(Variable variable) {return map.get(variable);}
}
package com.jmj.pattern.interpreter;/*** 加法表达式类*/
public class Minus extends AbstractExpression{private AbstractExpression left;private AbstractExpression right;public Minus(AbstractExpression left, AbstractExpression right) {this.left = left;this.right = right;}@Overridepublic int interpret(Context context) {//将左边表达式的结果和右边表达式的结果进行相加return left.interpret(context)-right.interpret(context);}@Overridepublic String toString() {return "("+left+"-"+right+")";}
}
package com.jmj.pattern.interpreter;/*** 加法表达式类*/
public class Plus extends AbstractExpression{private AbstractExpression left;private AbstractExpression right;public Plus(AbstractExpression left, AbstractExpression right) {this.left = left;this.right = right;}@Overridepublic int interpret(Context context) {//将左边表达式的结果和右边表达式的结果进行相加return left.interpret(context)+right.interpret(context);}@Overridepublic String toString() {return "("+left+"+"+right+")";}
}
package com.jmj.pattern.interpreter;public class Variable extends AbstractExpression{//声明存储变量名的成员变量private String name;public Variable(String name) {this.name = name;}@Overridepublic int interpret(Context context) {//直接返回变量的值return context.getValue(this);}@Overridepublic String toString() {return name ;}
}
package com.jmj.pattern.interpreter;public class Client {public static void main(String[] args) {//创建环境对象Context context = new Context();Variable a = new Variable("a");Variable b = new Variable("b");Variable c = new Variable("c");Variable d = new Variable("d");//context.assign(a,1);context.assign(b,2);context.assign(c,3);context.assign(d,4);AbstractExpression expression=new Minus(a,new Plus(new Minus(b,c),d));int interpret = expression.interpret(context);System.out.println(expression+"="+interpret);}
}