1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Design Pattern
A proven advice for solving a recurring class of problems by providing a template-like solution rather than a finished design.
Purpose of Design Patterns
Increase reusability, maintainability, clarity, and communication among developers.
Notes about Design Patterns
They are language-independent and serve as conceptual templates, not finished code.
Architectural Design Pattern
A high-level solution that defines the structure of a system, dividing it into components/layers and specifying their communication rules.
Scope of Architectural Patterns
Consider the whole application, providing the first level of abstraction.
Common Architectural Patterns
Multi-Layer, MVC
Multi-Layer Pattern
Divides an application into independent layers that provide services to their neighboring layers.
3-Layer Architecture
Presentation layer, Business Rules layer, Data Management layer.
Presentation Layer
Handles UI/UX and interacts with the user.
Business Rules Processing Layer
Handles computations, logic, and application flow.
Data Management Layer
Handles storage, retrieval, and database/file operations.
Layer Awareness
Each layer knows only its neighbors, reducing tight coupling.
Example of Multi-Layer Pattern
Operating systems with GUI → services → kernel layering.
Benefits of Multi-Layer Pattern
Separation of concerns, fewer dependencies, readability, reusability, testability, maintainability.
MVC Pattern
Divides an application into Model, View, and Controller to separate logic from UI.
Model (MVC)
Manages data, business rules, and state changes
View (MVC)
Displays information to the user and receives no direct data-manipulation ability.
Controller (MVC)
Handles user input, interprets View events, updates the Model.
MVC Interaction Flow
User → View → Controller → Model → View updates.
Benefits of MVC
Structural clarity, maintainability, reusable UI components, prevents direct user manipulation of data.
Multiple Views in MVC
One model can update many views simultaneously.
Class Design Pattern
Lower-level design solutions that focus on interactions among a few classes.
Three Types of Class Design Patterns
Creational, Structural, Behavioral.
Singleton Pattern
Ensures exactly one instance of a class exists at runtime and provides global access to it.
Singleton Use Case: Database Connection
Expensive connections are created only once and shared.
How Singleton Works
Private constructor, static instance variable, and a public static getter method.
Why Constructor Is Private
Prevents creation of multiple instances.
Singleton Instance Lifetime
Instantiated once, usually when the class loads.
Composite Pattern
Treats individual objects and composite groups uniformly by having both implement the same interface/abstraction.
Composite Situation Example
Products and bundles both behave as “products,” allowing unified handling.
Why Composite Helps
Simplifies code, supports extension, and unifies behavior of single items and groups.
Composite Structure
Component interface + leaf classes + composite classes containing children.
Façade Pattern
Provides a simplified interface to a complex subsystem, exposing only necessary operations.
Façade Problem Scenario
A client depends on many classes and sees methods it shouldn’t or doesn’t need.
Façade Solution
A wrapper class delegates tasks to subsystem classes but exposes a clean interface.
Façade Benefit
Reduces coupling, hides complexity, and improves client-side code clarity.
Strategy Pattern
Allows selecting an algorithm’s behavior at runtime by encapsulating each variant in its own class.
Strategy Example: Payment Methods
Credit card, PayPal, crypto — each is a strategy implementing PaymentStrategy.
Strategy Example: Sorting Algorithms
Algorithms like merge sort and bubble sort can be swapped.
Strategy Example: AI Solvers
Puzzle-solving algorithms chosen based on context or difficulty.
Context Class (Strategy)
Uses a Strategy interface reference and executes whichever strategy is assigned.
Runtime Behavior Change
Client can switch strategies at any time to alter behavior.
Strategy and Polymorphism
Leverages interface-based polymorphism to abstract algorithm families.
Template Method Pattern
Defines the skeleton of an algorithm in a superclass method, leaving certain steps abstract for subclasses to implement.
Template Method Role
A concrete method calls one or more abstract methods to complete its algorithm.
Subclass Responsibility (Template)
Provide their own implementations for the abstract steps.
Purpose of Template Method
Allows algorithm structure to remain fixed while subclasses customize steps.