-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractFactory.cpp
61 lines (52 loc) · 1.19 KB
/
abstractFactory.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<memory>
#include<iostream>
class Document {
public:
virtual void PrintInfo() const = 0;
};
class DocumentFactory {
public:
virtual std::unique_ptr<Document> Create() final
{
return this->MakeFactory();
}
virtual std::unique_ptr<Document> MakeFactory() = 0;
};
class PDFDoc final : public Document {
public:
void PrintInfo() const override
{
std::cout << "PDF Document." << std::endl;
}
};
class WordDoc final : public Document {
public:
void PrintInfo() const override
{
std::cout << "Word Document." << std::endl;
}
};
class PDFDocFactory final : public DocumentFactory {
public:
virtual std::unique_ptr<Document> MakeFactory()
{
return std::make_unique<PDFDoc>();
}
};
class WordDocFactory final : public DocumentFactory {
public:
virtual std::unique_ptr<Document> MakeFactory()
{
return std::make_unique<WordDoc>();
}
};
int main()
{
std::unique_ptr<DocumentFactory> creator = std::make_unique<PDFDocFactory>();
auto pdf = creator->Create();
pdf->PrintInfo();
creator.reset(new WordDocFactory);
auto word = creator->Create();
word->PrintInfo();
return 0;
}