Design Pattern Learning Notes (vi)-decorator decoration mode

Source: Internet
Author: User

"Design Patterns," a book on decorator is described in this way:

Dynamically add some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.

That is, add some extra functionality to the object dynamically. It works by creating a "chain" of an object that begins with the Decorator object (the object responsible for the new function) terminates in the original object. For example, we want to design a program for printing tickets for the cashier of the supermarket, some need to print the header information of the bill, some need to print the footer information of the bill, and some only need to print the contents of the bill. If you modify the program once for each situation, it will be troublesome. Then we can consider using the decorator mode. Its structure class diagram is as follows:

The code is as follows:

Abstract class component{
Abstract public void PrintTicket ();
}
Class Salesticket extends component{
public void PrintTicket () {
System.out.println ("Print out the contents of Salesticket");
}
}
Abstract class Ticketdecorator extends component{
Private Component Mytrailer;
Public Ticketdecorator (Component mycomponent) {
Mytrailer=mycomponent;
}
public void Calltrailer () {
if (mytrailer!=null)
Mytrailer.printticket ();
}
}
Class Header extends ticketdecorator{
Public Header (Component mycomponent) {
Super (MyComponent);
}
public void PrintTicket () {
System.out.println ("Print Salesticket header information");
Super.calltrailer ();

}
}
Class Footer extends ticketdecorator{
Public Footer (Component mycomponent) {
Super (MyComponent);
}
public void PrintTicket () {
Super.calltrailer ();
System.out.println ("Print footer information for salesticket");
}
}
public class Client {

public static void Main (string[] args) {
System.out.println ("====================================");
New Header (New Footer Salesticket ()). PrintTicket ();
System.out.println ("====================================");
New Footer (New Salesticket ()). PrintTicket ();
System.out.println ("====================================");
}

}

The output results are as follows:

====================================

打印salesTicket的头信息
打印出salesTicket的内容
打印salesTicket的页脚信息
====================================
打印salesTicket的头信息
打印出salesTicket的内容
打印salesTicket的页脚信息

====================================

From this example we can see that the decorator model divides the problem into two parts:

1 How to implement objects that provide new functionality.

2 How to organize objects for each particular situation.

This improves cohesion by separating the implementation of the Decorator object from the objects that decide how to use decorator, because each decorator object uses only the functionality that is added to it and does not care how it is added to the object chain. You can also rearrange the order of decorator arbitrarily, without changing any of its code.

Summary: The application of the decorator pattern is that the various optional features are executed before or after another feature that is certain to be performed.

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.