委派模式

作者: 愤怒的奶牛 | 来源:发表于2019-08-23 23:29 被阅读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());
        }
    }
    
    

    相关文章

      网友评论

        本文标题:委派模式

        本文链接:https://www.haomeiwen.com/subject/yuwxectx.html