Posts

Showing posts with the label Iterator Pattern

Design Patterns Series 14 - Composite Pattern

Image
This post will be about the second pattern in the alliance. In the last post we explored first pattern in this alliance, the Iterator Pattern , and today is the time of  Composite Pattern . The Composite Pattern : The Composite Pattern  is all about creating tree-like structures where the leaves in the structure can be treated in the same way as the branches(which are substructures that can contain multiple leaves, as well as other branches). The idea here is that, to make life easier, you should be able to treat the leaves and compositions of leaves in a tree structure the same. You use the Composite pattern  to " Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformally ". That is typical of Composite Pattern - when you ask a branch to perform some action, it iterates over all its contained leaves and branches. The insight behind The Composite Pattern is r...

Design Patterns Series 13 - Iterator Pattern

This post and the next will be about two allied patterns: the Iterator Pattern and the Composite Pattern . The Iterator Pattern: The Iterator Pattern gives you a way of accessing the elements inside an object without having to know the internal details of that object. When you dealing with a collection of objects, The Iterator Pattern  is the ideal solution. You can use the Iterator Pattern  to " Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. " In other words, iterators are designed to let you handle many different kinds of collections by accessing their members in standard, accepted way, without having to know the internal details of those collections. The Iterator Pattern is especially important when the collection you are creating is made up internally of separate subcollections, as when you have mixed hashes with array lists, for example. The design insight here is one of what’s called...