1.9 decorator Mode

Source: Internet
Author: User
Tags class manager
Java and mode: decorator ModeThe decoration mode uses an instance of a subclass of the decoration class to delegate the call of the client to the decoration class. The key to the decoration mode is that the extension is completely transparent. The decoration mode is also widely used in Java. For example, when we redefine buttons and dialogs, we are actually using the decoration mode. The simplest example of the decoration mode is the photo-frame example. 1. In the schematic diagram, the class responsibilities are as follows: Abstract component role (Project): provides an interface to standardize the role of the specific component (employe) of the object to be prepared to receive additional responsibilities ): define a class decoration role (manager) that will receive additional responsibilities: Hold an instance of a component object, and define an interface specific decoration role (managera, managerb) consistent with the abstract Component Interface ): 2. The following is an example of a software project to describe how to use the decoration mode: The Project Manager receives a project, the project must be finalized. After receiving the project, the project manager first does some preliminary work (such as Requirement Analysis and Design), and then delegates the coding work to the Code workers. After the code workers finish, the Project Manager finishes the project. The implementation code is as follows :/**
* Created by intellij idea.
* User: leizhimin
* Date: 12:51:06
* Project
*/
Public interface project {

/**
* Write code
*/
Void docoding ();
}/**
* Created by intellij idea.
* User: leizhimin
* Date: 12:52:12
* Code worker
*/
Public class employe
Implements project {
/**
* Encoding
*/
Public void docoding (){

System. Out. println! ");

}
}/**
* Created by intellij idea.
* User: leizhimin
* Date: 12:51:26
* Project Manager
*/
Public class Manager
Implements project {
Private project; // actually stores the code worker object

Public Manager (Project ){
This. Project = project;
}

/**
* Encoding
*/
Public void docoding (){

// The project manager starts a new job.
Startnewwork ();
}

/**
* Template: Define the project manager's own affairs
*/
Public void startnewwork (){

// The project manager is doing early work
Doearlywork ();
// The project manager is very good. After completing the requirements and design, the code is directly assigned to the Code worker.
Project. docoding ();
// The project manager is finishing the work.
Doendwork ();
}

/**
* The Project Manager's work is to do the early work.
*/
Public void doearlywork (){

}

/**
* The Project Manager finishes the work.
*/
Public void doendwork (){

}
}/**
* Created by intellij idea.
* User: leizhimin
* Date: 13:45:18
* Project Manager
*/
Public class managera
Extends manager {

Public managera (Project ){
Super (project );
}

/**
* The Project Manager's work is to do the early work.
*/
Public void doearlywork (){

System. Out. println ("requirement analysis by Project Manager ");
System. Out. println ("Project Manager A is designing the architecture ");
System. Out. println ("Project Manager A is designing in detail ");
}
}/**
* Created by intellij idea.
* User: leizhimin
* Date: 13:45:27
* Project Manager B
*/
Public class managerb
Extends manager {

Public managerb (Project ){
Super (project );
}

/**
* The Project Manager's work is to do the early work.
*/
Public void doearlywork (){

System. Out. println ("requirement analysis by Project Manager B ");
System. Out. println ("Project Manager B is designing in detail ");
}

/**
* The Project Manager finishes the work.
*/
Public void doendwork (){

System. Out. println ("Project Manager B finishing work ");
}
}/**
* Created by intellij idea.
* User: leizhimin
* Date: 13:03:22
* Client Test
*/
Public class client {

Public static
Void main (string ARGs []) {
Project employe = new employe (); // code worker

Project managera = new managera (employe );
// Project Manager
Project managerb = new managerb (employe );
// Project Manager
// The code is completed in the name of the manager. The credit is for the manager, and the actual code is for the worker.
Managera. docoding ();
Managerb. docoding ();
}
} Running result: Project Manager A is conducting requirement analysis.
Project Manager A is designing the architecture.
Project Manager A is doing a detailed design
Code workers are writing code, working overtime, editing, and finally finishing!
Project Manager B is conducting Requirement Analysis
Project Manager B is doing a detailed design
Code workers are writing code, working overtime, editing, and finally finishing!
Project Manager B is finishing the work

Process finished with exit code 0

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.