We are well into this whole patterns thing now, picking up lots of new lingo, and becoming better at communicating with other developers. Next on the list: the observer pattern. This is an important pattern to use to help prevent tight coupling and keep code separated into discrete objects. This pattern allows communication between two objects at particular times without each object needing to be too tightly coupled to the other.
The Observer in the Physical World
The idea behind the observer is like a subscription. Do you have a newspaper or magazine subscription? Have you signed up for cell phone texts from your favorite band when they have new tour dates? The act of you signing up with the newspaper, the magazine, or the band, is you saying you want a subscription, or that you want to become an observer. When the subject (newspaper, magazine, band) has something new to share (on a schedule or not) they run down their subscriber list, and notify the observers by mail, or text.
The Observer in the Software World
On the software side, the idea is the same. Your subject needs to provide a way to take subscriptions, hold a list of observers, and when a particular state changes, notify them. There is usually a couple of methods on the subject that takes in an interface that is used for getting on or off the subscription list. On this interface is also the “call back” method. This is the method that the subject will execute when it wants to inform the observers of state changes.
We have looked at the Factory, the Adapter, and the Singleton patterns. Now we look at the Decorator. The Decorator is similar to the Adapter, but with a subtle difference. With our ReptileAdapter, we wrapped the Reptile class to map the interface methods of Animal to the appropriate methods on Reptile. With the Decorator we also wrap a class, but to add functionality, not to map it or replace the functionality.
The Decorator in the Physical World
The idea behind decorating things in the real world is pretty much how it sounds. We wrap an object with new functionality, while keeping the old functionality. How about a camera in a waterproof housing? When closed up, we have added the functionality of waterproofing, but we still provide a way to press the buttons and turn the dials so that the camera can be operated.
The Decorator in the Software World
On the software side, the idea is the same. It is usually used when we have one object that we like how it functions, but we want to augment the behavior for a different situation. Done correctly, we can keep inserting one object inside another adding functionality at every step.
The last pattern we looked at was the Adapter pattern. It was a good pattern to start with as it maps well to the physical world and is fairly simple. Now we will look at the Singleton pattern. In some respects it is the simplest of patterns, but there are some things to think about.
The Singleton in the Physical World
The idea behind the Singleton is to ensure that there can be only one (insert Highlander jokes here). If you had a factory that made widgets, you would want a widget to be created many many times. But there is only one factory. If you were modeling the US government, you would only want one president. Only one senate.
The Singleton in the Software World
On the software side, the idea is the same. It is usually used when we only want one set of configuration variables, one controller, one data base connection pool, that kind of thing.
Fatal error: Uncaught Error: Call to undefined function eregi() in /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php:140
#0 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(76): wp_codebox_is_windowsie()
#1 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(50): wp_codebox_highlight_geshi(Array)
#2 [internal function]: wp_codebox_highlight(Array)
#3 /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php(134): preg_replace_callback('/<p>\\s*91751126...', 'wp_codebox_high...', '<div class="ser...')
#4 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/class-wp-hook.php(286): wp_codebox_after_filter('<div class="ser...')
#5 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/plugin.php(203): WP_Hook->apply_filters('<div class="ser...', Array)
#6 /homepages/40/d88105018/htdocs/cww/blog/wp-includes/post-template.php(240): apply_filters('the_content', 'Annotations all...')
#7 /homepages/40/d88105018/htdoc in /homepages/40/d88105018/htdocs/cww/blog/wp-content/plugins/wp-codebox/main.php on line 140