设计模式

委派模式

2019-08-23  本文已影响0人  愤怒的奶牛

什么是委派模式:老板将任务经理,经理将任务派发给员工。

public interface IEmployee {
    void doing(String command);
}
public class IEmployeeA implements IEmployee{
    @Override
    public void doing(String command) {
        System.out.println("IEmployeeA doing");
    }
}
public class IEmployeeB implements IEmployee {
    @Override
    public void doing(String command) {
        System.out.println("IEmployeeB doing..");
    }
}
public class Leader implements IEmployee{

    private Map<String,IEmployee> iEmployeeMap = new ConcurrentHashMap<>();

    {
        iEmployeeMap.put("A", new IEmployeeA());
        iEmployeeMap.put("B", new IEmployeeB());
    }
    @Override
    public void doing(String command) {
        iEmployeeMap.get(command).doing(command);
    }
}
public class Boss {

    /**
     * 任务分发
     * @param command
     * @param leader
     */
    public void command(String command, Leader leader) {
        leader.doing(command);
    }

    public static void main(String[] args) {
        new Boss().command("A",new Leader());
    }
}

上一篇 下一篇

猜你喜欢

热点阅读