This article brings you relevant knowledge about java, which mainly introduces related issues about design patterns. It mainly refers to the related content of decorator pattern without changing existing objects. In the case of structure, we can dynamically add some responsibility patterns to the object. I hope it will be helpful to everyone.
## Recommended study: "java video tutorial"
Definition of the Decorator pattern: refers to a pattern that dynamically adds some responsibilities to the object (that is, adds its additional functions) without changing the existing object structure. It belongs to the object Structural pattern.
Advantages1. Decorators are a powerful supplement to inheritance and are more flexible than inheritance. They can dynamically extend functions for an object without changing the original object, that is, plug-in Ready to useDisadvantages2. Different effects can be achieved by using unused decorative classes and the arrangement and combination of these decorative classes
3. The decorator mode fully complies with the opening and closing principle
The decorator pattern will add many subclasses, and excessive use will increase the complexity of the program.Knowledge Points
Normally, extending the functionality of a class is achieved using inheritance. However, inheritance has static characteristics and a high degree of coupling, and as extended functions increase, subclasses will expand. If you use composition relationships to create a wrapper object (that is, a decoration object) to wrap the real object and provide it with additional functionality while keeping the class structure of the real object unchanged, this is the goal of the decorator pattern. Let's analyze its basic structure and implementation method.Implementation of Decorator Pattern
Case: Hei Xiaohu caught the Seven Heroes and even roasted Hongmao Abstract Component (Component) Role: Seven Heroes
ConcreteComponent role: Rainbow Cat
Abstract Decorator role: Add materials
Concrete Decorator role: Add salt and cumin.
Qixia interface declares a barbecue abstract methodHongmaopublic interface Qi { void show();}Copy after login
Hongmao class Implement the Qixia interface and implement the method of roasting rainbow catsAdd ingredientspublic class Hong implements Qi { @Override public void show() { System.out.println("烧烤一个虹猫"); }}Copy after login
This is an abstract decorator that implements the Qixia interface and declares a Qixia Xia attribute, used to call Qixia's barbecue methodadd saltpublic class JiaLiao implements Qi { private Qi qi; JiaLiao() { } JiaLiao(Qi component) { this.qi = component; } @Override public void show() { qi.show(); }}Copy after login
Inherits the abstract class, and rewrites the barbecue method, adding a saltAdd cuminpublic class Yan extends JiaLiao { private Qi qi; Yan() { } Yan(Qi qi) { super(qi); } @Override public void show() { super.show(); add(); } public void add() { System.out.println("加盐"); }}Copy after login
Inherited the abstract class, rewritten the barbecue method, and added a cuminTestpublic class ZiRan extends JiaLiao { private Qi qi; ZiRan() { } ZiRan(Qi qi) { super(qi); } @Override public void show() { super.show(); add(); } public void add() { System.out.println("加孜然"); }}Copy after login
newA rainbow cat, roast it.SummaryAdd some salt
Add some salt and cuminpublic class Demo { public static void main(String[] args) { Qi qi = new Hong(); qi.show(); System.out.println(); Qi qi1 = new Yan(qi); qi1.show(); System.out.println(); Qi qi2 = new ZiRan(qi1); qi2.show(); }}Copy after login
Not only can you grill rainbow cats, but you can also grill them. For other Seven Heroes, you only need to add a class to implement the Seven Heroes interface.Recommended study: "Moreover, adding salt or cumin will not change the original meat quality of Hongmao.
java video tutorial"
The above is the detailed content of Summarize and organize JAVA decorator patterns (detailed examples). For more information, please follow other related articles on the PHP Chinese website!