Sự kết hợp của ảnh gốc và một số nội dung mới hình thức một đối tượng mới. Trong bức ảnh thứ hai được hiển thị trong Hình 2-1, có bốn đối tượng: hình ảnh ban đầu cho thấy bên trái, đối tượng cung cấp một biên giới, và hai đối tượng thẻ với các dữ liệu khác nhau liên quan đến họ. Mỗi người trong số họ là một đối tượng trang trí. Cho rằng số cách khác nhau của hình ảnh trang trí là vô cùng tận, chúng ta có thể có nhiều đối tượng mới. Vẻ đẹp. | Figure 2-1. Decorator pattern illustration a plain photograph and b photograph with tags The combination of the original photo and some new content forms a new object. In the second image shown in Figure 2-1 there are four objects the original photo as shown to the left the object that provides a border and two tag objects with different data associated with them. Each of them is a Decorator object. Given that the number of ways of decorating photos is endless we can have many such new objects. The beauty of this pattern is that The original object is unaware of any decorations. There is no one big feature-laden class with all the options in it. The decorations are independent of each other. The decorations can be composed together in a mix-and-match fashion. Design Now we can specify the players in the Decorator pattern in a UML diagram shown in Figure 2-2. Because this is the first pattern we are describing in UML we ll take it slowly. The UML that we need for patterns in this book is covered in Chapter 1 and summarized in Table 1-1. The essential players in this UML diagram are Component An original class of objects that can have operations added or modified there may be more than one such class Operation An operation in IComponent objects that can be replaced there may be several operations 10 Chapter 2 Structural Patterns Decorator Proxy and Bridge IComponent The interface that identifies the classes of objects that can be decorated Component is one of these Decorator A class that conforms to the IComponent interface and adds state and or behavior there may be more than one such class Figure 2-2. Decorator pattern UML diagram The center of the UML diagram is the Decorator class. It includes two types of relationships with the IComponent interface Is-a The is-a relationshipis shown by a dotted arrow from the Decorator to IComponent indicating that Decorator realizes the IComponent interface. The fact that Decorator inherits from IComponent means that Decorator