English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article mainly introduces the implementation of a simple Decorator design pattern in Java:
Let's first take a look at the class diagram of the Decorator design pattern:
From the diagram, we can see that we can decorate any implementation class of the Component interface, including the decorators themselves, and the decorators themselves can also be decorated.
This is a simple implementation of the Decorator design pattern in Java, starting with the basic addition of coffee, and can continue to add milk, chocolate, and sugar decorators.
interface Component { void method(); } class Coffee implements Component { @Override public void method() { // TODO Auto-generated method stub System.out.println("Pour coffee"); } } class Decorator implements Component { public Component comp; public Decorator(Component comp) { this.comp = comp; } @Override public void method() { // TODO Auto-generated method stub comp.method(); } } class ConcreteDecorateA extends Decorator { public Component comp; public ConcreteDecorateA(Component comp) { super(comp); this.comp = comp; } public void method1() { System.out.println("Pour milk"); } public void method2() { System.out.println("Add sugar "); } public void method() { super.method(); method1(); method2(); } } class ConcreteDecorateB extends Decorator { public Component comp; public ConcreteDecorateB(Component comp) { super(comp); this.comp = comp; } public void method1() { System.out.println("Add chocolate"); } public void method() { super.method(); method1(); } } public class TestDecoratePattern { public static void main(String[] args) { Component comp = new Coffee(); comp.method(); System.out.println("--------------------------------------------------"); Component comp1 = new ConcreteDecorateA(comp); comp1.method(); System.out.println("--------------------------------------------------"); Component comp2 = new ConcreteDecorateB(comp1); comp2.method(); System.out.println("--------------------------------------------------"); Component comp3 = new ConcreteDecorateB(new ConcreteDecorateA(new Coffee())); comp3.method(); System.out.println("--------------------------------------------------"); Component comp4 = new ConcreteDecorateA(new ConcreteDecorateB(new Coffee())); comp4.method(); } }
Running Result:
That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yelling Tutorial more.
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)