Java Software Design pattern------decorative design pattern

Source: Internet
Author: User

Java Software Design patterns are divided into 23 kinds;

Generally used are:

  Single Case design mode

Decorative design pattern

Factory design Pattern

Decorative design pattern

Definition: English is called decorator pattern, also known as decorator mode. Decorating mode is the ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.

In life we use programmers and project managers as an example of metaphor, project managers start from the programmer, through continuous accumulation of work experience, constantly learning new knowledge, it is possible to get promoted.

In the decorative design mode, the project manager is the programmer's enhancement.

Program Ape:
Write a program
Project Manager
Research needs
Write a program
Project Management

1. Create a new programmer class

/**
* Programmer
* @author Administrator
*/
public class Programmaer {
public void code () {
System.out.println ("Write Code:");
}
}

2. Create a Project manager class (the project manager adds new methods of work based on the programmer)

/**
* Project Manager
* @author Administrator
*/
Public class Programermannerger {
private Programmaer p;
//constructor method parameter for programmer's Reference
Public Programermannerger (Programmaer PR) {
THIS.P=PR;
}
//How the project manager works
Public void Work () {
System.out.println ("research needs");
P.code ();//Programmer's Method
System.out.println ("Project Management");

}
}

3. Create a Test class:

/**
* Add function on the basis of programmer, add function with package design mode
* @author Administrator
*/
public class Test2 {
public static void Main (string[] args) {
Programmers
Programmaer p=new Programmaer ();
Project Manager
Programermannerger pm=new Programermannerger (p);
Pm.work ();
}
}

Conclusion: When a program is completed, how do you add functionality without a wide range of code changes? You can use the decorative design pattern.

Design Patterns for Java software------decorative Design Patterns

Related Article

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.