Design Patterns

 

๐ŸŽฏ 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:

  • Configuration manager

  • Database connection pool

  • Logger utility

๐Ÿ›  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

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism