`
shake863
  • 浏览: 664721 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in Action Script-Composite

 
阅读更多

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()”

 

clip_image002

clip_image004

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.

Interpreter

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 Download Full Project

Enjoy!

分享到:
评论

相关推荐

    Design Patterns in Modern C++--2018

    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 ...

    Design-Patterns-in-Modern-C++

    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 in Modern C++-Apress(2018)

    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

    "Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...

    M. S. Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf

    Joshi - C++ Design Patterns and Derivatives Pricing - CUP[2008]2Ed.pdf》,是关于C++设计模式和衍生品定价的第二版教科书。本书着重讨论在C++中实现金融模型的背景下,设计模式这一面向对象编程的先进范式。...

    GOF-设计模式-Design Patterns-英文原版-高清-有目录-有页码

    **设计模式**(Design Patterns)是软件工程中的一个重要概念,它是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。本章节将从多个角度对设计模式进行深入探讨。 #### 二、设计模式的基本定义 ...

    Design Patterns in C#

    design pattern in C# language

    design-patterns-for-humans设计模式中文翻译版

    https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    Design Patterns in Modern C++

    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...

    dive into design patterns(Alexander Shvets).pdf

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段...

    Design Patterns in PHP and Laravel

    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 ...

Global site tag (gtag.js) - Google Analytics