Professional Documents
Culture Documents
Report
Report
Report
Douida)
Introduction
In software development, design patterns provide proven solutions to common problems. Two
such patterns are the Observer pattern, which is behavioral, and the Factory pattern, which is
creational. This report compares these two design patterns, highlighting their definitions,
workings, appropriate use cases, and their pros and cons.
Comparison Table
Observer Pattern
→What is a behavioral design pattern?
Behavioral design patterns are a category of design patterns that focus on communication
between objects. They are concerned with algorithms and the assignment of responsibilities
between objects. These patterns help to manage complex control flows, encapsulate
algorithms, and distribute behaviors across classes, making the system more flexible and
easier to understand.
→How It Works
1. Subject: The subject maintains a list of observers and provides methods to attach or detach
observers.
2. Observers: Observers register themselves with the subject to receive updates. When the
subject's state changes, it notifies all registered observers by calling their update methods.
The Observer Design Pattern class diagram involves four main components: Subject,
Observer, ConcreteSubject, and ConcreteObserver. Here's an explanation of each component
and how they interact:
1. Subject: The subject maintains a list of observers and provides methods to attach and
detach observers.
-Responsibilities:
▪ Attach an observer.
▪ Detach an observer.
▪ Notify all observers of a state change.
2. Observer: The observer defines an updating interface for objects that should be notified of
changes in a subject.
- Responsibilities:
▪ Update itself based on changes in the subject.
3. ConcreteSubject: The concrete subject stores the state of interest to ConcreteObserver
objects and sends a notification to its observers when the state changes.
-Responsibilities:
▪ Implement methods to get and set state.
▪ Notify observers when the state changes.
4. ConcreteObserver: The concrete observer maintains a reference to a ConcreteSubject object
and implements the Observer updating interface to keep its state consistent with the subject's
state.
-Responsibilities:
▪ Update its state to reflect the state of the subject.
Implementation:
1. Observer Interface
- Defines the `update` method that observers must implement.
- Allows different observers to react to updates from the subject.
2. Subject Interface
- Defines methods to attach, detach, and notify observers.
- Manages the list of observers.
Workflow
Key Points
This implementation ensures that any change in the weather is automatically communicated to
all registered observers, keeping the displays in sync with the current weather conditions.
→When to Use It
→ Pros
→Cons
Factory Pattern
Creational patterns deal with object creation mechanisms, trying to create objects in a manner
suitable to the situation. The basic form of object creation could result in design problems or
added complexity to the design. Creational design patterns solve this problem by controlling
the object creation process.
The Factory pattern 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.
→ How It Works
1. Factory Method: Defines an interface for creating an object but allows subclasses to alter
the type of objects that will be created.
2. Product: The product declares the interface, which is common to all objects that can be
produced by the creator and its subclasses.
3. Concrete Product: Concrete products are different implementations of the product interface.
4. Creator: The creator class declares the factory method that returns new product objects. It's
often an abstract class with a concrete method that calls the factory method.
In the image above:
VehicleFactory: This is the interface or abstract class for creators. It declares the factory
method createVehicle() which returns an object of type Vehicle.
Concrete Creators (TwoWheelerFactory, FourWheelerFactory)
TwoWheelerFactory: A concrete implementation of the VehicleFactory interface. It
implements the createVehicle() method to return a TwoWheeler object.
FourWheelerFactory: Another concrete implementation of the VehicleFactory interface. It
implements the createVehicle() method to return a FourWheeler object.
How it Works:
The VehicleFactory declares the factory method createVehicle(), which is intended to return
an object of type Vehicle.
TwoWheelerFactory and FourWheelerFactory are concrete classes that implement the
VehicleFactory interface. Each of these concrete classes overrides the createVehicle() method
to return instances of TwoWheeler and FourWheeler, respectively.
Vehicle is the common interface for all products. Both FourWheeler and TwoWheeler classes
implement this interface and provide their own implementations of the printVehicle() method.
Implementation:
• Library Classes:
• Factory Interface:
• Concrete Factories:
• Client Class:
• Driver Program:
• GFG (Main Class): Demonstrates how the Factory Method pattern is used.
o It creates instances of TwoWheelerFactory and FourWheelerFactory.
o Uses these factories to create instances of TwoWheeler and FourWheeler
through the Client class.
o Calls printVehicle() on these instances to demonstrate their specific
behaviors.
→ When to Use It
→Pros
- Promotes code reusability by providing a single place to instantiate objects.
- Supports the Open/Closed Principle by allowing new products to be created without
modifying existing code.
- Reduces code duplication by centralizing object creation.
→Cons
Conclusion:
The Observer and Factory patterns serve different purposes in software design. The Observer
pattern is best suited for scenarios where multiple objects need to be kept in sync with a
subject, promoting loose coupling and dynamic observer management. On the other hand, the
Factory pattern excels in scenarios where object creation needs to be centralized and
customized by subclasses, promoting reusability and adherence to the Open/Closed Principle.
Understanding the strengths and weaknesses of each pattern allows developers to apply them
effectively, resulting in cleaner, more maintainable code.