Simplified Version of The Code For All Three Patterns
Simplified Version of The Code For All Three Patterns
understanding:
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;
}
};
// 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;
}
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;
}
};
// 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();
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;
}
};
// Usage
int main() {
Coffee* espressoPrototype = new Espresso();
Coffee* cappuccinoPrototype = new Cappuccino();
delete espressoPrototype;
delete cappuccinoPrototype;
return 0;
}
Summary