5- decorative pattern

Decorative pattern

The case of decorative patterns, without having to change the original class files and use inheritance, dynamically extend an object's function. It is by creating a wrapper object, which is decorated to wrap the real object.

scenes to be used

  1. We need to extend the functionality of a class, or a class to add additional responsibilities.

  2. Need to dynamically add functionality to an object, these functions can then dynamically revoked.

  3. Needs to be increased by a very large number of permutations and combinations of features of the basic functions is generated, so that the inheritance becomes impractical.

  4. The method can not be employed when generating a subclass be expanded. In one case, there may be a large number of separate extension to support each combination will produce a large number of sub-categories, so that the explosive growth in the number of subclasses. Another situation may be hidden because of the class definition, the class or subclass definition can not be used to generate.

Decorative pattern

Here Insert Picture Description

It allows new functionality to an existing object, without changing its structure.

Decorated class

//被装饰类的抽象类
public abstract class AbstractCake { //被装饰类和装饰类的最终父类
    abstract String detail(); //蛋糕的描述
    abstract int money();	  //蛋糕的价钱
}
-----------------------------------------
//实现类
public class Cake extends AbstractCake { //被装饰类的具体实现类
    @Override
    String detail() {
        return new String("普通蛋糕");
    }

    @Override
    int money() {
        return 8;
    }
}

Decoration

//装饰类的抽象类
public class AbstractBigCake extends AbstractCake { //继承自抽象被装饰类
    AbstractCake cake; //保存被装饰的类
    public AbstractBigCake(AbstractCake cake) {
        this.cake = cake;
    }
    @Override
    String detail() {
        return cake.detail();
    }
    @Override
    int money() {
        return cake.money();
    }
}
-------------------------------------------
//装饰类实现类
public class ChocolateCake extends AbstractBigCake { //继承抽象装饰类
    public ChocolateCake(AbstractCake cake) {
        super(cake); //将被装饰类注入进其父类抽象装饰类中
    }
    @Override
    String detail() {
        return super.detail()+"加一份巧克力"; //增加描述
    }
    @Override
    int money() {
        return super.money()+4; //增加价钱
    }
}
--------------------------------------------
//装饰类实现类
public class FruitCake extends AbstractBigCake { //继承抽象装饰类
    public FruitCake(AbstractCake cake) {
        super(cake); //将被装饰类注入进其父类抽象装饰类中
    }
    @Override
    String detail() {
        return super.detail()+"加一份水果"; //增加描述
    }
    @Override
    int money() {
        return super.money()+2; //增加价钱
    }
}

Decorated class by saving a reference base, and when you call the method to call the combination of basic decoration and add new features to achieve a decorative function.

Test category

//测试类
class Test {
    public static void main(String[] args) {
        AbstractCake cake=new Cake(); //创建一个基本的蛋糕
        cake=new ChocolateCake(cake); //为蛋糕加一份巧克力
        cake=new FruitCake(cake);	  //为蛋糕加一份水果
        System.out.println(cake.detail()); //普通蛋糕加一份巧克力加一份水果
        System.out.println(cake.money());  //14	
    }
}

.println (cake.detail ()); // add an ordinary chocolate cake plus a fruit
System.out.println (cake.money ()); // 14
}
}


每次装饰只需要将其装入装饰类即可。
Published 27 original articles · won praise 1 · views 904

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103230464