JAVA and mode 9th-decoration Mode

Source: Internet
Author: User

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.


--------------------------------------------------------------------------------

Decoration mode structure

The decoration mode dynamically attaches more responsibilities to an object in a transparent manner to the customer. In other words, the client does not think that objects are different before and after decoration. The decoration mode can extend the functions of objects without creating more child classes.

The decoration mode class diagram is as follows:

 

Roles in the decoration mode include:

● Abstract Component role: this role provides an abstract interface to standardize the objects for receiving additional responsibilities.

● ConcreteComponent role: defines a class to 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 the attached responsibility to the component object.

Source code
Abstract component role

[Java]
Package com. bankht. Decorator;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:21:22 AM
*
* @ Class description: Abstract component role
*/
Public interface Component {
 
Public void sampleOperation ();
 
}
Package com. bankht. Decorator;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:21:22 AM
*
* @ Class description: Abstract component role
*/
Public interface Component {

Public void sampleOperation ();

}
 
Specific Component roles

[Java]
Package com. bankht. Decorator;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:22:05 AM
*
* @ Class description: Specific Component roles
*/
Public class ConcreteComponent implements Component {
 
@ Override
Public void sampleOperation (){
// TODO Auto-generated method stub
// Write Related Business Code
}
 
 
}
Package com. bankht. Decorator;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:22:05 AM
*
* @ Class description: Specific Component roles
*/
Public class ConcreteComponent implements Component {

@ Override
Public void sampleOperation (){
// TODO Auto-generated method stub
// Write Related Business Code
}


}
 
Decoration role

[Java]
Package com. bankht. Decorator;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:03 AM
*
* @ Class description: decorative role
*/
Public class Decorator implements Component {
Private Component component;
 
Public Decorator (Component component ){
This. component = component;
}
 
@ Override
Public void sampleOperation (){
// Delegate to component
Component. sampleOperation ();
}
 
}
Package com. bankht. Decorator;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:03 AM
*
* @ Class description: decorative role
*/
Public class Decorator implements Component {
Private Component component;

Public Decorator (Component component ){
This. component = component;
}

@ Override
Public void sampleOperation (){
// Delegate to component
Component. sampleOperation ();
}

}
Specific decoration roles

[Java]
Package com. bankht. Decorator;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:34 AM
*
* @ Class description: Specifies the decoration role.
*/
Public class ConcreteDecoratorA extends Decorator {
 
Public ConcreteDecoratorA (Component component ){
Super (component );
}
 
@ Override
Public void sampleOperation (){
Super. sampleOperation ();
// Write Related Business Code
}
}
Package com. bankht. Decorator;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:34 AM
*
* @ Class description: Specifies the decoration role.
*/
Public class ConcreteDecoratorA extends Decorator {

Public ConcreteDecoratorA (Component component ){
Super (component );
}

@ Override
Public void sampleOperation (){
Super. sampleOperation ();
// Write Related Business Code
}
}

 

[Java]
Package com. bankht. Decorator;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:34 AM
*
* @ Class description: Specifies the decoration role.
*/
Public class ConcreteDecoratorB extends Decorator {
 
Public ConcreteDecoratorB (Component component ){
Super (component );
}
 
@ Override
Public void sampleOperation (){
Super. sampleOperation ();
// Write Related Business Code
}
}
Package com. bankht. Decorator;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:23:34 AM
*
* @ Class description: Specifies the decoration role.
*/
Public class ConcreteDecoratorB extends Decorator {

Public ConcreteDecoratorB (Component component ){
Super (component );
}

@ Override
Public void sampleOperation (){
Super. sampleOperation ();
// Write Related Business Code
}
}
Example of Qi tiandasheng
Sun Wukong has a change, and each of his changes brings him an additional skill. When he becomes a fish, he can swim in the water; when he becomes a bird, he can fly in the sky.

In this example, the role of Component is played by the famous Qi Tiansheng; the role of ConcreteComponent belongs to the master of dasheng, that is, Zookeeper himself; the role of Decorator is played by the 7th change of dasheng. The role of ConcreteDecorator is the change of fish and birds.

 

