Notes: Design Patterns
These are notes from the Design Patterns and ColdFusion presentation by Sean Corfield on 12/13/07.
Design patterns are basically common solutions to recurring problems. Using a pattern provides a common vocabulary between developers. If you say, “I’m using a Remote Facade”, someone else will know what you’re talking about without any further explanation.
What makes up a pattern? (a) A name, to provide a common vocabulary between developers. (b) A recurring problem; typically there are “forces”, or circumstances in the coding situation, that will lead you to use the pattern. (c) The solution, or template for solving the problem. Note that a pattern doesn’t have an exact, proper way to be executed. It is indeed just a guide, or template. (d) Consequences: Every pattern has its pros and cons.
Common Problems and Design Patterns
- Problem: Duplicate site content at top and bottom of site.
Pattern Name: Composite View
Solution: Include a header and footer that has this duplicated content. - Problem: Need an instance of an object that is accessible everywhere in an application.
Pattern Name: Singleton
Solution: Create it at application startup and put it in application scope.
Trade offs: Initialization is in one place (+). Easy access to objects app-wide (+). But now your code is very dependent on that application scope, which breaks encapsulation (-). - Problem: Several components make up a particular functionality, but you don’t want to expose all of that complexity to the calling code, especially in case I want to change how I implement it.
Pattern Name: Facade
Solution: Create a new component that has a nice, simple, high-level API. Client code calls it, and it calls all the complex components under the hood.
Trade offs: App code no longer needs to no about all of the complex internals (+). However, there will be redundancy of methods between the API and the underlying code, which is more maintenance and more coding (-). - Problem: I need some standardized code that runs on every request of the application, perhaps security checks.
Pattern Name: Front Controller
Solution: Make all requests go through a common piece of code, and have that code apply the logic before/after processing the actual request. i.e. index.cfm?page=myaction. Everything routes through index.cfm.
Trade offs: Full control over every request; can easily add add’l functionality (+). All URLs look similar (+/-). The control page (index.cfm) can get complicated (-).
Recognize that patterns are NOT code. Any example code for a pattern is just that: An example. Design patterns can apply to any language and typically can be used in the design phase of an app. Some patterns are language-specific. Do patterns have an object-oriented bias? Well, yes. Most design patterns are focused on OO design. But design patterns in general do not have to be OO-specific. It just tends to be OO people who are considering design patterns.
Ruby Patterns for Consideration
- Active Record pattern for managing persistence of objects. An object knows how to save/get itself with a database.
- Dynamic finder methods. By virtue of the method name, the functionality can be figured out. This is done with a method that runs when the called method doesn’t exist (”method_missing”). That method can than act on the method that was called based on its name. ColdFusion can do this with the onMissingMethod() method of a CFC.
Patterns and interfaces. Interfaces are not needed but are useful as a descriptive tool in explaining pattern implementations. ColdFusion 8 introduced <cfinterface>. Alternatively, you could use duck typing.
Design Patterns from Gang of Four
- Creational Patterns
- Abstract Factory, Builder, Factory Method, Prototype, Singleton
- Structural Patterns
- Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
- Behavioral Patterns
- Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor
To learn patterns, you can observe frameworks, which often implement multiple patterns: Front Controller, MVC, Identity Maps, Context Objects, etc.
Thanks for the great presentation, Sean.
