Software Design Patterns Flashcards
(11 cards)
What is a design pattern?
A general reusable solution to a commonly occurring problem; a description or template for how to solve a problem that can be used in many different situations.
What are the benefits of design patterns?
- Reusability - design patterns eliminate the need to reinvent solutions to commonly recurring problems, which speeds up the development process
- Quality - design patterns are good, established solutions to problems that improve the internal quality of code
- Communication - design patterns provide common terminology and viewpoints on problems
- Perspective - design patterns help developers see the forest through the trees (i.e. avoid getting bogged down in details)
What are the 3 main types of design patterns?
- Creational - how objects are created. They separate the code that creates an object from the code that uses it.
- Behavioral - how objects are composed. They address problems such as:
○ how do objects communicate?
○ how are responsibilities assigned to different objects?
- Structural - how objects relate. They address problems such as:
○ how do we assemble the constituent parts of an object?
○ how do we define the relationships between classes?
○ how do we combine objects into larger structures?
Singleton pattern
Creational pattern that ensures that there is only one instance of a class, and that it can be accessed easily. The are three key aspects of this pattern’s implementation:
- The class’s constructor is private so that no other class can access it and create a new instance. The class will use the below features to create an instance of itself.
- There is a static, single instance of the class as field in the class. This too must be private so that no other classes can access it. Note, it’s static so that it can be accessed by the following static method.
- There is a public static accessor method to get the singleton instance so other classes can access it. For example, the following class could be leverage by another programmer by calling this method and writing to the logger as follows:
Logger myLogger = new Logger.getInstance( );
myLogger.log(“this is a test”);

Factory method pattern
Creational pattern that uses inheritance to separate the code that uses another class from the code that creates it. It works in the following manner:
- Class A has a dependency on Class B
- Class A contains code to use Class B
- Subclasses of A override a “factory method” used to create an instance of the desired subclass of B
For instance, in the following example, the Processor class originally had a dependency on a FileReader class. However, its processor method shouldn’t have to know the type of reader being used or any other of it’s dependencies, which after all is the whole point of abstraction. Plus, this tight dependency prevents us from being able to use the processor method on other sources such as a database, since a DatabaseReader would be required. Therefore we use this design pattern in which sub classes of A override a method called the factory method, hence the name, and a parent class A invokes that method so that it gets the subclass of B that it wants to use. In this case the factory method is createReader( ).

Bridge pattern
A structural pattern that maintains separate inheritance hierarchies of concepts (abstractions) and things that use them (implementors) and “bridges” them using aggregation so that clients only need to work with abstraction. This enables combinations of abstractions and implementors without ending up with M*N classes to represent all possible combinations.
For example, this is how a graphics package is able to implement an abstraction such as Shape in various renders such as color, grayscale, or 3D without having to create separate colorShape, grayscaleShape, 3DShape classes.
This is illustrated in the diagram below:

Strategy pattern
A behavioral pattern in which we create classes to specify certain “strategies” that can be used as part of a larger algorithm. This enables us to create one general algorithm, as opposed to multiple, that can be used in a variety of contexts because the class represents the “strategy” to apply for its particular use case.
For example, this pattern enables us to eliminate the duplicate code in the first example by refactoring it to what’s outlined in the second example:

Observer pattern
A behavioral pattern in which a subject has a set of observers that can be notified when an event occurs. For this reason, it is also known as Publish-Subscribe. It is one of the most commonly used design patterns, particularly in GUI systems such as Android and Java Swing.
It is illustrated in the diagram below:

The Collections.sort method taking a Comparator to determine how to compare elements is an example of which design pattern?
This is an example of the Strategy pattern because the overall algorithm (sorting) requires the user to specify a particular detail of how it is to be performed.
What do the Singleton and Factory Method patterns have in common?
They are both creational design patterns and as such, separate the code that creates an object from the code that uses it. Note that the Singleton creates itself, but is used elsewhere while with the Factory Method, a class may use the object but a subclass creates it.
What design pattern is used in the following code?

The bridge pattern because both Animal and Instrument are the parent classes of separate hierarchies, and the “instr” field in Animal is the bridge between the two.