Flashcard Deck · 22 cards · Public

Design Patterns - Gang of Four & SOLID Principles

Master essential software design patterns from the Gang of Four and the foundational SOLID principles to architect robust, maintainable, and scalable object-oriented systems. Elevate your coding skills and build future-proof applications.

Cards in this deck

(22 cards)

Preview terms and definitions before starting your study session.

#1
Term
What are Design Patterns in software engineering?
Definition
Design Patterns are reusable solutions to common problems encountered during software design. They are not finished designs that can be directly transformed into code, but rather templates or descriptions for how to solve a problem that can be used in many different situations.
#2
Term
What are the three main categories of Gang of Four (GoF) Design Patterns?
Definition
The three main categories of GoF Design Patterns are:
  • 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.
#3
Term
Explain the Singleton Pattern and provide a common use case.
Definition
The 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.
java13 lines
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!");
    }
}
#4
Term
Describe the Factory Method Pattern and its purpose.
Definition
The 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.
#5
Term
What is the Abstract Factory Pattern?
Definition
The 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).
#6
Term
Explain the Decorator Pattern and give an example.
Definition
The 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).
java23 lines
// 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; }
}
#7
Term
What is the purpose of the Adapter Pattern?
Definition
The 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.
#8
Term
Describe the Composite Pattern and its application.
Definition
The 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.
Showing 8 of 22 cards in this deck.
Study All Now

Related Flashcard Sets

Explore similar study materials created by the community

Browse community decks