0% found this document useful (0 votes)
10 views4 pages

Simplified Version of The Code For All Three Patterns

Uploaded by

Maaz Bhatti
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)
10 views4 pages

Simplified Version of The Code For All Three Patterns

Uploaded by

Maaz Bhatti
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/ 4

simplified version of the code for all three patterns, using a coffee shop example for better

understanding:

1. Factory Method Pattern

Yeh pattern object banane ka kaam subclasses ko deta hai, taake woh decide karein ke kaunsa
object create karna hai.

• Key Idea: Aap khud object nahi banate, balki ek factory method use karte hain jo decide
karta hai kaunsa object banana hai.
• Use Case: Jab aap chahte hain ke object creation ka kaam alag classes ko de diya jaye.

CODE
#include <iostream>
using namespace std;

// Product
class Coffee {
public:
virtual void prepare() const = 0;
virtual ~Coffee() = default;
};

// Concrete Products
class Espresso : public Coffee {
public:
void prepare() const override {
cout << "Espresso ready!" << endl;
}
};

class Cappuccino : public Coffee {


public:
void prepare() const override {
cout << "Cappuccino ready!" << endl;
}
};

// Factory
class CoffeeFactory {
public:
Coffee* createCoffee(const string& type) {
if (type == "Espresso") return new Espresso();
else if (type == "Cappuccino") return new Cappuccino();
return nullptr;
}
};

// Usage
int main() {
CoffeeFactory factory;
Coffee* coffee = factory.createCoffee("Espresso");
coffee->prepare();
delete coffee;

coffee = factory.createCoffee("Cappuccino");
coffee->prepare();
delete coffee;

return 0;
}

2. Abstract Factory Pattern

Yeh pattern aise related objects banata hai jo ek saath kaam karte hain, bina yeh bataye ke unki
exact class kya hai.

• Key Idea: Aap ek group ya family ke related objects ek hi factory ke zariye banate hain.
• Use Case: Jab aapko multiple related cheezen (objects) ek saath create karni hoon.

CODE
#include <iostream>
using namespace std;

// Abstract Products
class Coffee {
public:
virtual void prepare() const = 0;
virtual ~Coffee() = default;
};

class Snack {
public:
virtual void prepare() const = 0;
virtual ~Snack() = default;
};

// Concrete Products
class Espresso : public Coffee {
public:
void prepare() const override {
cout << "Espresso ready!" << endl;
}
};

class Cookie : public Snack {


public:
void prepare() const override {
cout << "Cookie ready!" << endl;
}
};
// Abstract Factory
class CoffeeShopFactory {
public:
virtual Coffee* createCoffee() = 0;
virtual Snack* createSnack() = 0;
virtual ~CoffeeShopFactory() = default;
};

// Concrete Factory
class SimpleCoffeeShopFactory : public CoffeeShopFactory {
public:
Coffee* createCoffee() override {
return new Espresso();
}
Snack* createSnack() override {
return new Cookie();
}
};

// Usage
int main() {
CoffeeShopFactory* factory = new SimpleCoffeeShopFactory();

Coffee* coffee = factory->createCoffee();


coffee->prepare();
delete coffee;

Snack* snack = factory->createSnack();


snack->prepare();
delete snack;

delete factory;
return 0;
}

3. Prototype Pattern

Yeh pattern ek existing object ko copy karke naya object banata hai.

• Key Idea: Naye object banane ke bajaye, ek existing object ko clone (copy) kar lete hain.
• Use Case: Jab naye object banane mein zyada time ya resources lagte hain, aur ek ready-made
object ko use karke duplicate banaya jaa sakta hai.

CODE

#include <iostream>
using namespace std;

// Prototype
class Coffee {
public:
virtual Coffee* clone() const = 0;
virtual void prepare() const = 0;
virtual ~Coffee() = default;
};

// Concrete Prototypes
class Espresso : public Coffee {
public:
Coffee* clone() const override {
return new Espresso(*this);
}
void prepare() const override {
cout << "Espresso ready!" << endl;
}
};

class Cappuccino : public Coffee {


public:
Coffee* clone() const override {
return new Cappuccino(*this);
}
void prepare() const override {
cout << "Cappuccino ready!" << endl;
}
};

// Usage
int main() {
Coffee* espressoPrototype = new Espresso();
Coffee* cappuccinoPrototype = new Cappuccino();

Coffee* coffee1 = espressoPrototype->clone();


coffee1->prepare();
delete coffee1;

Coffee* coffee2 = cappuccinoPrototype->clone();


coffee2->prepare();
delete coffee2;

delete espressoPrototype;
delete cappuccinoPrototype;

return 0;
}

Summary

• Factory Method: "Runtime pe decide karo ke kya banana hai."


• Abstract Factory: "Ek saath related objects ka group banao."
• Prototype: "Existing object ko copy kar lo."

You might also like