tailieunhanh - Applied Java Patterns Stephen phần 4

Một xem xét khi thực hiện mô hình này là liệu mỗi thành phần phải có một tham chiếu vào thùng chứa của nó (composite). Lợi ích của việc tham khảo như là nó giúp giảm bớt các mâu thuẫn của cây, nhưng nó cũng làm giảm tính linh hoạt của bạn. | A general consideration when implementing this pattern is whether each component should have a reference to its container composite . The benefit of such a reference is that it eases the traversal of the tree but it also decreases your flexibility. Benefits and Drawbacks The Composite pattern provides a powerful combination considerable flexibility of structure and an extremely manageable interface. The structure can be changed at any time by calling the appropriate methods on a Composite to add or remove Components. Changing a Composite s Components means you re able to change the behavior of the Composites. No matter where you are in the tree structure you can call the same method on each of the individual components. The use of interfaces further increases the flexibility. Interfaces allow the construction of frameworks using the Composite pattern and they enable the introduction of new types at runtime. At the same time use of interfaces can be a drawback when you want to define attributes and provide default implementations in order to let each of the nodes inherit behavior. In that case the Component needs to be an abstract class. Another drawback of the pattern arises from its flexibility because it is so dynamic the Composite pattern is often difficult to test and debug. It normally requires a more sophisticated test validation strategy that is designed around the concept of the whole-part object hierarchy. If testing becomes a problem the best approach is to build the testing into the Composite class implementation. Additionally the Composite normally requires full advance knowledge of the structure being modeled in other words a full class design for the Composite or a more sophisticated class-loading mechanism. The interface form of this pattern discussed in the Pattern Variants section can be a useful alternative for providing dynamic behavior during runtime. Pattern Variants Some variations on the base Composite pattern include The root node - To .

TỪ KHÓA LIÊN QUAN