Professional Documents
Culture Documents
OOAD Lab3
OOAD Lab3
1. Creational Design Pattern: It is a design pattern that deals with an object creation
mechanism, trying to create object in a manner suitable to the situation.
1.1 Singleton Pattern Demo: The singleton pattern is a design pattern used to implement the
mathematical concept of a singleton, by restricting the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system.
Name: Factory
Problem: How can be guaranteed that on one and only one instances of a class can be
created? Solution: Create a class with a class operation get Instance().
Ayush Katwal
OOAD - BIM 7th SEM
1.2 Factory Pattern: - It provides a way to use an instance as object factory. Then the factory
returns a instance of one of several possible classes depending on the data provided to it. In
Factory pattern, we create object without exposing the creation logic to the client and refer to
newly created object using a common interface.
Problem Who should be responsible for creating objects when there
are special considerations such as complex creation logic, a
desire to separate creation responsibilities for better
cohesion?
Ayush Katwal
OOAD - BIM 7th SEM
Ayush Katwal
OOAD - BIM 7th SEM
Output:
2. Structural Design Pattern: - It is a design pattern that defines how an object
and classes can be combined to form a structure.
2.1 Adapter Pattern: - This pattern works as a bridge between two incompatible interfaces. This
pattern involves a single class which is responsible to join functionalities of independent or
incompatible interfaces.
Ayush Katwal
OOAD - BIM 7th SEM
Ayush Katwal
OOAD - BIM 7th SEM
Ayush Katwal
OOAD - BIM 7th SEM
Output:
Ayush Katwal
OOAD - BIM 7th SEM
3. Behavioural Design Pattern: - This design patterns that focus on communication between
objects and the way they operate together. They aim to improve the flexibility and reuse of
software systems by making the relationships between objects more dynamic.
3.1 Observer Pattern: - It is a software design pattern in which an object called the subject,
maintains a list of its dependents, called observer and notifies them automatically of any
state change, usually by calling one of their methods.
Solution MVC, but refined by separating abstract from concrete subjects and
observers.
//Observer Class
package Behavioral;
public abstract class Observer {
protected Subject subject;
public abstract void update();
}
//Subject Class
import java.util.ArrayList;
import java.util.List;
@Override
public void update() {
System.out.println("Hex String: " + Integer.toHexString(subject.getState()).toUpperCase()); }
}
//BinaryObserver Class
public class BinaryObserver extends Observer{
Ayush Katwal
OOAD - BIM 7th SEM
1. Creator: -
• Creator pattern is to find a creator that needs to be connected to the created object in
any event.
• Who creates an Object? Or who should create a new instance of some class? •
Decide who can be creator based on the object’s association and their interaction.
Problem: Who should be responsible for creating a new instance of some class?
Solution: Assign class B the responsibility to create an instance of class A if one
or more of the following is true:
• B aggregates A objects.
• B contains A objects.
• B has the initializing data that will be passed to A when it is created (thus
B is an Expert with respect to creating A).
Ayush Katwal
OOAD - BIM 7th SEM
2. Information Expert: -
It is a principle used to determine where to delegate responsibilities. These responsibilities
include methods, computed fields and son on.
3. Controller: -
• A controller is the first object beyond the UI layer that is responsible for receiving
or handling a system operation message.
• Deals with who should be responsible for handling event from external actors (UI). •
Deals with how to delegate the request from UI layer object to domain layer objects.
Problem: - What first object beyond the UI layer receives and coordinates
(Control) a system operation?
Solution: - Assign the responsibility to a class representing one of the following
choices:
Coupling: A measure of how strongly one element is connected to, has knowledge of or
relies on other elements.
• However, a Register need to know about a payment in first but not in second •
5. High Cohesion: -
High cohesion is another important software design principle that focuses on how closely
the elements (such as functions, classes, or modules) within a software module or
component are related to each other in terms of their responsibilities and functionality.
• Easy to maintain
• Easy to understand
• Easy to reuse