Still remember the Interpreter example? No matter what you type is a
number or an expression; it can calculate the result just by the
clause” new GeneralExpression(inputText.text).interpret()”
It means that we treat the number the same with the expression.
Actually, the expression is form by the numbers. So, you can take the
expression as a container, it contains the numbers, and the number is a
special kind of expression, it calculates itself.
It this picture, you can consider it as a tree. And all the
inner nodes are containers, and the leaf node is the Number. We treat
all the nodes in the same way, just use the interpret() method. So, the
client is very convenient to deal with the expressions. This is what we
introduce today, the composite pattern.
The intent of this pattern is as follows.
Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and
compositions of objects uniformly.
–By GOF BOOK
In the interpreter example, we put the differences of dealing
with the inner node and leaf node into the concrete class. And leave a
common interface for the client to deal with. This is what the
composite pattern does.
This pattern is very useful, especially when you building a
user interface. You can treat display object and display container
object the same way, without considering the differences between the
two objects. It’s very common in the GUI building environment.
Of course, it has its own disadvantages. For example, you need
to carefully avoid two or more composites to form a cycle. Remember, be
a tree, not a cycle!
Download Full Project
Enjoy!
分享到:
相关推荐
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
Design Patterns are also a fun investigation of how a problem can be solved in many different ways, with varying degrees of technical sophistication and different sorts of trade-offs. Some patterns ...
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...
**设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...
design pattern in C# language
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段...
Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...