1. 程式人生 > >[19/04/30-星期二] GOF23_行為型模式(中介者模式、命令模式、解釋器模式、訪問者模式)

[19/04/30-星期二] GOF23_行為型模式(中介者模式、命令模式、解釋器模式、訪問者模式)

調用 匯報 需求 med ssi client 執行者 pack part

一、中介者模式(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_行為型模式(中介者模式、命令模式、解釋器模式、訪問者模式)