zoukankan      html  css  js  c++  java
  • 第十三讲、装饰器模式

    1.定义

    装饰(Decorator)模式又叫做包装模式。通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案。

    2.结构

       

    3.装饰模式的角色和职责

    • 抽象组件角色:一个抽象接口,是被装饰类和装饰类的父接口。
    • 具体组件角色:为抽象组件的实现类。
    • 抽象装饰角色:包含一个组件的引用,并定义了与抽象组件一致的接口。
    • 具体装饰角色:为抽象装饰角色的实现类,负责具体的装饰。

    4.代码演示

    package test.com.decorator;
    /*
     * 抽象组件角色 component--装饰类的父接口
     */
    public interface Car {
        
        public void show();
    }
    package test.com.decorator;
    /*
     * 具体组件角色concreteComponent--抽象组件的实现类(封装公共的功能)
     */
    public class RunCar implements Car {
    
        @Override
        public void show() {
            this.run();
        }
        
        public void run() {
            System.out.println("车可以跑");
        }
    }
    package test.com.decorator;
    /*
     * 抽象装饰角色decorator--包含一个组件的引用,并定义了与抽象组件一致的接口
     */
    public abstract class CarDecorator implements Car{
        private Car car;
        
        public CarDecorator(Car car) {
            this.car = car;
        }
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
        
        public abstract void show();
    }
    package test.com.decorator;
    /*
     * 具体装饰角色concreteDecoratorC--抽象装饰角色的实现类,负责具体的装饰
     */
    public class RunCarDecorator extends CarDecorator {
    
        public RunCarDecorator(Car car) {
            super(car);
        }
    
        @Override
        public void show() {
            this.getCar().show();
        }
    
    }
    package test.com.decorator;
    /*
     * 具体装饰角色concreteDecoratorA--抽象装饰角色的实现类,负责具体的装饰
     */
    public class FlyCarDecorator extends CarDecorator {
    
        public FlyCarDecorator(Car car) {
            super(car);
        }
    
        @Override
        public void show() {
            this.getCar().show();
            this.fly();
        }
        
        public void fly() {
            System.out.println("车可以飞");
        }
    }
    package test.com.decorator;
    /*
     * 具体装饰角色concreteDecoratorB--抽象装饰角色的实现类,负责具体的装饰
     */
    public class SwimCarDecorator extends CarDecorator {
    
        public SwimCarDecorator(Car car) {
            super(car);
        }
    
        @Override
        public void show() {
            this.getCar().show();
            this.swim();
        }
        
        public void swim() {
            System.out.println("车可以游");
        }
    }
    package test.com.decorator;
    /*
     * 测试类
     */
    public class Main {
        public static void main(String[] args) {
            Car car = new RunCar();
            Car car1 = new RunCarDecorator(car);
            car1.show();
            System.out.println("*****************");
            
            Car car2 = new FlyCarDecorator(car);
            car2.show();
            System.out.println("*****************");
            
            Car car3 = new SwimCarDecorator(car);
            car3.show();
            System.out.println("*****************");
            
            Car car4 = new SwimCarDecorator(car2);
            car4.show();
        }
    }
  • 相关阅读:
    关于在php+apache开发过程中使用svn进行版本的维护
    Fragment的切换动画实现
    IOS MJExtension json转模型的轻量级框架的使用
    Centos 配置Red5流媒体服务器
    在Centos 6.5 上面配置 SVN
    在Centos 上面配置Openfire
    关于阿里云上面的Centos上面配置 防火墙
    【Android 一些难以理解的控件、容易混淆的、多种实现方式的、一些该纠正的想法】
    【进攻移动开发_htm5_跨平台_的号角】
    【进攻Android的号角】
  • 原文地址:https://www.cnblogs.com/zheaven/p/10084094.html
Copyright © 2011-2022 走看看