0% found this document useful (0 votes)
12 views

Design Making Patterns

The document discusses three creational design patterns: Singleton, Builder, and Factory. The Singleton pattern ensures a class has only one instance with global access, while the Builder pattern constructs complex objects step by step, and the Factory pattern provides an interface for creating objects without specifying their exact class. Each pattern has its pros and cons, affecting code maintainability, complexity, and testing.

Uploaded by

johnwick771981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Design Making Patterns

The document discusses three creational design patterns: Singleton, Builder, and Factory. The Singleton pattern ensures a class has only one instance with global access, while the Builder pattern constructs complex objects step by step, and the Factory pattern provides an interface for creating objects without specifying their exact class. Each pattern has its pros and cons, affecting code maintainability, complexity, and testing.

Uploaded by

johnwick771981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Singleton Design Pattern

It is a creational design pattern that ensures a class has only one instance and provides a global
access point to that instance.

Key Characteristics:

1.​ Single Instance: Only one object of the class can exist.​

2.​ Global Access: That instance is accessible globally through a static method.​

3.​ Controlled Instantiation: The class controls how and when the instance is created.​

Typical Use Case:

You’d use a Singleton when:

●​ You need exactly one instance of a class to coordinate actions across the system.​

●​ Example: logging, configuration management, thread pools, or a connection to a


database.​

Pros:

●​ Controlled access to the single instance.​

●​ Reduced memory footprint if only one object is needed.​

●​ Useful in resource management (e.g., logging, configs).​

Cons:

●​ Can introduce global state into an application.​

●​ Makes unit testing harder due to the implicit dependency.​

●​ Violates the Single Responsibility Principle (SRP) if overused.​


Builder Design Pattern
It is a creational design pattern used to construct complex objects step by step, allowing for
more control over the construction process. It's especially useful when an object has many
optional parameters or complex nested structures.

Used to separate the construction of a complex object from its representation, so the same
construction process can create different representations.

Key Components:

1.​ Builder: An abstract interface or base class that defines the steps to build the object.​

2.​ ConcreteBuilder: Implements the steps defined in the Builder to construct and
assemble the parts.​

3.​ Director (optional): Orchestrates the building process using the builder.​

4.​ Product: The complex object that’s being built.​

Benefits:

●​ More readable and maintainable code when dealing with many optional parameters.​

●​ Avoids the need for complex constructors.​

●​ Better control over object creation.​

Drawbacks:

●​ Can be verbose to implement.​

●​ Slightly more complex than traditional constructors if not used carefully.


Factory Design Pattern
It is a creational design pattern that provides an interface for creating objects in a superclass but
allows subclasses to alter the type of objects that will be created.

It is used To encapsulate object creation logic so that the client code doesn’t need to know the
exact class of the object it is creating.

Key Idea:

Instead of instantiating objects directly using new, you delegate the creation to a factory method.

When to Use:

●​ When the exact type of the object isn’t known until runtime.​

●​ When you want to manage or control the object creation process.​

●​ When you need to return objects from a common base class or interface.​

Advantages:

●​ Decouples object creation from usage.​

●​ Simplifies code by handling instantiation logic in one place.​

●​ Makes code easier to extend (e.g., adding new Shape types).​

Disadvantages:

●​ May introduce extra classes or complexity.​

●​ Doesn’t eliminate the need for new, just moves it to a centralized place.​

You might also like