Design Pattern

🎯 What Are Design Patterns?

Design patterns are proven solutions to common software design problems. They represent best practices refined over time by experienced developers.

They help you:

  • Write reusable and maintainable code.

  • Improve communication between team members using a common vocabulary (e.g., "This uses the Singleton pattern").

  • Solve structural or behavioral challenges in object-oriented design.


🧩 Categories of Design Patterns

  1. Creational – deal with object creation (e.g., Singleton, Factory).

  2. Structural – deal with class/object composition (e.g., Adapter, Decorator).

  3. Behavioral – deal with object communication (e.g., Observer, Strategy).


🟨 1. Singleton Design Pattern (Creational)

✅ Purpose:

Ensure only one instance of a class exists in the application and provide a global point of access to it.

🔧 When to Use:

🛠 Implementation in Java:


class Singleton { private static Singleton instance; // private constructor prevents instantiation from other classes private Singleton() { System.out.println("Singleton instance created"); } // global access method public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); // Lazy initialization } return instance; } } public class SingletonTest { public static void main(String[] args) { Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); System.out.println(obj1 == obj2); // true — both refer to the same instance } }

✅ Key Points:

  • Only one object ever created.

  • Constructor is private.

  • Instance is returned via a static method.


🟩 2. Adapter Design Pattern (Structural)

✅ Purpose:

Convert the interface of a class into another interface that clients expect. It allows classes with incompatible interfaces to work together.

🔧 When to Use:

  • You want to use a class, but its interface is different from what your code expects.

  • You want to reuse legacy code.


🧱 Example Scenario:

Suppose your client code expects USBCharger, but you have a class OldCharger with a different interface.

🔁 Without Adapter:

class OldCharger {
public void chargePhone() { System.out.println("Charging phone with OldCharger"); } }

🧩 Adapter Code:


interface USBCharger { void charge(); } // Adapter class class ChargerAdapter implements USBCharger { OldCharger oldCharger = new OldCharger(); public void charge() { oldCharger.chargePhone(); // adapting method } }

🧪 Client Code:


public class AdapterDemo { public static void main(String[] args) { USBCharger usb = new ChargerAdapter(); usb.charge(); // Output: Charging phone with OldCharger } }

✅ Key Points:

  • Adapter implements the expected interface.

  • It translates calls to the actual/legacy interface.


✨ Summary

Pattern            CategoryIntent
Singleton            Creational                Ensure only one instance of a class
Adapter            Structural                Bridge between incompatible interfaces

Comments

Popular posts from this blog

PBCST304 OBJECT ORIENTED PROGRAMMING(2024 SCHEME)

CST446 DATA COMPRESSION TECHNIQUES(2019 scheme)

CST 362 PROGRAMMING IN PYTHON(2019 scheme S6 Program Elective I )