This article describes a case of dyed steamed buns implemented by Java based on the decorator pattern. Share it for your reference, as follows:
1. Pattern definition
The decorator pattern dynamically extends an object function without changing the original file and using inheritance. It wraps the real object by creating a wrapping object, that is, decoration.
Decorative objects and real objects have the same interface, so that client objects can interact with decorative objects in the same way as real objects.
A decorative object contains a reference to a real object.
2. Examples of the model
1. Pattern analysis
We use the case of black-hearted vendors to make dyed buns to illustrate this model.
2. Decorator mode static class diagram
3. Code example
3.1 Create a steamed bun interface - IBread
package com.demo.abs;/** * steamed bun processing interface* * @author * */public interface IBread { // Prepare the materials public void prepair(); // Harmony public void kneeFlour(); // Steamed buns public void steamed(); /** * Processing steamed buns*/ public void process();}3.2 Normal bun implementation - NormalBread
package com.demo.abs;/** * Implementation of normal steamed buns* * @author * */public class NormalBread implements IBread { // Prepare the ingredients public void prepair() { System.out.println("Prepare flour, water and baking powder..."); } // Hemian public void kneeFlour() { System.out.println("Hmian..."); } // Steamed buns public void steamed() { System.out.println("Steamed buns... The fragrant steamed buns are out of the oven!"); } /** * Processing steamed buns*/ public void process() { // Prepare the ingredients prepair(); // KneadFlour(); // Steamed steamed(); }}3.3 Create an abstract decorator - AbstractBread
package com.demo.decorator;import com.demo.abs.IBread;/** * Abstract decorator* * @author * */public abstract class AbstractBread implements IBread { // Store incoming IBread object private final IBread bread; public AbstractBread(IBread bread) { this.bread = bread; } // Prepare the material public void prepare() { this.bread.prepair(); } // Gently public void kneeFlour() { this.bread.kneadFlour(); } // Steamed buns public void steamed() { this.bread.steamed(); } // Processing steamed buns public void process() { prepair(); kneeFlour(); steamed(); }}3.4 Create a dye decorator - CornDecorator
package com.demo.decorator;import com.demo.abs.IBread;/** * Dyeed corn buns* * @author * */public class CornDecorator extends AbstractBread { // Construct method public CornDecorator(IBread bread) { super(bread); } // Black-hearted vendors have started dyeing public void paint() { System.out.println("Add lemon yellow colorant..."); } // Overload the parent class's dough method @Override public void kneeFlour() { // This.paint() is started after adding dye to the flour; // paste super.kneadFlour(); }}3.5 Create a scalypse decorator - SweetDecorator
package com.demo.decorator;import com.demo.abs.IBread;/** * Cyperus steamed bun* * @author * */public class SweetDecorator extends AbstractBread { // Construct method public SweetDecorator(IBread bread) { super(bread); } // Black-hearted vendors start adding cyperus public void paint() { System.out.println("Add cyperus..."); } // Overload the parent class's dough method @Override public void kneeFlour() { // Start dough this.paint() after adding cyperus to flour; // Hemma super.kneadFlour(); }}3.6 Producing sweet corn steamed buns - Client
package com.demo;import com.demo.abs.IBread;import com.demo.abs.NormalBread;import com.demo.decorator.CornDecorator;import com.demo.decorator.SweetDecorator;/** * Client application* * @author * */public class Client { /** * @param args */ public static void main(String[] args) { // Production of decorative steamed buns System.out.println("/n======Start decorative steamed buns!!!"); // Create a normal normal steamed bun instance// This is an object instance that we need to wrap (decorate) IBread normalBread = new NormalBread(); // Let’s start decorating the normal steamed buns! ! ! // Use saccharin to decorate the steamed buns normalBread = new SweetDecorator(normalBread); // Use lemon yellow colorant to decorate the steamed buns normalBread = new CornDecorator(normalBread); // Production of steamed bun information normalBread.process(); System.out.println("=====Decorate the steamed buns end!!!"); }}4. Operation results
====Start decorating steamed buns! ! !
Prepare flour, water and baking powder...
Add lemon yellow colorant...
Add cyanin...
Grease...
Steamed buns...the fragrant steamed buns are out of the oven!
====The decorative steamed bun ends! ! !
3. The design principles of this model
1 Closed change part
2 "Open and Close" Principle
3 Abstract oriented programming
4. Prefer combinations rather than inheritance
4. Use occasions
1. When we need to add a new feature or responsibility to an existing object dynamically, we can consider using the Decorator pattern.
2. When the responsibilities of an object often change or the responsibilities need to be dynamically increased, avoid adding inherited subclass expansion to adapt to such changes, because this method will cause the subclass to expand too quickly and be difficult to control. At this time, the decorator mode can be used.
5. Decorator mode static class diagram
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.