What are the causes of System Redesign?
To get started on Part 2 of choosing a design pattern, we need to look at the causes of redesign. I realize that his is jumping ahead in the list, but because the issue of re-design is at the heart of design patterns, it’s a good place for the second discussion of choosing a design pattern. The Gang of Four point out eight causes, and these causes each have design patterns that will help you avoid this redesign (see pp. 23-25 in GoF.) In this session, we’ll review the eight causes and the suggested patterns that can be used to avoid redesign. Look for the cause of redesign that best fits your system or general type of app.
Creating an object by Specifying a Class Explicitly
In one sense, this cause for redesign can be handled by the what you might do in making a movie deal–
Talk to my agent…
You don’t want to deal with the lawyers, contracts and all the negotiations required to make a movie deal. You just want to read the script, rehearse and get your lines straight. You let Murray (your agent) do all of that for you so you don’t get all tangled up with details you don’t know about or want to know about. Right away, you may be thinking,
That sounds like a Factory…
That’s right! You let the factory deal with the class your client wants to make a request from. A Proxy or Prototype would be another choice for separating request from the class you’re making the request to.
Dependence on specific Operations
Another cause for re-design is a dependence on specific operations which may change. The reason for changing an operation or a set of operations can be anything from a bright idea you get for a better way to set up the operations to changes in the objects the used by the operations. It could also be due to a new operation you want to add to the system. Suppose your system has an operation that handles the request for a phone number. The number could be anything from a land-line phone to a Skype number or something in the future none of us have even though of. You don’t want change in or the addition of a single operation to cause re-design for the whole site. So avoid hard-coded requests and let the decisions be made at compile time or run time. Two examples that avoid tightly bound requests are the Chain of Responsibility and Command patterns.
Dependence on Hardware and Software platforms
If you’re programming on a fairly low level, hardware differences can be more pronounced because you can program directly to the processor. PHP is a server-side language sent to a browser and it doesn’t matter is the user’s system is Linux, Windows and Mac operating systems. So platform is not something we tend to worry about or even have to concern ourselves with—albeit a few exceptions.
Having written design patterns for PHP5+, ActionScript 3.0 and a little Java, I am able to preserve most structures in a cross-language environment. However, I do need to attend to language differences, such as the fact that PHP5+ is weakly typed and ActionScript 3.0 is strongly typed. Gamma and associates suggest the Abstract Factory and Bridge designs to help overcome any software dependencies that may arise. The high level abstraction in both patterns helps to ameliorate any differences in hardware and software. However, most design patterns encourage communication between interfaces rather than concrete implementations, and so I would suggest that most (if not all) design patterns have a healthy regard for cross-platform and cross-API cases.
Dependence on Object Representations or Implementations

Figure 1: Works like Bait & Switch—as long as the interface is the same, it can make the request
Dependence on Algorithms
Most programs have a set of algorithms and some may have to be changed. Where your system is set up so that changing an algorithm will ripple through your program causing errors, you need to isolate the algorithms. For example, an algorithm can change so that a set of methods require additional parameters to deal with the revised algorithm. A simple change from a circle to an ellipse algorithm can cause far-reaching problems.
Several design patterns are developed to deal with changing algorithms. The Gang of four suggest the following:
- Builder
- Iterator
- Strategy
- Template Method
- Visitor
Because so many different patterns are designed to deal with changing algorithms, they are likely to represent a common reason for re-design. So, you may be well-served to first ask whether any algorithms in your system are likely to change.
Tight Coupling

Figure 2: Tightly Coupled classes are difficult to re-use
- Abstract Factory
- Bridge
- Chain of Responsibility
- Command
- Facade
- Mediator
- Observer (Chapter 14 in Learning PHP Design Patterns)
At this point, you can see where both algorithm change and tight coupling account for a huge amount of redesign. As you look at the other design patterns, see where your program has one or more reasons for redesign. Where the most intersections occur, ask yourself,
Which of these redesign causes are in my program?
Then find the “intersection” where the same design pattern is used to solve those problems.
Extending Functionality by Subclassing

Figure 3: Too many levels of subclassing leads to too many dependencies and can fall like a house of cards—just ask Alice.
Inability to alter classes conveniently
The final cause for re-design GoF note occurs where you want to modify a class that you may not have full access to. Such cases could exist where you may not have access to the class’ source code (such as ones in libraries.) They suggest using patterns such as the Adapter, Decorator or Visitor to wrap in some code without altering the original system.
Copyright © 2013 William Sanders. All Rights Reserved.
0 Responses to “Choosing a PHP Design Pattern: Part II—Avoiding Redesign”