一, 中介者模式(meditor)
[中介]
- /***
- * 抽象中介者接口和其具体实现类 "经理" 类
- */
- package cn.sxt.meditor;
- import java.util.HashMap;
- import java.util.Map;
- public interface Mediator {
- void register(String dName,Department d);// 自己部门名字
- void command(String dName);// 向别的部门发布命令
- }
- // 经理类
- class Manger implements Mediator{
- private Map<String, Department> map=new HashMap<String, Department>();
- public void register(String dName, Department d) {
- map.put(dName, d);
- }
- public void command(String dName) {
- map.get(dName).selfAction();// 调用财务部回到这里. map 调用传进来的部门的本来职责 selfAction()
- }
- }
[同事]
- /***
- * "同事" 类的接口 和 3 个同事类
- */
- package cn.sxt.meditor;
- public interface Department {
- void selfAction();// 做本部门的事情
- void outAction();// 向总经理发出申请
- }
- // 研发部
- class Development implements Department{
- private Mediator mediator;// 持有中介者 (这里指的是总经理, 得知道总经理是谁) 的引用
- public Development(Mediator mediator) {
- super();
- this.mediator = mediator;
- mediator.register("研发部", this);// 当前对象 (研发部) 报给中介者 (总经理) 知晓
- }
- public void selfAction() {
- System.out.println("专心科研, 研发项目!");
- }
- public void outAction() {
- System.out.println("汇报工作: 需要资金支持!");
- }
- }
- // 财务部
- class Financial implements Department{
- private Mediator mediator;// 持有中介者 (这里指的是总经理, 得知道总经理是谁) 的引用
- public Financial(Mediator mediator) {
- super();
- this.mediator = mediator;
- mediator.register("财务部", this);// 当前对象 (研发部) 报给中介者 (总经理) 知晓
- }
- public void selfAction() {
- System.out.println("对内发工资!");
- }
- public void outAction() {
- System.out.println("汇报工作: 没钱了, 债主上门了!");
- }
- }
- // 市场部
- class Market implements Department{
- private Mediator mediator;// 持有中介者 (这里指的是总经理, 得知道总经理是谁) 的引用
- public Market(Mediator mediator) {
- super();
- this.mediator = mediator;
- mediator.register("市场部", this);// 当前对象 (研发部) 报给中介者 (总经理) 知晓
- }
- public void selfAction() {
- System.out.println("开拓国内市场!");
- }
- public void outAction() {
- System.out.println("汇报工作: 市场不好开展呀!");
- mediator.command("财务部");// 市场部去调用财务部
- }
- }
[客户]
- /**
- *
- */
- package cn.sxt.meditor;
- public class Client {
- public static void main(String[] args) {
- Mediator mediator=new Manger();
- Market market=new Market(mediator);// 把中介者传进去
- Development development=new Development(mediator);
- Financial financial=new Financial(mediator);
- market.selfAction();// 市场部自己的职责
- market.outAction();// 市场部去找经理要钱, 经理找财务部
- }
- }
二, 命令模式(command) 用的很少
[命令]
- /**
- *
- */
- package cn.sxt.command;
- public interface Command {
- // 实际项目中根据需求设计多个不同的方法
- void execute();
- }
- class ConcreteCommand implements Command{
- private Receiver receiver;// 命令真正的执行者对象
- public ConcreteCommand(Receiver receiver) {
- super();
- this.receiver = receiver;
- }
- public void execute() {
- receiver.action();
- }
- }
[接收者]
- /***
- * 命令真正的执行者 . 广大的指战员们
- */
- package cn.sxt.command;
- public class Receiver {
- public void action() {
- System.out.println("4 月 21 日渡江!");
- }
- }
[发起者]
- /***
- * 命令的发起者
- */
- package cn.sxt.command;
- public class Invoke {
- private Command command; // 可以写多条, 采用容器
- public Invoke(Command command) {
- super();
- this.command = command;
- }
- // 用于调用命令类的方法
- public void call() {
- command.execute();
- }
- }
[客户]
- /**
- * 客户端
- */
- package cn.sxt.command;
- public class Client {
- public static void main(String[] args) {
- Command command=new ConcreteCommand(new Receiver());
- Invoke invoke=new Invoke(command);
- invoke.call();
- }
- }
三, 解释器模式(Interpreter) 屠龙之技
MESP 的网址: http://sourceforge.net/projects/expression-tree/
Expression4J 的网址: http://sourceforge.net/projects/expression4j/
四, 访问者模式
[19/04/30 - 星期二] GOF23_行为型模式(中介者模式, 命令模式, 解释器模式, 访问者模式)
来源: http://www.bubuko.com/infodetail-3040134.html