zoukankan      html  css  js  c++  java
  • 装饰模式

    装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案

    一.UML示意图


    二.组成部分

    1.       抽象构件:原始的功能接口

    2.       具体构件:具体的原始功能类

    3.       装饰角色:持有具体构件类的对象,以便执行原有功能

    4.       具体装饰:具体扩展的功能在这里

    三.例子代码:使用装饰模式来演示一个对行走功能扩展的例子(听音乐+行走和唱歌+行走)

    1 抽象构件

    package com.eekq.decorator;

     

    publicinterface Component {

        /**原始接口*/

        publicvoid go();

    }

    2 具体构件

    package com.eekq.decorator;

     

    publicclass ConcreteComponent implements Component {

     

        publicvoid go() {    

            System.out.println("行走");

        }

     

    }

    3.装饰角色来了

    package com.eekq.decorator;

     

    publicclass Decorator implements Component {

        /**持有私有的原始构件*/

        private Component component;

     

        /**构造子,委派给原始构件*/

        protected Decorator(Component component) {

            this.component = component;

        }

     

        /**调用原始构件功能,通常就可直接把扩展功能加在此方法中*/

        publicvoid go() {

            this.component.go();

        }

     

    }

    4.具体装饰(这里演示了两种扩展的情况,走路+听音乐和唱歌s

    (1).

    package com.eekq.decorator;

     

    publicclass ConcreteDecoratorListen extends Decorator {

     

        /**构造子,相关初始化*/

        public ConcreteDecoratorListen(Component component) {

            super(component);

            // code is here

        }

     

        /**商业逻辑,对原始构件功能的扩展*/

        publicvoid go() {    

            listen("听音乐");//执行扩展功能

            super.go();

        }

       

        privatevoid listen(Object obj){

            System.out.println(obj);       

        }

     

    }

    (2).

    package com.eekq.decorator;

     

    publicclass ConcreteDecoratorSing extends Decorator {

     

        /**构造子,相关初始化*/

        public ConcreteDecoratorSing(Component component) {

            super(component);

            // code is here

        }

     

        /**商业逻辑,对原始构件功能的扩展*/

        publicvoid go() {    

            super.go();

            System.out.println(sing());;// 执行扩展功能

        }

     

        private String sing() {

            return"唱歌";

        }

    }

    5.客户端调用

    package com.eekq.decorator;

     

    publicclass Main {

     

        publicstaticvoid main(String[] args) {

            /**原始构件*/

            Component component = new ConcreteComponent();

            /**边听音乐,边走路*/

            ConcreteDecoratorListen cdl = new ConcreteDecoratorListen(component);

            cdl.go();

            System.out.println();

            /**边走路,边唱歌*/

            ConcreteDecoratorSing cds = new ConcreteDecoratorSing(component);

            cds.go();

        }

     

    }

     

  • 相关阅读:
    shell命令--chattr
    OCA读书笔记(1)
    shell命令--tree
    网络知识汇总(1)-朗文和牛津英语词典网址
    shell命令--touch
    CCNP交换实验(7) -- NAT
    shell命令--rm
    CCNP交换实验(6) -- NTP
    shell命令--pwd
    CCNP交换实验(5) -- 网关热备冗余
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1934314.html
Copyright © 2011-2022 走看看