zoukankan      html  css  js  c++  java
  • 使用组合的方式来创建新类, 并可以重新定制某些类的特定行为

    class Action
    {
        private    OnEnterListener    enterListener;
        private OnLeaveListener leaveListener;
    
        public Action(OnEnterListener enterListener)
        {
            this.enterListener = enterListener;
        }
        
        public Action(OnLeaveListener leaveListener)
        {
            this.leaveListener = leaveListener
        }    
    
        public interface OnEnterListener
        {
            void onEnter();
        }
        
        public interface OnLeaveListener
        {
            void onLeave();
        }
    }
    
    class Persone implements Action.OnEnterListener, Action.OnLeaveListener
    {
        /*
        使用组合的方式定义一个新类,而且可以对特定的对象的特定行为进行重写
        */
    
        private Action    action;
        
        public Persone()
        {
            action = new Action(this);
        }
        
        public void onEnter()
        {
            
        }
        
        public void onLeave()
        {
        
        }
    }

    上面是在代码中看到的设计模式

  • 相关阅读:
    Java修饰符大汇总
    死锁
    线程的几种可用状态
    重载与覆盖(重写)
    Git
    JS跨域
    Spring中的Bean
    ZooKeeper
    Mysql(2)
    Maven
  • 原文地址:https://www.cnblogs.com/emyueguang/p/5009222.html
Copyright © 2011-2022 走看看