Design Pattern (11) Decorator pattern-Structural Pattern

Source: Internet
Author: User

Design Pattern (11) Decorator pattern-Structural Pattern
Decorator Mode

In program development, developers sometimes use inheritance to extend the functions of objects. The user's requirements are changeable, which leads to wide-range changes to the Code when inheritance is used, in fact, the extended object function is much better than inheritance by using a combination. When users need to change the object combination, they only need to change the inherited code, in this case, you need to use the modifier mode.
The essence of the modifier mode is to expand the functions of the software without changing the original functions of the software. Just like decorating a house.
The structure of the Decorator design mode is as follows:
Interface
|
_ |____
|
MyClass Decorator
_ | _____
|
DecoratorA DecoratorB
The main purpose of adopting the decorator mode is to decouple two associated classes to facilitate the addition of some new functions.

Implementation principle diagram

 


Decorator mode implementation schematic
If you do not use the modifier mode, each time you add a new feature, you need to create a new class to inherit from the previous class to obtain all the attributes and methods of the parent class. If you modify the superclass, the child classes must be changed accordingly and are not suitable for maintenance. In the modifier mode, a superclass is an interface that can achieve decoupling.

 

Implementation

If it is a supermarket, it needs to receive cash, usually the scanning price of the product, but sometimes it will also engage in activities, playing special prices and so on. Therefore, a cashier interface is created.Cash. java

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 26, 2015 8:53:14 * class description */public interface Cash {public double getCash (String product );}

Then implement a specific Cashier function,CashImpl. java

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 26, 2015 8:53:55 * class description */public class CashImpl implements Cash {@ Override public double getCash (String product) {return 100 ;}}

Then define a cash register decoration class,CashDecorator. javaThe Code is as follows:

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: April 26, 2015 8:54:38 * class description */public class CashDecorator implements Cash {private Cash cash; public CashDecorator (Cash cash) {this. cash = cash;} @ Override public double getCash (String product) {return cash. getCash (product );}}

A New Discount Function is added, which inherits the decoration class of the cash register,RebateCash. java

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: April 26, 2015 8:58:53 * class description */public class RebateCash extends CashDecorator {public RebateCash (Cash cash) {super (cash);} public double getCash (String product) {return super. getCash (product) * 0.1 ;}}

Added the discount-only function, which inherits the decoration class of the cash register,BacktrackCash. java

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: April 26, 2015 8:58:53 * class description */public class BacktrackCash extends CashDecorator {public BacktrackCash (Cash cash) {super (cash);} public double getCash (String product) {return super. getCash (product)-0.1 ;}}
Test

In the client test, both discounts and discounts are required:

Package com. devin. decorator;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 26, 2015 9:04:59 * class description */public class Client {public static void main (String [] args) {Cash cash = new CashImpl (); cash rebateCash = new BacktrackCash (new RebateCash (cash); System. out. println ("the price of chocolate is:" + rebateCash. getCash ("Chocolate "));}}

Test results:

The price of chocolate is: 9.9.
Application

Java I/O Stream
Reader Writer
BufferedReader BufferedWriter
BufferedInputStream BufferedOutputStream

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.