Designing Software  «Prev  Next»
Lesson 2 Choosing a design pattern
Objective Find the design pattern that fits your need.

Choosing a Design Pattern during Software Development

Design patterns provide a generalized reusable solution to commonly occurring problems in software design. However, determining the correct design pattern to apply to a particular problem can be challenging. The selection process requires a deep understanding of the problem domain, a comprehensive knowledge of available patterns, and an ability to match a pattern's strengths to the problem's requirements. Below is a step-by-step process to guide this selection:
  1. Analyze the Requirement: Understanding the requirement is the first and most important step. You should thoroughly analyze the problem at hand and understand what is expected from the solution. Try to pinpoint the core concerns, whether they relate to object creation, class structure, or object behavior. This phase may involve dialogues with stakeholders, detailed study of requirement documents, and brainstorming sessions among the development team.
  2. Identify the Problem Type: After grasping the requirement, categorize the core problem you are facing. Is it related to object creation, structural organization, or the behavior of objects in your system? For example, if objects need to be created on the fly with a particular behavior or state, then the problem can be classified under "Creational" category.
  3. Explore Design Patterns: Once the problem category is clear, explore various design patterns that fall under that category. Each design pattern is meant to address a specific type of problem. For instance, if you're facing a creational problem, look into patterns such as Singleton, Factory Method, Abstract Factory, Builder, Prototype, etc. For structural issues, you might consider patterns like Adapter, Bridge, Composite, Decorator, etc. Behavioral concerns could be addressed by patterns like Chain of Responsibility, Command, Iterator, Strategy, etc.
  4. Evaluate the Patterns: After identifying potential design patterns, critically evaluate each of them. Compare the strengths and weaknesses of the patterns in relation to your specific problem. Consider factors such as the complexity of the pattern, its implications for future changes, its overhead, and how well it integrates with the existing system. In some cases, you might want to use more than one pattern or even a combination of patterns to solve a complex problem.
  5. Consider Practical Constraints: Be aware of any practical constraints like project deadlines, team expertise, performance requirements, and so on. Some patterns might require more time to implement correctly, or they might be more complex and require a higher level of programming expertise. Some patterns may also introduce a degree of performance overhead that might not be acceptable in performance-critical applications.
  6. Prototype and Review: Once you have made a choice, consider creating a small prototype using the chosen design pattern. This can provide a reality check on whether the pattern is a good fit for the problem. Have the prototype reviewed by peers or subject matter experts to gain additional insight and identify potential issues before full-scale implementation.

Remember, the goal of using a design pattern is to simplify and improve the design process, making your software easier to understand, maintain, and extend. If a design pattern introduces more complexity without significant benefits, it may not be the right choice. Always be prepared to reassess your choice of design pattern as your understanding of the requirements evolves and as the system itself changes over time.
Choosing Design Pattern
Patterns are not the panacea of program design. They do not replace traditional object-oriented analysis techniques like CRC cards or use-case modeling. To use an architectural analogy, analysis lets you determine that your house needs 200 amps of electricity. Patterns let you determine how the wiring will be installed.
Patterns do help you think about the problems you may encounter while designing a system. Therefore, a generic, pattern-based solution isoften more robust than a solution designed by one individual to solve a specific problem.
Given the number of design patterns in common use (as well as many more being invented and discovered almost daily) it can sometimes be hard to choose the pattern that suits your needs.
The first thing you should decide is whether the problem is fundamentally creational, structural, or behavioral. Some problems, of course, have aspects of two or even three, and may require you to mix and match patterns.

Integration of Design Patterns

The most notable aspect of this site is the manner in which the coverage of design patterns is interwoven with the remainder of the material.
For example,
  1. Swing containers and components motivate the COMPOSITE pattern.
  2. Swing scroll bars motivate the DECORATOR pattern, and Swing borders are examined as a missed opportunity for that pattern.
  3. Java streams give a second example of the DECORATOR pattern. Seeing the pattern used in two superficially different ways greatly clarifies the pattern concept.
Without memorable examples, design patterns are just words. In order to visualize design patterns, this website uses examples from graphical user interface programming. Students will remember how a component is decorated by scroll bars, and how layout managers carry out different strategies. (A small and carefully selected subset of Swing is used for this purpose.)

A Foundation for Further Study

This website discusses the design and implementation of computer programs from the object-oriented point of view. Although much of what we say remains valid for large projects, there are added complexities with large projects that we will not address here. Programming tasks originate from the desire to solve a particular problem. The task may be simple, such as writing a program that generates and formats a report, or complicated, such as writing a word processor. The end product is a working program. To this end, it is a common practice to break up the software development process into three phases:
  1. Analysis
  2. Design
  3. Implementation