Detailed description of decoration patterns for Java design patterns

  • 2020-04-01 02:38:54
  • OfStack

1.       Definition of Decorator: also known as the Wrapper pattern, the Decorator pattern extends the functionality of the object in a way that is transparent to the client and is an alternative to inheritance relationships.

2.       The decorator pattern dynamically attaches more responsibility to an object in a way that is transparent to the client. In other words, the client does not perceive the difference between the object before and after the decoration.

3.       The decorator pattern extends the functionality of the object without creating more subclasses.

4.       Differences between decorator patterns and class inheritance:

1)       The decorator pattern is a dynamic behavior, which randomly combines existing classes, while the inheritance of classes is a static behavior. What a class is defined as, the object of the class will have what kind of functions, which cannot be dynamically changed.

2)       Decorator mode extends the function of the object without increasing the number of classes, while 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 two methods in the subclass through the inheritance relationship.

3)       Comparison of decoration and inheritance:

4)       The decorator pattern dynamically extends the functionality of an object without changing the class file and using inheritance, by creating a wrapper object that wraps the real object, or decorator.

5.       The key to the decorator pattern, which delegates calls to the client to the decorated class, is that the extension is completely transparent.

6.       Composition of decorative pattern:

1)       Abstract build role (Component): gives an abstract interface to an object that is ready to accept additional responsibilities. It's the same thing as I /o stream InputStream/OutputStream and Reader/Writer.

2)       Concrete build role: defines a class that will accept additional responsibilities. The FileOutputStream and FileInputStream in I /o.

3)       Decorator role (Docorator): holds a reference to an abstract build (Component) role and defines an interface consistent with the abstract Component. The FilerOutputStream and FilterInputStream in I /o.

4)       ConcreteDecorator: responsible for attaching additional responsibilities to build objects. The BufferedOutputStream and BufferedInputStream in the I /o stream and the DataOutputStream and the DataInputSrtream.

7.       Features of decorative mode:

1)       A decorated object has the same interface as a real object, so that the client object can interact with the decorated object in the same way as a real object.

2)       A decoration object contains a reference to a real object.

3)       The decorator object accepts all requests from the client, which it forwards to the real object.

4)       The decorator object can add additional functionality before or after forwarding these requests. This ensures that at run time additional functionality can be added externally without modifying the given object structure. In object-oriented programming, it is common to use inherited relationships to extend the functionality of a given class.

8.       Case study:

1)       Abstract build interface:


packagecom.abao.decorate;
 
public interface Component
{
   public void doSomething();
}

2)       Specific build roles:

packagecom.abao.decorate;
public class ConcreteComponent implements Component
{
   @Override
   public void doSomething()
   {
      System.out.println(" function A");
   }
}

3)       Decorative role:

packagecom.abao.decorate;
 
public class Decorate implements Component
{
   private Component component;
 
   public Decorate(Component component)
   {
      this.component = component;
   }
 
   @Override
   public void doSomething()
   {
      component.doSomething();
   }
}

4)       Specific decorative role 1:

packagecom.abao.decorate;
 
public class ConcreteDecorate1 extends Decorate
{
   public ConcreteDecorate1(Component component)
   {
      super(component);
   }
 
   @Override
   public void doSomething()
   {
      super.doSomething();
     
      this.doAnotherDosomething();
   }
 
   private void doAnotherDosomething()
   {
      System.out.println(" function B");
   }
}

5)       Specific decorative role 2:

packagecom.abao.decorate;
 
public class ConcreteDecorate2 extends Decorate
{
   public ConcreteDecorate2(Component component)
   {
      super(component);
   }
 
   @Override
   public void doSomething()
   {
      super.doSomething();
     
      this.doAnotherDosomething();
     
   }
 
   private void doAnotherDosomething()
   {
      System.out.println(" function C");
   }
}

6)       The client

packagecom.abao.decorate;
 
public class Client
{
   public static void main(String[] args)
   {
 
      Component component = new ConcreteDecorate1(
        new ConcreteDecorate2(new ConcreteComponent()));
      component.doSomething();
   }
}

9.       Finished!


Related articles: