Java Adorner mode

Source: Internet
Author: User

Intent: Dynamically attaching responsibility to an object
When to use:
1. Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects
2. Handling of duties that can be revoked
3. When you cannot expand by the way the subclass is generated
Structure diagram:

 Package Com.test.patten.decorator;  Public Interface Person {     void  docoding ();}
 package   Com.test.patten.decorator;  public  class  Employee implements   person {@Override  
   
    public  
    void  
     Docoding () {System.out.println () programmer to write programs, write programs, finally finished ...    "
    ); }}
   
 Package Com.test.patten.decorator;  Public Abstract class Implements person{        // Adorner add function public    abstractvoid  Docoding ();}
 PackageCom.test.patten.decorator; Public classManageraextendsManager {Privateperson person;//to raise a job for an employee         PublicManagera (person person) {Super();  This. person =Person ; } @Override Public voiddocoding () {doearlywork ();            Person.docoding (); }    /*** Project Manager to start preparatory work*/     Public voiddoearlywork () {System.out.println ("Project Manager A Needs Analysis"); System.out.println ("Project Manager A architecture design"); System.out.println ("Project Manager a detailed design"); }}
 PackageCom.test.patten.decorator; Public classManagerbextendsManager {Privateperson person;//to raise a job for an employee         PublicManagerb (person person) {Super();  This. person =Person ; } @Override Public voiddocoding () {person.docoding ();    Doendwork (); }    /*** Project Manager to start Project finishing work*/     Public voiddoendwork () {System.out.println ("Project Manager B is doing a finishing job."); }    }
 package   Com.test.patten.decorator;  public  class   Client { public  static  void   main (String args[]) {person employee  = new          Employee (); Employee  = new  Managera (employee); //  Assign Program Ape Project manager a responsibility  employee = new  managerb (Emplo Yee); //  Assign program Ape Project Manager B responsibility      employee.docoding (); }}

项目经理A做需求分析

项目经理A做架构设计项目经理A做详细设计程序员加班写程序啊,写程序,终于写完了。。。项目经理B 在做收尾工作

Applied design principles: open to extensions, close to modifications

Advantages and Disadvantages

1. Decorator class reflects the type of component being decorated

2. The Decorator class may add his or her behavior before or after the act of the decorator, or even replace the act of the decorator, for a specific purpose.

3. You can use countless decorators to package a component, the decorator class will lead to many small objects in the design, overuse will make the program complex

4. Decorators are generally transparent to the customer of the component, unless the client program relies on the specific type of the component

Transferred from: http://www.cnblogs.com/ikuman/archive/2013/01/29/2877913.htm

Java Adorner mode

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.