zoukankan      html  css  js  c++  java
  • 设计模式5---装饰模式

      问题:人要穿衣服,可以穿鞋子,裤子,领带。。。,顺序可以使随机的。

      也就是包含大量随机的操作,但是我们的结果却需要一个明确的顺序去执行这个方法。

      UML图:

    该方法的变种是,可以Decorate直接继承自RealComponent。

    上图的方法可以通过接口来操作,符合开闭原则。

    这样所有实现component接口的类都可以使用Decorate类处理。

     已穿衣服为例:

    component接口:

    public interface IComponent {
        void show();
    }

    具体实现类:

    public class Person implements IComponent {
        
        private String name;
        
        public Person(String name)
        {
            this.name = name;
        }
        @Override
        public void show() {
            System.out.println("穿衣服的"+name);
        }
    
    }

    装饰抽象类:

    public abstract class Chothes implements IComponent {
        protected IComponent _mComponent = null;
        
        public void show()
        {
            if(_mComponent!=null)
            {
                _mComponent.show();
            }
        }
        
        public void decorate(IComponent comp)
        {
            _mComponent = comp;
        }
    }

    通过decorate方法,可以把其他Chothes 的子类传入。

    Chothes装饰功能类:

    public class Westernstyleclothes extends Chothes {
        
        @Override
        public void show() {
            // TODO Auto-generated method stub
            System.out.print("西装	");
            super.show();
        }
    
    }
    public class Tie extends Chothes {
    
        /* (non-Javadoc)
         * @see com.jayfulmath.designpattern.decorate.Chothes#show()
         */
        @Override
        public void show() {
            System.out.print("领带	");
            super.show();
        }
        
    }
    public class Pants extends Chothes {
    
        /* (non-Javadoc)
         * @see com.jayfulmath.designpattern.decorate.Chothes#show()
         */
        @Override
        public void show() {
            System.out.print("西裤	");
            super.show();
        }
        
    }
    public class LeatherShoes extends Chothes {
    
        /* (non-Javadoc)
         * @see com.jayfulmath.designpattern.decorate.Chothes#show()
         */
        @Override
        public void show() {
            System.out.print("皮鞋	");
            super.show();
        }
        
    }

    main函数:

    public class DecorateMain extends BasicExample {
    
        @Override
        public void startDemo() {
            Person mPerson = new Person("小明");
            
            Westernstyleclothes wl = new Westernstyleclothes();
            Pants pt = new Pants();
            Tie ti = new Tie();
            LeatherShoes ls = new LeatherShoes();
            
            ti.decorate(mPerson);
            ls.decorate(ti);
            pt.decorate(ls);
            wl.decorate(pt);
            
            wl.show();
        }
    
    }

    如上,可以任意更换顺序,但不需改变其他代码,即可实现穿衣服流程的变化。

  • 相关阅读:
    JAVA_集合_作业01
    Java_Objects_hashCode
    Java成员内部类
    Java集合练习_实现购物车需求
    Java初级_单例设计模式
    HelloWorld
    vue中视频标点
    防抖节流?俩者区别?vue如何使用防抖、节流来解决点击问题?
    keep-alive
    axios和ajax的区别
  • 原文地址:https://www.cnblogs.com/deman/p/4128113.html
Copyright © 2011-2022 走看看