Decorator design pattern - gene zeiniss

Jiedao jdon 2021-05-04 07:54:14
decorator design pattern gene zeiniss

Decorator patterns can use aggregation to combine runtime behavior .“ polymerization aggregation It's the process of combining things or quantities into a single group or total ”. One of the synonyms for aggregation is “ Pile up heap”, The pile is “ A disorderly pile or mass of things ”. I think this definition is OK . For me, there's only one thing that destroys it . If aggregation is a process , And heaps ( An untidy pile ) It's the result of this process , So why are these two words synonymous ?

Java Abstraction in language

stay Java in , Aggregation is easier . This is the relationship between classes represented by one-way associations . Heap is the embodiment of aggregation .

Have a look . This is an object . It has a type and a name . It can exist .

public class Thing {
   String type;
   String name;

This is a pile . It has a height ( A lot / huge ……) And location , But no Thing It can't exist .

public class Heap {
   private String height;
   private String location;
   private List<Thing> thing;

Only if you have something like clothes Thing when , You can create a bunch of heap clothes . Pick off layers of clothes and throw them untidy on the floor

public Heap createHeapOfClothes() {
   Heap heap = new Heap();
   heap.setLocation("on the bathroom floor");
           Thing.builder().type("clothes").name("right sock").build(),
           Thing.builder().type("clothes").name("left sock").build()));
   return heap;

I'm not just proposing aggregation . Decorator patterns can use aggregation to combine runtime behavior .

Decorator mode

Decorator pattern is a design pattern , It allows you to dynamically add behavior to a single object , Without affecting the behavior of other objects in the same class . It's a structural pattern , Extend the behavior of objects by constructing wrappers around them . Consider layering . Every floor is decorated with ornaments , Put a layer on the package .

Foundation class :

public class AutumnOutfit extends Outfit {
   public void describeTop() {"white sweater"); }
   public void describeBottom() {"ripped jeans"); }
   public void describeFootwear() {"veja sneakers"); }

Now? , You need a Outwear Decorator, To use aggregation to add more clothing to basic clothing . You will need to use subclasses of the abstract decorator class to add to the basic costume . As we discussed earlier ,“ Clothing adorner ” yes “ clothing ” Subclasses of . therefore , Concrete decorators are also a subclass of clothing .

public abstract class OuterwearDecorator extends Outfit {
   public abstract void describeOuterwear();
   public void createDailyLook() {

The basic decorator has a new abstract behavior describeOuterwear(), It will be implemented by specific decorators . also , It covers the createDailyLook() Method .

The next step is to implement the abstract method . Create a concrete jacket decorator .

public class CoatDecorator extends OuterwearDecorator {
   private final Outfit outfit;
   public void describeOuterwear() {"camel coat");}
   public void describeTop() { outfit.describeTop();}
   public void describeBottom() { outfit.describeBottom();}
   public void describeFootwear() { outfit.describeFootwear(); }

Decorator classes behave like another class that implements abstract methods . The only difference is , It aggregates the base class . It will allow some behaviors to be stacked together .

Calling code :

public void createAutumnOutfitWithCoat() {
   Outfit autumnOutfit = seasonOutfitFactory.createOutfit(Season.AUTUMN);
   autumnOutfit = new CoatDecorator(autumnOutfit);

The important thing is that the basic clothing must be a pile of heap The first one in . Next , Wrap it in a coat decorator . And then call the createDailyLook() Method , To draw and cut the described Garment . Your abstract decorator simply delegates daily appearance creation to its aggregated Outfit object . Check the log . Running results :

AutumnOutfit - white sweater
AutumnOutfit - ripped jeans
AutumnOutfit - veja sneakers
CoatDecorator - camel coat
Outfit - drawing outfit according to selections
Outfit - cutting drawn outfit

This is the article Of git project Of link


本文为[Jiedao jdon]所创,转载请带上原文链接,感谢

  1. Error in installing qtsdk in centos7.6: you need a C + + compiler
  2. Java集合(复习整理)
  3. Java collection (review)
  4. Hibernate的fetch问题,都琢磨一晚上了
  5. Hibernate's fetch problem has been pondering all night
  6. Kafka原理分析之基础篇
  7. The basis of Kafka principle analysis
  8. 请bang老师帮我看下这样说面向服务和面向对象的关系是否正确,谢谢
  9. struts ActionForm设计的缺陷
  10. 如何从weblogic集群环境中获得一个唯一对象
  11. 使用Redis缓存和Spring AOP使Spring Boot应用更健壮?
  12. Please help me see if the relationship between service-oriented and object-oriented is correct. Thank you
  13. Defects of struts ActionForm design
  14. How to get a unique object from Weblogic cluster environment
  15. Using redis cache and spring AOP to make spring boot application more robust?
  16. Kafka原理分析之基础篇
  17. The basis of Kafka principle analysis
  18. Java应用服务器死了
  19. tomcat session出现问题
  20. java+互联网架构人才
  21. UML和Java的阻抗
  22. The Java application server is dead
  23. There is a problem with Tomcat session
  24. Java + Internet architecture talents
  25. UML and Java
  26. 深入理解Linux内核之内核线程(上)
  27. 如何看待明尼苏达大学因插入实验性漏洞,被禁止贡献 Linux 内核代码?
  28. 梁金荣:使用eBPF追踪LINUX内核
  29. Docker study notes (2)
  30. Deep understanding of kernel threads in Linux kernel (Part one)
  31. How do you think the University of Minnesota is forbidden to contribute Linux kernel code because of the experimental loophole?
  32. Liang Jinrong: tracking Linux kernel with ebpf
  33. 2021Java最新学习路线图新鲜出炉
  34. New learning roadmap for 2021 Java
  35. Spring MVC 的Controller 结构问题
  36. 【烂KDE】Linux登录时用户密码正确,却登录失败
  37. Controller structure of spring MVC
  38. [KDE] when Linux logs in, the user password is correct, but the login fails
  39. HttpSession对象与Cooike的关系 以及 Cookie对象构造函数问题
  40. gRPC-Web:替代REST的gRPC的Javascript库包
  41. java call dll
  42. 用Java构建反应式REST API - Kalpa Senanayake
  43. The relationship between httpsession object and cooike and the construction of cookie object
  44. Grpc Web: a JavaScript library package to replace rest grpc
  45. java call dll
  46. Building reactive rest API with Java - kalpa Senanayake
  47. Five wonders. Feeling of visiting villa in spring
  48. Spring will be far away
  49. 最全面的SpringBoot配置文件详解
  50. 最全面的SpringBoot配置文件详解
  51. 微服务开发神器之JRebel 插件破解和实现本地及远程热部署教程
  52. 漫画 | 你还记得原生的JDBC怎么连接数据库吗?
  53. Java流及流操作示例
  54. The most comprehensive spring boot configuration file
  55. The most comprehensive spring boot configuration file
  56. Cracking and implementing jrebel plug-in of microservice development artifact
  57. Comic | do you remember how the native JDBC connects to the database?
  58. Java stream and stream operation example
  59. ftp连接windows与linux
  60. 装饰器设计模式 - Gene Zeiniss