0% found this document useful (0 votes)
3 views8 pages

NormalCar Class Implementation

The document outlines the implementation of the Strategy and Observer design patterns in a programming context. It provides code examples for a Car class hierarchy using different Engine types, and an Observable interface for managing observers in a temperature monitoring system. The document emphasizes the flexibility and functionality of these design patterns in software development.

Uploaded by

saksham2700
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)
3 views8 pages

NormalCar Class Implementation

The document outlines the implementation of the Strategy and Observer design patterns in a programming context. It provides code examples for a Car class hierarchy using different Engine types, and an Observable interface for managing observers in a temperature monitoring system. The document emphasizes the flexibility and functionality of these design patterns in software development.

Uploaded by

saksham2700
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

LLD

Wednesday, 29 January 2025 7:17 PM

Strategy pattern, where we are inserting the strategy into th


class

abstract class Car {


Engine engine;

Car (Engine e) {
[Link] = e;
}

void drive() {
print("Driving");
[Link]();
}
}

class SportsCar extends Car { class NormalCar extends Car {


SportsCar () { NormalCar () {
super(RacingEngine()); super(NormalEngine());
} }
}
}

Observer Pattern
he

interface Engine {
void work ();
}

class RacingEngine implements Engine {


void work () {
print("Speed")
}
}

class NormalEngine implements Engine {


void work () {
print("Efficiency")
}
}
Observer Pattern

interface Observable<T> {
void add (Observer o);
void remove (Observer o);
void notify();
void setValue(T v);
T getValue();
}

class TemperatureObservable implements Observable<int> {


int value;
vector<Observer> observers;

void add (Observer o) {


this->observers.push_back(o);
}

void remove (Observer o) {


for (int i = 0; I < this->[Link](); i++) {
if (this->observers[i] == o) {
this->[Link](this->[Link]() + i);
break;
}
}
}

void notify () {
for (int i = 0; I < this->[Link](); i++) {
this->observers[i].update();
}
}

void setValue (int v) {


this->value = v;
this->notify();
}

int getValue () {
}
}

abstract class Observer {


int id;
Observable observable;

Observer(Observable o) {
this->observable = o;
}

void update();
}

Observer1 (is-a Observer) can implement update functionality

Similarly, Observer2 can implement its own update functionality


void setValue (int v) {
this->value = v;
this->notify();
}

int getValue () {
return this->value;
}

You might also like