Design Patterns (Week 8-9) Flashcards
What is a design pattern?
A way of reusing abstract knowledge about a problem and its possible solutions
- Description of a problem
- Essence of a solution
Patterns describe a problem which occurs over and over again - and then offers a solution
Not a finished design
- they are a description/template
- Provide enough information about trade-offs and consequences to enable design decisions to be made
What types of patterns are there?
Architectural patterns
- Describe structural designs for software systems
Design patterns
- Template for addressing design issues in software design
Describe architectural patterns
Architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context.
- similar to software design patterns, but with a broader scope
Example:
Client-server pattern
- Describes how a server may be connected with multiple clients
Peer-to-Peer pattern
- Describes how nodes can behave as clients and/or servers and how they might connect
What are the three main types of design patterns in OO design?
Creational
- deal with classes and object instantiation
Structural
- Deal with class-object composition
Behavioural
- Deal with communications between objects
Talk about some creational design patterns
Builder
- Separate the construction of a complex object from its representation, so that the same build process can create different representations. E.g constructing meals at a take-away
Prototype
- Create new objects by copying a prototype. E.g cell division, a cell clones itself
Singleton - Ensure a class has only one instance. E.g A logger object or a window manager
What is a singleton?
Software design pattern that restricts the instantiation of a class to one. (ensure a class has only one instance and provide a global point of access to it)
What are the steps of creating a singleton?
- Define a private static attribute in the “single instance” class
- Define a public static accessor function in the class
- Do “Lazy initialization” (creating on first use) in the accessor function.
- Define all constructors to be protected or private
- Clients may only use the accessor function to manipulate the singleton
When is singleton unnecessary?
Short answer: most of the time. Long answer: when it’s simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
What is the advantage of using singletons over global variables?
The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
Describe factory pattern
Creates object without specifying the exact class of object that will be created
The factory design pattern defines an interface for creating an object
The factory method lets a class defer instantiation to subclasses
Returns an instance of one of several possible classes
- Depending on data we provide
- Classes returned have a common parent class
- Each class performs differently
Factory can return an instance of several possible classes that have a common parent class
What programming principle does the factory give us a solution to?
Polymorphism
What are factory pattern variants?
Static factory pattern
- Create a method which is responsible for creating objects of a particular type
- if this method is ‘static’ then it is a static factory pattern
Factory Method Pattern
- The method creating the objects becomes ‘abstract’
- Causes the class to be abstract and must be used as a base class
- The method needs to be implemented in a ‘concrete Client’
Abstract Factory Pattern
- Group together individual Factory methods
What are structural patterns?
Structural patterns modify the structure or design of the system
They are responsible for building simple and efficient class hierarchies and relations between different classes
What is a Facade pattern?
Facade is a structural design pattern that lets you provide a simplified interface to a complex system of classes, library or framework
The problem:
- Imagine some code that has to work with a large set of objects of some complex library or framework
- You have to manually initialize all these objects, keep track of the dependencies, correct order and so on.
- The business logic of your classes becomes tightly coupled to the implementation details of third party library. Such code is pretty hard to comprehend and maintain
What is the solution for working with large set of objects of some complex library or framework?
Facade pattern.
Facade pattern provides a simple interface to a complex subsystem containing dozens of classes.
- the facade may have limited functionality in comparison to working with subsystem directly
- However, it includes only those features that clients really care about