Java Common design pattern 07: Decorator mode for common design patterns

Source: Internet
Author: User

1. Java Decorator mode (Decorator pattern)

(1) Overview:

Decoration mode is also widely used in Java, for example, when we redefine buttons, dialogs, etc., we are actually using decorative mode. The ability to dynamically extend an object without having to change the original class file and use inheritance. It is done by creating a wrapper object, and also by decorating it to wrap the real object .

Decorator mode: Dynamically add some extra responsibilities to an object, just like painting on a wall. For added functionality, the decorator mode is more flexible than generating subclasses.

The decorator mode works by creating an object "chain" that starts with the Decorator object (the object responsible for the new function) and finally the original object.

                      Figure 1 Decorator Chain

features of the decoration mode :

• Decorative objects and real objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object. • Adornment object contains a reference to a real object (reference)   • The Adornment object accepts all requests from the client. It forwards these requests to the real object.    • Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.   

(2) the UML diagram of the adorner mode:

schematic diagram:the duties of the classes are as follows:Abstract Component Role (Project): An interface is given to standardize the objects that are ready to receive additional responsibilitiesConcrete Component Role (Employe): Defines a class that will receive additional responsibilitiesDecorative Role (Manager): Holds an instance of a Component object and defines an interface that is consistent with the abstract component interfaceSpecific decorative roles (Managera, Managerb): Responsible for "attaching" additional responsibilities to the component objects2. Decorator Mode code implementation:The following is a software project example to illustrate the use of decorative patterns:The process is this:The project manager receives a project, and the project eventually completes the coding. after the project manager receives the project, it first does some preliminary work (such as the requirement analysis, the design), then assigns the coding work to the code worker, after the code worker finishes, the project manager does the project the finishing work. The implementation code is as follows:
1 /** 2 * Project  3 */ 4  Public Interface  5  6     /**   7 8             * *  9     voidten }

1 /** 2 * code worker 3 */ 4  Public classEmployeImplementsproject{5     /** 6 * Code7      */ 8      Public voiddocoding () {9SYSTEM.OUT.PRINTLN ("Code workers in writing code, overtime, ah, finally finished!" "); Ten     }  One}

1 /** 2 * project manager 3 */ 4  Public classManagerImplementsProject {5     PrivateProject project;//The code worker object is actually stored6 7      PublicManager (Project project) {8          This. Project =project;9     } Ten  One     /**  A * Code -      */  -      Public voiddocoding () { the         //The project manager starts a new job - startnewwork (); -     }  -  +     /**  - * Template: Define the project manager's own business +      */  A      Public voidstartnewwork () { at         //The project manager is doing early work - doearlywork (); -         //The project Manager is very bull, after the requirements and design, directly to the code to delegate coding workers to do - project.docoding (); -         //the project manager is finishing the work. - doendwork (); in     }  -  to     /**  + * Project manager's own business: doing early work -      */  the      Public voiddoearlywork () { *     }  $ Panax Notoginseng     /**  - * Project Manager to do the finishing work the      */  +      Public voiddoendwork () { A     }  the}

1 /** 2 * specific project manager a 3 */ 4  Public classManageraextendsmanager{5 6      PublicManagera (Project project) {7         Super(project);8     } 9 Ten     /**  One * Project manager's own business: doing early work A      */  -      Public voiddoearlywork () { -System.out.println ("project Manager A is doing demand analysis"));  theSystem.out.println ("project manager A is doing architecture design");  -System.out.println ("project Manager A is doing detailed design");  -     }  - } +  

1 /** 2 * specific project manager B 3 */ 4  Public classManagerbextendsManager {5 6      PublicManagerb (Project project) {7         Super(project);8     } 9 Ten      /**  One * Project manager's own business: doing early work A      */  -      Public voiddoearlywork () { -System.out.println ("project Manager B is doing demand analysis"));  theSystem.out.println ("project Manager B is doing detailed design");  -     }  -  -     /**  + * Project Manager to do the finishing work -      */  +      Public voiddoendwork () { ASystem.out.println ("project Manager B is doing a finishing job");  at     }  -}

1 /** 2 * Client testing 3 */ 4  Public classClient {5      Public Static voidMain (String args[]) {6Project Employe =NewEmploye ();//Code worker7Project Managera =NewManagera (Employe);//Project Manager8Project Managerb =NewManagerb (Employe);//Project Manager9         //in the name of the manager to complete the code, the credit is the manager, the actual code is the workersTen managera.docoding (); One managerb.docoding (); A     }  -}

Operation Result:

Project Manager A is doing demand analysis
Project Manager A is doing architecture design
Project Manager A is doing detailed design
Code workers in writing code, overtime, make up Ah, finally finished!
Project Manager B is doing demand analysis
Project Manager B is doing detailed design
Code workers in writing code, overtime, make up Ah, finally finished!
Project Manager B is doing a finishing job.

Java Common design pattern 07: Decorator mode for common design patterns

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.