-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.hpp
49 lines (47 loc) · 1.03 KB
/
Factory.hpp
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
47
48
49
#ifndef __ABSTRACTFACTORY__
#define __ABSTRACTFACTORY__
#include <memory>
#include <string>
namespace peking {
enum class ProductID { Laptop, Cellphone, Camera };
class Product {
public:
Product() {}
Product(const std::string &name) : name_(name) {}
std::string Name() { return name_; }
private:
std::string name_;
};
class ConcreteProduct : public Product {
public:
ConcreteProduct(const std::string &name) : Product(name) {}
};
class Creator {
public:
virtual Product *Create(ProductID id);
protected:
};
Product *Creator::Create(ProductID id) {
switch (id) {
case ProductID::Laptop:
return new ConcreteProduct("Laptop");
break;
case ProductID::Cellphone:
return new ConcreteProduct("Cellphone");
break;
case ProductID::Camera:
return new ConcreteProduct("Camera");
break;
default:
return new Product("Meta");
break;
}
}
template <typename OneProduct> class StandardCreator : public Creator {
public:
Product *Create() {
return new OneProduct("one");
}
};
} // namespace peking
#endif