Design Patterns - Decorative Patterns


decorative pattern 

1. Overview 
2. Structure of decoration mode 
3. Specific cases 
4. Difference between decoration mode and class inheritance 
5. Features of decoration mode: 


I. Overview

Decorative mode, also known as Wrapper mode, extends the functionality of objects in a transparent manner to clients and is an alternative to inheritance.
The Decorator pattern dynamically attaches more responsibilities to an object in a way that is transparent to the client. In other words, the client will not feel any difference between the object before and after decoration.
The Decorator pattern extends the functionality of an object without creating more subclasses.




Second, the structure of decorative patterns

  The class diagram of the decoration mode is as follows:

write picture description here




The roles in the decoration pattern are:
Abstract component (Component) role: Gives an abstract interface to standardize objects that are ready to accept additional responsibilities.
ConcreteComponent role: Define a class that will accept additional responsibilities.
Decorator role: hold an instance of a Component object and define an interface consistent with the abstract component interface

Concrete Decorator: Responsible for attaching additional responsibilities to component objects.


Source code:
//Abstract component role
public interface Component{
public void sampleOperation();
}    

// specific component role
public class ConcreteComponent implements Component {
 @Override
 public void sampleOperation (){
 //Write related business code
 }
 }                    

// decorate character
public class Decorator implements Component{
private Component component;
public Decorator(Component component){
this.component = component;
    }
@Override
public void sameOperation(){
//委派给构件
component.sampleOperation();
    }
}    
            
                        




3. Specific cases 

The role of the abstract component is played by the Programmer programmer interface The role of the
concrete component is played by the class Tom, which implements the functions that the programmer has. The
decorative component is played by the class Derector. It must also implement the abstract component interface
. The role of the specific decorative component is the class Hacker (hacker) and the class SoftwareAchitect (Architect) Playing as a
specific programmer Tom has programming ability God can give him more abilities for each ability he has one more skill This is achieved by decorating components


//Abstract component role

/**
 *Abstract component role
 * Programmer interface Programmer has the ability to program
 *
 */
 public interface Programmer {
 //Programming
 public void programme ();
 }        


// specific component role

/**
 * Concrete component role
 * Tom Gao is a specific programmer
 * then he has programming ability
 *
 */
 public class Tom implements Programmer {
 public void programme () {
 System . out . println ( "Programmers can program" );
     }
 }            

// decorate character
/*
 *Decorative role
 */
 public class Derector implements Programmer {
 private Programmer programmer ;
 public Derector( Programmer programmer ){
 this . programmer = programmer ;
     }
 @Override
 public void programme (){
 programmer . programme ();
 //Additional responsibilities or function
 }
 }    
            
                            


//Specific decoration role 1
/**
 * Specific decorative role  
 * The hacker class has additional functions and can hack into other people's computers
 */
 public class Hacker extends Derector {
 public Hacker( Programmer programmer ) {
 super ( programmer );
     }
 @Override
 public void programme () {
 super . programme ();
 //Additional responsibility or function
 System . out . println ( "I have hacker skills and I can hack into other people's computers" );
     }
 }            
                                



//Specific decoration role 2
/**
 * Specific decoration roles  
 * Software architect class, which has additional functions to design the bones of a website or system
 */
 public class SoftwareArchitect extends Derector {
 public SoftwareArchitect( Programmer programmer ) {
 super ( programmer );
     }
 @Override
 public void programme () {
 super . programme ();
 // Additional responsibilities or functions
 System . out . println ( "I have the skills of an architect and I can design the skeleton of a website or system" );
     }
 }                
                                

//client
public class Client {
    public static void main(String[] args) {
        //创建构件对象    汤高——》具体的程序员
        //现在要拓展它的功能 对它进行装饰
        Programmer tom = new Tom();
        Derector hacker = new Hacker(tom);
        System.out.println("第一次装饰");
        hacker.programme();

        //我们对它的功能还不满足
        // 进行第二次装饰
        Derector architect = new SoftwareArchitect(hacker);
        System.out.println("第二次装饰");
        architect.programme();


        //也可以一步装饰两个技能 因为他们有共同的父类抽象构件接口 Programmer
        System.out.println("--------------- 一次装饰两个技能");
        Derector softwareArchitect = new SoftwareArchitect(new Hacker(new Tom()));
        softwareArchitect.programme();
    }
}

运行结果:

第一次装饰

Programmers can program
Hackers have hacker skills
Second decoration
Programmers can program
Hackers have hacker skills
Security architects have security architect skills
--------------- Two decorations at a time Skills
Programmers can program
Hackers have the skills of a hacker
Security architects have the skills of a security architect




Fourth, the difference between decoration mode and class inheritance

1) The decoration mode is a dynamic behavior, which arbitrarily combines existing classes, while the inheritance of classes is a static behavior. What kind of a class is defined, what kind of function does the object of this class have, which cannot be dynamically change.
2) The decoration mode extends the function of the object and does not need to increase the number of classes. The class inheritance extension is the function of the class. In the inheritance relationship, if we want to add the function of an object, we can only add methods to the subclass through the inheritance relationship.
3) The decoration mode is to dynamically expand the function of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapper object, that is, decoration.


Five, the characteristics of decoration mode

1) The decorated object and the real object have the same interface, so that the client object can interact with the decorated object in the same way as the real object.
2) The decoration object contains a reference to the real object.
3) The decoration object receives all requests from the client, and it forwards these requests to the real object.
4) Decorative objects can add some additional functions before or after forwarding these requests.
This ensures that additional functionality can be added externally at runtime without modifying the structure of a given object.
In object-oriented programming, it is common to use integrated relationships to extend the functionality of a given class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566129&siteId=291194637