DP Flashcards
What is a design pattern and why do we use them?
Solution to a problem that repeatedly occurs in software development
A design pattern shows you how to structure your classes and how classes should talk to each other
What are the 3 categories of design patterns
Creational - different ways to create objects
Structural - relationships between objects
Behavioural - communication between objects
What are 5 creational design patterns?
- Singleton
- Factory Method
- Abstract Factory
- Builder
- Prototype
What are 6 structural design patterns?
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
What are 11 behavioural design patterns
- Chain of responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template method
- Visitor
What is singleton design pattern
- creational design pattern
- only 1 instance of the class
- Check satic Singleton instance == null, create instance
- might be used for database connection manager
What is the factory design pattern?
- Creational design pattern
- Provides an interface for creating objects in a superclass
Ie., Shape check the name of the shape ie., rectangle and then return a shape of that type
What is the benefits of design patterns
Improves the quality of our code
Makes it more maintainable and scalable
- Help developers write code that is easy to understand and modify and reduce the time and effort to develop a solution
- Reduce the risk of bugs
- Lead to a more consistent code base
What is a creational design pattern for?
Object creation
What is a structural design pattern for?
relationships between objects
What is a behavioural design pattern for?
Communication between objects
Which design pattern ensures that only one instance of a class can be created?
The Singleton design pattern
What is the problem with this implementation of the Singleton design pattern?
public class Database {
private static Database singletonDatabase; private Database(){}; public static Database getInstance(){ if(singletonDatabase == null){ return singletonDatabase = new Database(); } return singletonDatabase; } }
It is not thread safe so will not work in a multi threaded environment
Write a code example of the Singleton design pattern that would work in a multi-threaded environment?
public class Singleton {
private Singleton() {}
private static class SingletonHolder { public static final Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } }
What is the factory method pattern?
The factory method pattern is when a factory method is created in its own class to create objects
A parameter can be passed to this method and it will return the object of this type
When could the factory method design pattern be a good choice?
when you only know at runtime which object to create
or
object creation is expensive
or
the types being created will expand over time
When could you use the abstract factory design pattern?
When you want to encapsulate a group of individual factories that have a common theme without specifying their concrete classes
What is the builder design pattern?
The builder design pattern allows you to construct complex objects step by step