Source code
The abstract component role "" interface defines a move () method, which must be implemented by all the specific component classes and decoration classes.

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:28:23 AM
*
* @ Class description: The abstract component role "" interface defines a move () method, which must be implemented by all the specific component classes and decoration classes.
*/
// The grand Holy privilege
Public interface TheGreatestSage {
 
Public void move ();
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:28:23 AM
*
* @ Class description: The abstract component role "" interface defines a move () method, which must be implemented by all the specific component classes and decoration classes.
*/
// The grand Holy privilege
Public interface TheGreatestSage {

Public void move ();
}
 

 

Specific Component roles: "Big Sage"

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:28:50 AM
*
* @ Class description: Specifies the component role "" class.
*/
Public class Monkey implements TheGreatestSage {
 
@ Override
Public void move (){
// Code
System. out. println ("Monkey Move ");
}
 
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:28:50 AM
*
* @ Class description: Specifies the component role "" class.
*/
Public class Monkey implements TheGreatestSage {

@ Override
Public void move (){
// Code
System. out. println ("Monkey Move ");
}

}
Abstract decoration role "72 changes"

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:29:24 AM
*
* @ Class description: The abstract decoration role is changed in"
*/
Public class Change implements TheGreatestSage {
Private TheGreatestSage sage;
 
Public Change (TheGreatestSage sage ){
This. sage = sage;
}
 
@ Override
Public void move (){
// Code
Sage. move ();
}
 
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:29:24 AM
*
* @ Class description: The abstract decoration role is changed in"
*/
Public class Change implements TheGreatestSage {
Private TheGreatestSage sage;

Public Change (TheGreatestSage sage ){
This. sage = sage;
}

@ Override
Public void move (){
// Code
Sage. move ();
}

}
Specific decorative role "fish"

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:29:47 AM
*
* @ Class description: The specific decoration role "fish"
*/
Public class Fish extends Change {
 
Public Fish (TheGreatestSage sage ){
Super (sage );
}
 
@ Override
Public void move (){
// Code
System. out. println ("Fish Move ");
}
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:29:47 AM
*
* @ Class description: The specific decoration role "fish"
*/
Public class Fish extends Change {

Public Fish (TheGreatestSage sage ){
Super (sage );
}

@ Override
Public void move (){
// Code
System. out. println ("Fish Move ");
}
}
 
Specific decorative role "bird"

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:30:11 AM
*
* @ Class description: Specifies the decoration role "bird"
*/
Public class Bird extends Change {
 
Public Bird (TheGreatestSage sage ){
Super (sage );
}
 
@ Override
Public void move (){
// Code
System. out. println ("Bird Move ");
}
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:30:11 AM
*
* @ Class description: Specifies the decoration role "bird"
*/
Public class Bird extends Change {

Public Bird (TheGreatestSage sage ){
Super (sage );
}

@ Override
Public void move (){
// Code
System. out. println ("Bird Move ");
}
}
Client type

[Java]
Package com. bankht. Decorator. wukong;
 
/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:30:32 AM
*
* @ Class description: client class
*/
Public class Client {
 
Public static void main (String [] args ){
TheGreatestSage sage = new Monkey ();
// Method 1
TheGreatestSage bird = new Bird (sage );
TheGreatestSage fish = new Fish (bird );
// Method 2
// TheGreatestSage fish = new Fish (new Bird (sage ));
Fish. move ();
Bird. move ();
}
 
}
Package com. bankht. Decorator. wukong;

/**
* @ Author: special soldier-AK47
* @ Creation Time: 09:30:32 AM
*
* @ Class description: client class
*/
Public class Client {

Public static void main (String [] args ){
TheGreatestSage sage = new Monkey ();
// Method 1
TheGreatestSage bird = new Bird (sage );
TheGreatestSage fish = new Fish (bird );
// Method 2
// TheGreatestSage fish = new Fish (new Bird (sage ));
Fish. move ();
Bird. move ();
}

}

 

The "big Sage" is a ConcreteComponent class, while the "Birds" and "fish" are decorative classes. You need to describe the "Big Sage", that is, the "zookeeper" instance.

In the above example, the system has decorated a saint from a bird to a bird (adding the functionality of the bird to the bird ), then, the birds were decorated as a fish (adding the functions of the fish to the slogan + birds and getting the slogan + bird + fish ).

 

As shown in, the major changes first attach the functionality of the bird to zookeeper, and then the functionality of the fish to the birds.

  

 

Author: m13666425773

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.