.
Concretecomponent is a concrete component class that can add new functionality by adding decorations to it.
Decorator is the public parent class for all decorations, which defines the methods that all decorations must implement, and it also holds a reference to component to forward the user's request to the component and may perform additional actions before and after the request is forwarded.
Concretedecoratora and Concretedecoratorb are concret
As the name implies, the decorative mode is to add some new features to an object, and is dynamic, requires decorative objects and objects are decorated to achieve the same interface, decorative objects hold the object of the decoration objects, the diagram is as follows:The source class is a decorated class, and the decorator class is a decorative class that can dynamically add some functionality to the source class, with the following code:[
Java Decorator and javadecoratorDecoration Mode
Dynamically attaches the responsibility to the object. If you want to extend the function, the modifier provides a more flexible alternative solution than inheritance.
Code
Package gx. component;/*** Component: the decoration class and the decorated class must be inherited: to maintain the same type * @ author always **/public abstract class component {public
Decorator mode for Java design modeAdorner modeAdorner mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure.This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.This pattern creates an adornment class that wraps the original class and dynamically adds ad
passed in, that is to show Likeeat likeeat = new Endeat (New Eatfoot (New Inrestaurant (New Gotorestaurant (New Findinmap (person)))))The look of this structure. So when calling Likeeat.eat () , and because of each specific decorator class, first call super.eat (); method, and the Super has been passed by the construction and pointed to a specific decorator class (this can be replaced according to
com.lyz.design.decorator;/** * The implementation class of the person interface man * @author Liuyazhuang * */public class Man implements person {Publi c void Eat () {System.out.println ("Man is Eating");}}DecoratorPackage com.lyz.design.decorator;/** * Decorator abstract class implementation person interface * @author Liuyazhuang * */public abstract class Decorator IM Plements person { protected person
the system runs.2. Designers can create many different combinations of behaviors by using different kinds of specific decorations and arranging combinations of these decorative classes. And inheritance does not have this relationship advantage. Each of these different combinations needs to be well designed in advance by subclass inheritance.3. This is more flexible than inheritance, but also means that the decoration mode is more error-prone than inheritanceDisadvantagesUsing adornment mode res
enhancement behavior of the original concrete component classDecoratora.operation (); System. out. println ("---------------------------\ n"); System. out. println ("---decoratora and decoratorb two times for the original component objects---");//If you need to re-decorate the Decoratora object after the original artifact object is decoratedComponent Decoratorb; Decoratorb =NewConcretedecoratorb (Decoratora); Decoratorb.operation (); }}7. Operation Result:我是具体的构件类(被装饰对象),这是我的原有方法我是具体的装饰类A,我可
Niurou y = new Malaniurou (guodi);/to a spicy beef
maodu x = new Maodu (y);//on the basis of spicy beef and then a big knife belly
System.out.println ("A total point" + X.name () + ", Total consumption" +s.cost ());
}
Output results:
11 A total of the Tai long 燚 hotpot flavor pot of the original pot + spicy beef + broadsword belly, a total consumption of 135
The above is a small set to introduce the Java
definition: dynamically adds some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.
Type: Object-structured pattern
alias : Packing mode (wrapper)
class Diagram:
The decorator decoration pattern is a structured pattern that solves the problem of "excessive use of inheritance to extend the functionality of an object", whic
(component); the } the + Public voidMethodB () - { theSystem.out.println ("I am concretedecoratorb, add new Features");Bayi } the the /** { @inheritDoc } */ - @Override - Public voidoperation () the { the MethodB (); the Super. Operation (); theSystem.out.println ("Concretedecoratorb Operation execution Complete"); - } the the }93//test class94 Public classDemo the { the Public Static voidMain (string[] args) the {98Component Compone
DefinedThe responsibility is dynamically attached to the object. To extend functionality, decorators provide a more resilient alternative than inheritance.Class diagram(Image from headfirst design mode)Java instanceIn online games there are usually a variety of weapons, but also by enchanting to the weapon to add a variety of attack effects, now we use decorator mode to achieve this effect./** * 武器剑类,武器接口的实
The adorner pattern can dynamically add capabilities to existing objects. Next, I'll use a simple example to illustrate how to use the decorator pattern in a program.
1. Decorator Mode
Let's assume that you're looking for a girlfriend. There are a lot of girls from different countries, such as: The United States, China, Japan, France and so on, each of them have a unique personality and hobbies, if you ne
(); System.out.println ("with the phone case."); } Public int Cost() {return -+ This. Component.cost (); }} PackageGx.decorator.impl;ImportGx.component.Component;ImportGx.component.impl.Decorator;/** * Decorate 2: Put a film on your phone * @author always * */ Public class tiemo extends Decorator{ Public Tiemo(){} Public Tiemo(Component Component) { This. component=component; } Public void Description() { This. Component.description (); System
Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39759199Decorator (Decorator) mode: Dynamically attaches responsibility to objects, and to extend functionality, adorners provide a more resilient alternative than inheritance. For decorator mode, it's actually a wrapper, so I'd rather call it a wrapper. The IO stream in Java, like we use
I think any book about the mode can't help but mention its practical application when talking about the decorator mode-applications in the Java/IO library,
I. Introduction (General Introduction to Java I/O)
Regardless of the programming language, input and output are an important part. Java is no exception, and
Concept
The decorator pattern dynamically attaches responsibility to the object. To extend functionality, adorners provide a more flexible alternative than inheritance.
The adorner and the decorated object have the same super type.You can wrap an object with one or more adorners.Since the adorner has the same super type as the decorated object, it can be replaced with a decorated object in any situation where the original object (being wrapped) is
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.