Java design pattern-modifier pattern (simple notes)

Source: Internet
Author: User
Purpose: Expand the object function.
Involved roles:
1. Abstract component role: defines an abstract interface to standardize the classes for preparing additional functions.
2. Specific Component role: class to be attached with a function to implement the abstract component role interface.
3. Abstract modifier role: holds a reference to a specific component role and defines an interface consistent with the abstract component role.
4. Specific decoration role: implements the abstract decoration role and adds additional functions to specific components.
 
Code example:
Java code of the abstract component role
package decorator;    
public interface InterfaceComponent {   
    public void say();   
}    
Java code of the specific component role
package decorator;    
public class Component implements InterfaceComponent{   
    public void say() {   
System. Out. println ("component. Say (): Method of the original component! ");
    }    
} 
Java code of the abstract modifier role
package decorator;    
public abstract class AbstractDecorator  implements   InterfaceComponent{   
         private InterfaceComponent   component;   
         public AbstractDecorator(InterfaceComponent   component){   
                  this.component = component;    
         }    
Protected void presay () {}; // pre-processing method before the component method is executed
Protected void aftersay () {}; // post-processing method for component method execution
         public void say(){   
                  preSay();    
                  component.say();    
                  afterSay();    
         };    
}    
Specific modifier 2 Java code
package decorator;    
public class DecoratorTwo extends AbstractDecorator{   
      public DecoratorTwo(InterfaceComponent component) {   
           super(component);   
       }    
Protected void presay () {// reload the template class presay () method as needed
System. Out. println ("decoratortwo. presay (): presay () method of modifier 2! ");
      }    
Protected void aftersay () {// reload the template class aftersay () method as needed
System. Out. println ("decoratortwo. aftersay (): The aftersay () method of modifier 2! ");
   }    
}    
Modifier 1 Java code
package decorator;    
public class DecoratorOne extends AbstractDecorator{   
       public DecoratorOne(InterfaceComponent component) {   
              super(component);   
       }    
Protected void presay () {// reload the template class presay () method as needed
System. Out. println ("decoratorone. presay (): presay () method of modifier 1! ");
      }            
Protected void aftersay () {// reload the template class aftersay () method as needed
System. Out. println ("decoratorone. aftersay (): The aftersay () method of modifier 1! ");
     }    
Public static void main (string [] ARGs) {// Test Method
               InterfaceComponent interfaceComponent = new DecoratorTwo(new DecoratorOne(new Component()));   
               interfaceComponent.say();    
      }    
}    
Console output:
  1. * Console output:
  2. * Decoratortwo. presay (): The presay () method of decoratortwo!
  3. * Decoratorone. presay (): The presay () method of decoratorone 1!
  4. * Component. Say (): Method of the original component!
  5. * Decoratorone. aftersay (): The aftersay () method of modifier 1!
  6. * Decoratortwo. aftersay (): The aftersay () method of decoratortwo!
 
4. Advantages and Disadvantages
Advantages: 1) provide more flexibility than inheritance 2) use different decorative combinations to create combinations of different behaviors 3) Reduce the number of classes required
Disadvantages: 1) Flexibility leads to greater errors. 2) more objects are generated, which makes error detection difficult.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.