🟢 What is a Design Pattern?
A design pattern is a common way to write code to fix a common problem.
Think of it as a ready-made plan.
1️⃣ CREATIONAL PATTERNS
(How to create objects)
✅ Singleton
Goal: Only one object of a class exists.
Example: only one settings object for the whole app.
Use: var s = AppSettings.Instance;
✅ Factory
Goal: Decide which object to create inside a method.
Use: Animal a = AnimalFactory.Create("dog"); a.Speak();
✅ Builder
Goal: Build a complex object step by step.
Use:
2️⃣ STRUCTURAL PATTERNS
(How to join or wrap objects)
✅ Adapter
Goal: Make two classes work together even if they don’t match.
✅ Facade
Goal: Give a simple interface to complex code.
Use: new CarFacade().Drive();
✅ Decorator
Goal: Add extra features to an object without changing its class.
3️⃣ BEHAVIORAL PATTERNS
(How objects talk to each other)
✅ Observer
Goal: When one object changes, others know.
Use:
✅ Strategy
Goal: Switch algorithm easily.
Use:
✅ Command
Goal: Save an action as an object so you can run it later.
Quick Table
Type | Pattern | Simple Purpose |
---|---|---|
Creational | Singleton | Only one object |
Factory | Create objects smartly | |
Builder | Build big object step by step | |
Structural | Adapter | Connect mismatched classes |
Facade | Simple front for complex system | |
Decorator | Add features without editing code | |
Behavioral | Observer | Notify many when one changes |
Strategy | Swap algorithms easily | |
Command | Store and run commands later |
💡 Tip:
-
Use patterns only when you need them.
-
They make code clean, flexible, and easy to change later.
No comments:
Post a Comment