Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Ayush Katwal

OOAD - BIM 7th SEM

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?

Solution Create a Pure Fabrication object called a Factory that handles


the creation.

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.

Problem How to resolve incompatible interfaces, or how to provide a


stable interface to similar components with different interfaces?

Solution Convert the original interface of a component into another


interface, through an intermediate adapter object.

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.

Problem Define a one-to-many dependency among objects so that when


one object changes state, all of its dependents are notified and
updated automatically. What to do?

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;

public class Subject {


private List<Observer> observers = new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
Ayush Katwal
OOAD - BIM 7th SEM

public void attach(Observer observer) {


observers.add(observer);
}
public void notifyAllObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
//OctalObserver Class
public class OctalObserver extends Observer{
public OctalObserver(Subject subject) {
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println("Octal String: " + Integer.toOctalString(subject.getState())); }
}
//HexaObserver Class
public class HexaObserver extends Observer{
public HexaObserver(Subject subject) {
this.subject = subject;
this.subject.attach(this);
}

@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

public BinaryObserver(Subject subject) {


this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println("Binary String: " +
Integer.toBinaryString(subject.getState())); }
}
//ObserverPatternDemo Class
public class ObserverPatternDemo {
public static void main(String[] args) {
Subject subject = new Subject();
new HexaObserver(subject);
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 12");
subject.setState(12);
System.out.println("Second state change: 10");
subject.setState(10);
}
}
Output: -
Ayush Katwal
OOAD - BIM 7th SEM

GRASP(General Responsibility Assignment Software Principles / Patterns):


It consists of guidelines for assigning responsibility to classes and objects in object-oriented
design. It helps us to define how classes work with one another. GRASP helps us in deciding
which responsibility should be assigned to which object/class i.e., it helps to guide object
oriented design by clearly outlining who does what and which object or class is responsible
for what actions.

Different GRASP patterns with Figure:


1. Creator
2. Information Expert
3. Controller
4. Low Coupling
5. High Cohesion

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 records instance of A objects.

• B closely use 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.

Problem: - What is a general principle of assigning responsibilities to object A?


Solution: - Assign those responsibilities to A for which A has the information
needed to fulfill it.
Ayush Katwal
OOAD - BIM 7th SEM

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:

▪ Represents the overall system, a root object, a device, or a major

subsystem (Facade Controller).

▪ Represents a use case scenario within which the system event

occurs, often named <UsedCaseName>Handler,<UseCaseName>


4. Low Coupling: -
Low coupling in software design refers to the practice of minimizing interdependencies
between different parts (components or modules) of a software system. When components
have low coupling, they are relatively independent and can be modified or replaced
without affecting other parts of the system. This design principle leads to more modular,
maintainable, and flexible code, making it easier to develop and maintain software
systems over time.
Ayush Katwal
OOAD - BIM 7th SEM

Coupling: A measure of how strongly one element is connected to, has knowledge of or
relies on other elements.

Problem: How to support low dependency, low change impact, increased


reuse? Solution: Assign a responsibility so coupling is low.

Example: POS System


Consider Payment, Register, Sale
Need to create a Payment and associate it with a Sale, who is responsible? Creator =>
Since, Register “records” a Payment, it should have this responsibility.
Consider coupling in two approaches:
• In both cases a Sale needs to know about a Payment

• However, a Register need to know about a payment in first but not in second •

Second approach has lower coupling


Ayush Katwal
OOAD - BIM 7th SEM

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.

Problem: How to keep complexity manageable?


Solution: Assign the responsibility so that cohesion remains high.

Cohesion: A measure of how strongly related and focused the responsibilities of an


element (class, subsystem, etc.) are.

Typically, high cohesion => few methods with highly related


functionality Benefits of high cohesion:

• Easy to maintain
• Easy to understand

• Easy to reuse

You might also like