Scene
I've got a lot of goddesses, and it's a problem to pick a gift for their birthday. Tomorrow is marry's birthday, send what gift. Don't want to spend too much, after all, it is Jack. Choose a photo of us, write on the back "marry Goddess, Happy Birthday", and then go to buy a picture frame decoration, in the outside to set a gift box, a simple but also have the intention of the gift produced.
Yes, the above we are the gift in the decoration, the same, the same needs to decorate the house, then we need to decorate the mode.
Brief introduction
Decorative mode is the ability to extend objects in a transparent mode to the client, an alternative to inheritance, providing more flexibility than inheritance. Dynamically adds functionality to an object and adds a lot of functionality by arranging combinations of some basic functions.
See this design pattern, is seen in Java io, the input stream and the input stream, has quite a lot of functions, such as processing files, buffering, reading and writing data, if the use of inheritance, there will be many combinations, the use of decorative classes, with greater flexibility.
Role
- Component (abstract artifacts): it is a common parent of concrete artifacts and abstract decorative classes, declaring the business methods implemented in concrete artifacts
- Concretecomponent (concrete component): It is a subclass of an abstract component class that defines a concrete artifact object (the decorator), implements the method declared in the abstract artifact, and the adorner can add additional responsibilities (methods) to it.
- Decorator (Abstract decoration Class): It is also a subclass of abstract component classes, used to add responsibilities to specific components, but the specific responsibilities are implemented in their subclasses. It maintains a reference to an abstract artifact object that can invoke the method of decorating the previous artifact object and extend the method through its subclasses to achieve the purpose of decorating.
- Concretedecorator (Specific decoration Class): It is a subclass of an abstract adornment class that is responsible for adding new responsibilities to the component. Each specific adornment class defines some new behaviors that can invoke methods defined in the abstract adornment class, and can add new methods to augment the behavior of the object.
The UML class diagram looks like this:
Specific implementation
- Program Scenario Description: A girl to see a boyfriend, makeup is a big problem, after all, girl heart.
- can choose to face and make-up, make-up can be thrush, lipstick, hair and so on different ways of make-up. You can choose a different combination, and there are many possible ways to use inheritance. You can use the decorative mode, using a combination of better to choose makeup. Can be very intuitive to reflect the advantages of decorative mode.
Womenface.java
package com.zy.disignpattern;publicinterface WomenFace { publicvoidouting();}
Womenfaceimpl.java
package com.zy.disignpattern;publicclass WomenFaceImpl implements WomenFace { @Override publicvoidouting() { System.out.println("今天晚上有约会,怎么出门呢"); }}
Womenfacedecorator.java
package Com.zy.disignpattern; public abstract class womenfacedecorator implements womenface { public womenface womenface; public womenfacedecorator (Womenface womenface) {this . Womenface = Womenface; } @Override public void outing () {//TODO Auto-genera Ted Method stub this . womenface.outing (); }}
Naturalwomenfacedecorator.java
package com.zy.disignpattern;publicclass NaturalWomenFaceDecorator extends WomenFaceDecorator { publicNaturalWomenFaceDecorator(WomenFace womenFace) { super(womenFace); } publicvoidouting() { super.outing(); System.out.println("男朋友喜欢素颜,不化妆了吧"); }}
Eyebrowwomenfacedecorator.java
package com.zy.disignpattern;publicclass EyeBrowWomenFaceDecorator extends WomenFaceDecorator { publicEyeBrowWomenFaceDecorator(WomenFace womenFace) { super(womenFace); } publicvoidouting() { super.outing(); System.out.println("画个浓眉毛"); }}
Lipstickwomenfacedecorator.java
package com.zy.disignpattern;publicclass LipstickWomenFaceDecorator extends WomenFaceDecorator { publicLipstickWomenFaceDecorator(WomenFace womenFace) { super(womenFace); } publicvoidouting() { super.outing(); System.out.println("画个口红"); }}
Test.java
Package Com.zy.disignpattern; Public classTest { Public Static void Main(string[] args) {Womenface Womenface =NewWomenfaceimpl (); Womenface.outing (); System. out. println ("-----------------------"); Naturalwomenfacedecorator WomenFace1 =NewNaturalwomenfacedecorator (Womenface); Womenface1.outing (); System. out. println ("-----------------------"); Eyebrowwomenfacedecorator WomenFace2 =NewEyebrowwomenfacedecorator (Womenface); Womenface2.outing (); System. out. println ("-----------------------"); Lipstickwomenfacedecorator WomenFace3 =NewLipstickwomenfacedecorator (Womenface); Womenface3.outing (); System. out. println ("-----------------------"); Eyebrowwomenfacedecorator WomenFace4 =NewEyebrowwomenfacedecorator (NewLipstickwomenfacedecorator (Womenface)); Womenface4.outing (); }}
Run results
今天晚上有约会,怎么出门呢-----------------------今天晚上有约会,怎么出门呢男朋友喜欢素颜,不化妆了吧-----------------------今天晚上有约会,怎么出门呢画个浓眉毛-----------------------今天晚上有约会,怎么出门呢画个口红-----------------------今天晚上有约会,怎么出门呢画个口红画个浓眉毛
Advantages and Disadvantages
- Pros: With decorative mode, it provides the ability to extend objects more flexibly than using inheritance, which can dynamically increase the functionality of objects, and can be arbitrarily combined with these features
- Cons: Because of the ability to combine functions at will, there may be some unreasonable logic at times.
Reference
Java design mode "Decorator pattern" decoration mode
Java decoration mode
Easy to learn design patterns-Guo Zhikong Authoring
Decorative mode of design mode (Decorator)