Does anyone have a good example in Ruby of using a Composite of Commands? It is a design pattern hybrid that I have seen mentioned in various Design Patterns literature which sounds quite powerful, but have not been able to find any interesting use cases or code.
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- How to specify memcache server to Rack::Session::M
相关文章
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
I'm trying to understand this pattern myself, and have been mulling over what things might be modeled this way.
The basic idea of the Composite pattern is situations where items and collections need to be treated, in some respects, the same way. Collections may contain a mixture of items and subcollections, nested as deeply as you like.
Some ideas I have (borrowing some from Design Patterns in Ruby and Ruby Under a Microscope):
A filesystem
You can ask a file for its size, and it returns a simple value. You can also ask a folder for its size, and it return the sum of the sizes of its files and subfolder. The subfolders are of course returning the sum of their files and subfolders.
Likewise, both files and folders can be moved, renamed, deleted, backed up, compressed, etc.
A system command
A command object could have a
run
method. That method could work by running any number of subcommands, which have subcommands, etc. It could return true if all of its subcommands return true, and could report statistics based on its childrens' statistics (time elapsed, files modified, etc).A company hierarchy
Individuals, teams, divisions, and whole companies could all be seen as having salaries, bringing in revenue, completing units of work, etc.
A military unit
In a game, a soldier could have defense and offense statistics, could be told to move to a location, attack a base, etc. Regiments and divisions could be treated the same way.
Weight or monetary value
The weight of a truck full of boxes includes the weight of each box. The weight of each box includes the weight of each of its items, their parts, etc.
Similarly, the monetary value of a financial portfolio is the value of all its assets. Some of those assets might be index funds that contain multiple stocks. You could buy or sell an individual stock or an entire portfolio.
GUI elements
A window could be composed of frames, which are composed of frames, which are composed of frames. Any element can be positioned, shaded, focused, hidden, etc.
Expressions in a programming language
When the Ruby interpreter evaluates an expression, it does so by breaking it down into a tree of expressions (an Abstract Syntax Tree) and evaluating each of those, coming to the final value by working its way back to the top of the tree. In a sense, each level of the tree is asked the same question: "what is your value?"
As a simple example, the first step in finding the value of
((4 + 8) * 2)) + 9
is to find the value of4 + 8
.Inspired by the general idea and the sample pattern implementations in this blog post, here's a stab at what it might look like:
And sample usage using the application of a software installer program: