------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------
The basic Java Decoration Category:
Brief introduction:
First of all, he is a design pattern belongs to one of 23 design patterns, English is called decorator pattern. Also called decorator mode. Decoration mode is not necessary to change the original type of text
and the ability to dynamically extend an object using inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object
Benefits:
1. The purpose of the decorator pattern and the inheritance relationship is to extend the functionality of the object, but decorator can provide more flexibility than inheritance.
2. Designers can create a combination of many different behaviors by using different decorative classes and arranging combinations of these decorations.
Principle:
1. Multi-use combination, less inheritance.
The behavior of using inheritance design subclasses is statically determined at compile time, and all subclasses inherit the same behavior. However, if you can take advantage of the combined approach
The behavior of the extended object can be dynamically extended at run time.
2. External expansion and opening, internal modification close (good cross-platform, can be used inside or not).
What is the use of:
1. You need to extend the functionality of a class or add additional responsibilities to a class.
2. The need to dynamically add functionality to an object, these functions can be re-dynamic revocation.
3. It is necessary to increase the number of functions resulting from the permutations of some basic functions, thus making the inheritance relationship impractical.
4. When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, making
The number of sub-categories has increased explosively. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
code example:
Abstract component role Java code
Package decorator;
/**
* Decorator and common Method interface of the original assembly (abstract component role)
* @author mouca.he
*
*/
Public interface Interfacecomponent {
/**
* Component method Say ()
*
*/
public void say ();
}
Concrete Component role Java code
Package decorator;
/**
* Original component (specific component role)
* @author mouca.he
*
*/
public class Component implements interfacecomponent{
public void Say () {
TODO automatically generate method stubs
System.out.println ("Component.say (): Method of the original component! ");
}
}
Abstract Decorator role Java code
Package decorator;
/**
* abstract decorator
* @author mouca.he
*
*/
Public abstract class Abstractdecorator implements INTERFACECOMPONENT{&NBSP;
Private Interfacecomponent COMPONENT;&NBSP
Public abstractdecorator (interfacecomponent component) {
This.component = component;
}
/**
* pre-processing method for component method
*
*/
protected void Presay ( {};
/**
* Component Method execution post-processing method
*
*/
protected void Aftersay () {};
public void Say () {
Presay ();
Component.say ();
Aftersay ();
};
}
Concrete decorator two Java code
Package decorator;
/**
* Decorator II
* @author mouca.he
*
*/
public class Decoratortwo extends abstractdecorator{
Public Decoratortwo (Interfacecomponent component) {
Super (component);
TODO automatically generates constructor stubs
}
/**
* Overload the template class Presay () method as needed
*/
protected void Presay () {
System.out.println ("Decoratortwo.presay (): Decorator II's Presay () Method! ");
}
/**
* Overload the template class Aftersay () method as needed
*/
protected void Aftersay () {
System.out.println ("Decoratortwo.aftersay (): Decorator II's Aftersay () Method! ");
}
}
Decorator one Java code
Package decorator;
/**
* Decorator One
* @author mouca.he
*
*/
public class Decoratorone extends abstractdecorator{
Public Decoratorone (Interfacecomponent component) {
Super (component);
TODO automatically generates constructor stubs
}
/**
* Overload the template class Presay () method as needed
*/
protected void Presay () {
System.out.println ("Decoratorone.presay (): Decorator one's Presay () Method! ");
}
/**
* Overload the template class Aftersay () method as needed
*/
protected void Aftersay () {
System.out.println ("Decoratorone.aftersay (): Decorator one's Aftersay () Method! ");
}
/**
* Test method
* @param args
*/
public static void Main (string[] args) {
TODO automatically generate method stubs
Interfacecomponent interfacecomponent = new Decoratortwo (new Decoratorone (New Component ()));
Interfacecomponent.say ();
/*
* Console output:
* Decoratortwo.presay (): Decorator two Presay () Method!
* Decoratorone.presay (): Decorator one's Presay () Method!
* Component.say (): The method of the original component!
* Decoratorone.aftersay (): Decorator one's Aftersay () Method!
* Decoratortwo.aftersay (): Decorator two Aftersay () Method!
*/
}
}
------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------
The basic Java Decoration class