-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprak4.cpp
46 lines (45 loc) · 1.22 KB
/
prak4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <memory>
enum ProductId {
MINE,
YOURS
};
// Interface for objects created by the factory method.
class Product {
public:
virtual void print() = 0;
};
// Concrete implementations of the Product interface.
class ConcreteProductMINE : public Product {
public:
void print() override {
std::cout << "this=" << this << " print MINE\n";
}
};
class ConcreteProductYOURS : public Product {
public:
void print() override {
std::cout << "this=" << this << " print YOURS\n";
}
};
// Creator class with the factory method.
class Creator {
public:
virtual std::unique_ptr<Product> create(ProductId id) {
if (ProductId::MINE == id)
return std::make_unique<ConcreteProductMINE>();
if (ProductId::YOURS == id)
return std::make_unique<ConcreteProductYOURS>();
// Add more cases for other product types...
return nullptr;
}
};
int main() {
std::unique_ptr<Creator> creator = std::make_unique<Creator>();
std::unique_ptr<Product> product = creator->create(ProductId::MINE);
product->print();
product->print();
product = creator->create(ProductId::YOURS);
product->print();
product->print();
}