Detail example of Java Decorator pattern
- 2020-10-23 20:05:23
- OfStack
This article gives an example of the Java decorator pattern. To share for your reference, the details are as follows:
Decorative pattern
Dynamically extend the functionality of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapper object, also known as decoration.
The decorator object accepts all requests from the client. It forwards these requests to real objects. Decorator objects can add additional functionality before or after forwarding these requests.
This ensures that additional functionality can be added externally at run time without modifying the structure of a given object. In object-oriented design, extensions to a given class are usually implemented through inheritance.
advantages
The purpose of both Decorator and inheritance is to extend the functionality of an object, but Decorator can provide more flexibility than inheritance.
2. By using different specific decorative classes and the permutation and combination of these decorative classes, designers can create many combinations of different behaviors.
role
The roles in the decorator mode are:
(1) Abstract component (Component) role: Give an abstract interface to the specification of objects ready to receive additional responsibilities.
(2) Concrete artifact (Concrete Component) role: Define a class that will receive additional responsibilities.
(3) Decoration (Decorator) role: it holds an instance of a component (Component) object and implements an interface with abstract component interface 1.
(4) Specific decoration (Concrete Decorator) Role: responsible for adding additional responsibilities to component objects.
The sample code
Let's take a shredded cake as an example. Shredded cake can have many flavors, but the basic one is a original shredded cake. On this basis, we can add one or more kinds of eggs, beef, sirloin and so on.
Abstract component (Component) roles
package Decorator pattern ;
/**
* Abstract class interface, specification of pie properties
* @author oooo
*
*/
public interface Cake {
// Two attributes
public String descripestion();
// Property description
public Double money();
// Price description
}
Specific artifact (Concrete Component) roles
package Decorator pattern ;
/**
* Original shredded cake class
* @author oooo
* The class responsible for receiving additional attributes
*/
public class shreddedcake implements Cake{
// implementation Cake interface
@Override
public String descripestion() {
// Description attribute
return " Plain shredded cake ";
}
@Override
public Double money() {
// Describe the price
return 3.5;
}
}
Decorate (Decorator) characters
package Decorator pattern ;
/**
* Establish a connection with the pie, get the original pie attribute, and add the mediation of the attribute.
* @author oooo
*
*/
public abstract class Deractor implements Cake{
Cake cake;
public Deractor(Cake cake) {
this.cake=cake;
}
public String descripestion(){
return cake.descripestion();
}
public Double money(){
return cake.money();
}
}
Specific decoration (Concrete Decorator) role
package Decorator pattern ;
/**
* Eggs shredded cake class
* @author oooo
* Add egg properties
*/
public class EggshrededCake extends Deractor{
public EggshrededCake(Cake cake) {
super(cake);
}
public String descripestion() {
return " egg "+cake.descripestion();
}
@Override
public Double money() {
return 3.5+1.5;
}
}
Specific decoration (Concrete Decorator) character
package Decorator pattern ;
/**
* Beef shredded cake
* @author oooo
* Add beef attributes
*/
public class beffshredded extends Deractor{
public beffshredded(Cake cake) {
super(cake);
// TODO Auto-generated constructor stub
}
public String descripestion() {
return " beef "+cake.descripestion();
}
@Override
public Double money() {
return cake.money()+3.0;
}
}
The test class
package Decorator pattern ;
public class Test {
/**
* The test class
* @param args
*/
public static void main(String[] args) {
// new 1 Original shredded cake
shreddedcake sh=new shreddedcake();
System.out.println(sh.descripestion());
System.out.print(" Price: "+sh.money());
// new 1 Egg class, with egg attribute attached to grab cake
EggshrededCake egg=new EggshrededCake(sh);
System.out.println(egg.descripestion());
System.out.print(" Price: "+egg.money());
// new 1 A beef type, attached to the beef attribute grab cake
beffshredded beff=new beffshredded(egg);
System.out.println(beff.descripestion());
System.out.print(" Price: "+beff.money());
}
}
Output results:
Plain shredded cake Price: 3.5
Egg plain shredded cake Price: 5.0
Beef and egg plain shredded cake Price: 8.0
For more information about java, please visit Java Data Structure and Algorithm Tutorial, Java Operation of DOM Node Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.
I hope this article has been helpful for java programming.