Design Patterns - Gang of Four & SOLID Principles
Cards in this deck
(22 cards)Preview terms and definitions before starting your study session.
- Creational Patterns: Deal with object creation mechanisms, trying to create objects in a manner suitable for the situation.
- Structural Patterns: Deal with the composition of classes and objects, forming larger structures.
- Behavioral Patterns: Deal with algorithms and the assignment of responsibilities between objects.
Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.Use Case: A logging utility, a configuration manager, or a database connection pool where only one instance is needed to manage a shared resource.
public class Singleton {
private static Singleton instance;
private Singleton() {} // Private constructor
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void showMessage() {
System.out.println("Hello from Singleton!");
}
}Factory Method pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. It defers instantiation of an object to its subclasses.Purpose: To create objects without specifying the exact class of object that will be created. This promotes loose coupling.
Use Case: A document editor that can create different types of documents (e.g.,
WordDocument, PdfDocument) based on user input, where each document type has its own factory.Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It's a "factory of factories."Use Case: A GUI toolkit that needs to create different sets of UI components (e.g.,
Button, Checkbox) for different operating systems (e.g., WindowsFactory creates WindowsButton, MacOSFactory creates MacOSButton).Decorator pattern allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. It wraps an object to provide new responsibilities.Example: Adding logging, encryption, or compression functionality to a data stream (
FileInputStream decorated with BufferedInputStream and GZIPInputStream).// Basic Coffee interface
interface Coffee {
String getDescription();
double getCost();
}
// Concrete Coffee
class SimpleCoffee implements Coffee {
public String getDescription() { return "Simple Coffee"; }
public double getCost() { return 2.0; }
}
// Decorator base
abstract class CoffeeDecorator implements Coffee {
protected Coffee decoratedCoffee;
public CoffeeDecorator(Coffee coffee) { this.decoratedCoffee = coffee; }
public String getDescription() { return decoratedCoffee.getDescription(); }
public double getCost() { return decoratedCoffee.getCost(); }
}
// Concrete Decorator
class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) { super(coffee); }
public String getDescription() { return super.getDescription() + ", Milk"; }
public double getCost() { return super.getCost() + 0.5; }
}Adapter pattern allows objects with incompatible interfaces to collaborate. It acts as a wrapper between two objects, catching calls for one object and transforming them into a format recognizable by the second object.Use Case: Integrating a new third-party library with an existing system that expects a different interface. For example, an old
SquarePeg system needs to work with a new RoundHole system, so a SquarePegAdapter is created.Composite pattern composes objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly.Application: Representing file systems (files and directories), UI components (individual widgets and panels containing widgets), or organizational charts.
Example: A
Graphic interface with draw() method. Circle and Rectangle implement Graphic. CompositeGraphic also implements Graphic and holds a collection of Graphic objects, allowing drawing of a group of shapes.Related Flashcard Sets
Explore similar study materials created by the community
TypeScript - Advanced Type Systems, Generics & Design Patterns
Master advanced TypeScript features like generics, conditional types, and mapped types to build robust, scalable applications. This deck covers essential utility types, type narrowing techniques, discriminated unions, and the nuances between interfaces and type aliases, empowering you to write highly maintainable and type-safe code.
Harvard ECON1011 - Intermediate Microeconomics: Game Theory & Market Structures
Master key concepts in intermediate microeconomics with this comprehensive flashcard deck. Dive deep into game theory, market structures, strategic pricing, and general equilibrium models, perfect for Harvard ECON1011 students and anyone aiming to ace microeconomics.
MIT 18.03 - Differential Equations: Ordinary Differential Equations, Laplace & Fourier
Master MIT 18.03 Differential Equations with high-yield flashcards covering first and second-order ODEs, Laplace transforms, Fourier series, and matrix exponential state-space systems.
CPA Exam FAR - Financial Accounting & Reporting: US GAAP & Revenue Recognition
Master high-yield CPA FAR concepts covering ASC 606 revenue recognition, ASC 842 lease accounting, inventory valuation methods, and consolidated financial statements.