zoukankan      html  css  js  c++  java
  • CommandPattern

    /**
     * 命令模式
     * @author TMAC-J
     * 将调用者和接受者分离
     * 可以将一组命令组合在一起,适合很多命令的时候
     */
    public class CommandPattern {
        
        interface Command{
            void excute();
        }
        
        public class TVReceiver{
            
            public void shift(){
                System.out.println("shift");
            }
            
            public void turnon(){
                System.out.println("turnon");
            }
            
            public void Turndown(){
                System.out.println("Turndown");
            }
            
        }
        
        public class ShiftTV implements Command{
    
            private TVReceiver tv;
            
            public ShiftTV(TVReceiver tv) {
                this.tv = tv;
            }
            
            @Override
            public void excute() {
                tv.shift();
            }
            
        }
        
        public class TurnonTV implements Command{
            
            private TVReceiver tv;
            
            public TurnonTV(TVReceiver tv) {
                this.tv = tv;
            }
            
            @Override
            public void excute() {
                tv.turnon();
            }
            
        }
        public class Turndown implements Command{
            
            private TVReceiver tv;
            
            public Turndown(TVReceiver tv) {
                this.tv = tv;
            }
            
            @Override
            public void excute() {
                tv.Turndown();
            }
            
        }
        
        public class Invoker{
            
            private Command shiftTv,turnon,turndown;
            
            public Invoker(Command shiftTv,Command turnonTV,Command turndown) {
                this.shiftTv = shiftTv;
                this.turnon = turndown;
                this.turndown = turndown;
            }
            
            public void shift(){
                shiftTv.excute();
            }
    
            public void turnon(){
                turnon.excute();
            }
            
            public void turndown(){
                turndown.excute();
            }
        }
        
        public class Client{
            public void test(){
                TVReceiver tv = new TVReceiver();
                Invoker invoker = new Invoker(new ShiftTV(tv), new TurnonTV(tv), new Turndown(tv));
                invoker.shift();
                invoker.turnon();
                invoker.turndown();
            }
        }
        
        
    }
  • 相关阅读:
    uva 10881
    uva 1388
    【USACO 3.2.5】魔板
    【USACO 3.2.4】饲料调配
    【USACO 3.2.3】纺车的轮子
    【USACO 3.2.2】二进制数01串
    【USACO 3.2.1】阶乘
    【USACO 3.1.6】邮票
    【USACO 3.1.5】联系
    【USACO 3.1.4】形成的区域
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6233593.html
Copyright © 2011-2022 走看看