Java design pattern-modifier Pattern

Source: Internet
Author: User

Java design pattern-modifier Pattern
Simulate clothing wear

Let's take a look at the following specific case: it is essential for everyone to wear clothes (to decorate themselves) after getting up in a day. The question is, what to wear? In what order?
The Code is as follows:

/*** Program simulates a person's clothing process * @ author: qhyuan1992 * // abstract interface, interface People {public void wear () ;}// specific object to be appended with some operations, this object will be appended with some additional operations class Jane implements People {public void wear () {System. out. println (What should I wear today ?);} // The modifier class, holding an instance class Decorator implements People {private People people; public Decorator (People people People) {this. people = people;} public void wear () {people. wear () ;}}// the specific modifier class, responsible for adding additional operations: wearing a shirt class DecoratorShirt extends Decorator {public DecoratorShirt (People people People) {super (people);} public void wear () {super. wear (); System. out. println (wearing a shirt) ;}/// specific modifier class, responsible for adding additional operations: wearing a suit class DecoratorSuit extends Decorator {public DecoratorSuit (People people) {super (people);} public void wear () {super. wear (); System. out. println (wear a suit) ;}/// specific modifier class, responsible for adding additional operations: wear T-Shirtclass DecoratorTShirt extends Decorator {public DecoratorTShirt (People people) {super (people);} public void wear () {super. wear (); System. out. println (wear T-Shirt) ;}// the specific modifier class, responsible for adding additional operations: wear pants class DecoratorPants extends Decorator {public DecoratorPants (People people) {super (people);} public void wear () {super. wear (); System. out. println (trousers) ;}/// specific modifier class, responsible for adding additional operations: Shoes class DecoratorShoes extends Decorator {public DecoratorShoes (People people People) {super (people);} public void wear () {super. wear (); System. out. println (shoes) ;}} public class DecoratorTest {public static void main (String [] args) {People p1 = new DecoratorSuit (new DecoratorShirt (new Jane ())); p1.wear (); System. out. println (--------------); People p2 = new DecoratorTShirt (new DecoratorPants (new Jane (); p2.wear (); System. out. println (--------------); People p3 = new DecoratorTShirt (new DecoratorPants (new DecoratorShoes (new Jane (); p3.wear (); System. out. println (--------------); People p4 = new DecoratorShoes (new DecoratorPants (new DecoratorTShirt (new Jane (); p4.wear ();}}

Print Output:

What should I wear today?
Wear a shirt
Wear a suit
-----
What should I wear today?
Pants
Wear a T-Shirt
-----
What should I wear today?
Shoes
Pants
Wear a T-Shirt
-----
What should I wear today?
Wear a T-Shirt
Pants
Shoes

In the above example:

People class: abstract interface, used to standardize the object to be appended with some operations. Jane class: a specific object, this object will be appended with some additional operations. Decorator class: modifier class, the DecoratorShirt class holds an instance of the interface object to be decorated: The specific modifier class, which is responsible for adding additional operations: wearing a shirt DecoratorSuit class: The specific modifier class, additional operations: DecoratorTShirt class: Specific modifier class; additional operations: T-Shirt DecoratorPants class: Specific modifier class, add additional operations: wear pants DecoratorShoes class: Specific decoration class, add additional operations: wear shoes

In the test code, a series of common methods in the new and java I/O streams are almost the same. Yes, this is the modifier mode.

Decoration Mode Decorator Mode

Decoration mode, also known as Wrapper mode. The decoration mode extends the object functions in a transparent way to the client. It is an alternative to the inheritance relationship. The Decorator mode is more flexible than the subclass generation function.
The class graph structure of the modifier mode is as follows:
VcDgzbw = "src =" http://www.bkjia.com/uploads/allimg/150907/04330153S-0.png "title =" \ "/>
The role of classes or interfaces in the modifier mode:

Abstract Component role: provides an abstract interface to standardize the objects that are prepared to receive additional responsibilities. ConcreteComponent role: defines a class that will receive additional responsibilities. Decorator: holds an instance of a Component object and defines an interface consistent with the abstract Component Interface. The ConcreteDecorator role is responsible for attaching "attaching" to the component object.

The following is a class diagram of the simulated clothing program.

Why do I use decorator instead of inheritance?

We know that to implement the above example of wearing clothes, we don't have to use the decorator mode. It can be solved like using integration. The decorator and integration both need to expand the object function. So why do you choose the decorator mode?

The decoration mode provides more flexibility than inheritance. The decoration mode allows you to dynamically add or delete a decoration function. Inheritance requires that the corresponding class be determined before this.
In the simulated example above, we know that the order in which a person wears clothes is variable. In other words, there are many ways to arrange and combine the clothes, if inheritance is used to consider so many situations, many classes are required. However, the modifier can easily create different combinations of behaviors dynamically during use.

Decorator Mode Simplified modifier Mode Only one specific component role

If a specific Component (ConcreteComponent) has only one role, you can remove the abstract Component Interface and use Decorator as a subclass of ConcreteComponent.

Code:

Class Jane {public void wear () {System. out. println (What should I wear today ?);} Class Decorator extends Jane {private Jane; public Decorator (Jane) {this. jane = Jane;} public void wear () {Jane. wear () ;}} class DecoratorShirt extends Decorator {public DecoratorShirt (Jane) {super (Jane) ;}public void wear () {super. wear (); System. out. println (shirt);} class DecoratorSuit extends Decorator {public DecoratorSuit (Jane) {super (Jane);} public void wear () {super. wear (); System. out. println (suit);} class DecoratorTShirt extends Decorator {public DecoratorTShirt (Jane) {super (Jane);} public void wear () {super. wear (); System. out. println (wearing T-Shirt);} class DecoratorPants extends Decorator {public DecoratorPants (Jane) {super (Jane);} public void wear () {super. wear (); System. out. println (trousers);} class DecoratorShoes extends Decorator {public DecoratorShoes (Jane) {super (Jane);} public void wear () {super. wear (); System. out. println (shoes) ;}} public class DecoratorTest {public static void main (String [] args) {Jane p1 = new DecoratorSuit (new DecoratorShirt (new Jane ())); p1.wear (); System. out. println (================); Jane p2 = new DecoratorTShirt (new DecoratorPants (new Jane (); p2.wear (); System. out. println (================); Jane p3 = new DecoratorTShirt (new DecoratorPants (new DecoratorShoes (new Jane ()))); p3.wear (); System. out. println (================); Jane p4 = new DecoratorShoes (new DecoratorPants (new DecoratorTShirt (new Jane ()))); p4.wear ();}}
Only one decoration role

If there is only one ConcreteDecorator role, you can remove the Decorator class.

Code:

Interface People {public void wear ();} class Jane implements People {public void wear () {System. out. println (Jane: What should we wear today ?); } Class LiMing implements People {public void wear () {System. out. println (LiMing: What should I wear today ?); } Class DecoratorShoes implements People {private People people; public DecoratorShoes (People people People) {this. people = people;} public void wear () {people. wear (); System. out. println (shoes) ;}} public class DecoratorTest {public static void main (String [] args) {People p1 = new DecoratorShoes (new Jane (); p1.wear (); people p2 = new DecoratorShoes (new LiMing (); p2.wear ();}}

Output:

Jane: What should I wear today?
Shoes
LiMing: What should I wear today?
Shoes

The specific component role and the specific decoration role have only one

In extreme cases, a specific component (ConcreteComponent) has only one role, and a specific Decoration (ConcreteDecorator) has only one role.

Class Jane {public void wear () {System. out. println (Jane: What should I wear today ?); } Class DecoratorShoes extends Jane {private Jane jane; public DecoratorShoes (Jane jane) {this. jane = jane;} public void wear () {jane. wear (); System. out. println (shoes) ;}} public class DecoratorTest {public static void main (String [] args) {Jane j = new DecoratorShoes (new Jane (); j. wear ();}}

This completely degrades to the simplest form of inheritance. As follows:

Class Jane {public void wear () {System. out. println (Jane: What should I wear today ?); } Class DecoratorShoes extends Jane {public void wear () {super. wear (); System. out. println (shoes) ;}} public class DecoratorTest {public static void main (String [] args) {DecoratorShoes j = new DecoratorShoes (); j. wear ();}}
Translucent Modifier

In Decorator mode, the Decorator role holds an instance of a Component object and defines an interface consistent with the abstract Component Interface. That is to say, the Decorator class is not allowed to extend the interfaces in the Component class. However, some new methods may be defined in the Decorator. In this case, the decorator becomes a translucent decorator mode, and the standard decorator mode above becomes a transparent decorator mode.
For example, the Decorator class adds the eat method, which is not in the People class.

A good program design method tells us to declare variables to use top-level superclasses as much as possible, and use polymorphism to achieve the same code to achieve different effects, which is also the same in the modifier mode, you must declare a Component type object instead of a Decorator, not a ConcreteComponent type object.

For example, in the following sample code:

// Try not to declare DecoratorSuit decoratorSuit = new DecoratorSuit (new DecoratorShirt (new Jane () like this ())); // declare People p1 = new DecoratorSuit (new DecoratorShirt (new Jane () like this ()));

Sample Code:

Interface People {public void wear ();} class Jane implements People {public void wear () {System. out. println (What should I wear today ?);} Class Decorator implements People {private People people People; public Decorator (people People) {this. people = people;} public void wear () {people. wear () ;}public void eat () {}} class DecoratorShirt extends Decorator {public DecoratorShirt (People people) {super (people);} public void wear () {super. wear (); System. out. println (wearing a shirt);} public void eat () {System. out. println (shirt-go to dinner);} class DecoratorSuit extends Decorator {public DecoratorSuit (People people) {super (people);} public void wear () {super. wear (); System. out. println (suit);} public void eat () {System. out. println (suit-go to dinner);} public class DecoratorTest {public static void main (String [] args) {// do not declare DecoratorSuit decoratorSuit = new DecoratorSuit (new DecoratorShirt (new Jane (); decoratorSuit. wear (); decoratorSuit. eat ();/* declare * // declare People p1 = new DecoratorSuit (new DecoratorShirt (new Jane (); p1.wear (); // p. eat (); // error/* errors */People p2 = new DecoratorSuit (new DecoratorShirt (new Jane (); (DecoratorSuit) p2 ). eat ();}}

Output

What should I wear today?
Wear a shirt
Wear a suit
Suit-go to dinner
What should I wear today?
Wear a shirt
Wear a suit
Suit-go to dinner

The above Code provides a new method eat in the Decorator class, which is not available in the People class, therefore, if the object is declared as a super class in the code above, that is, the People class (the real type is the DecoratorSuit class), there is no problem when the eat method is not called, however, to call this method, you must perform a downward transformation.

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.