-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory_method.cpp
110 lines (98 loc) · 1.63 KB
/
factory_method.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <memory>
#include <boost/shared_ptr.hpp>
using namespace std;
class IBike
{
public:
virtual void ride( ) = 0;
virtual ~IBike( )
{
}
};
class SmartBike : public IBike
{
public:
SmartBike( )
{
cout<<"smart bike created"<<endl;
}
void ride( )
{
cout<<"ride smart bike"<<endl;
}
~SmartBike( )
{
cout<<"--------destroy smart bike"<<endl;
}
};
class NormalBike : public IBike
{
public:
NormalBike( )
{
cout<<"normal bike created"<<endl;
}
void ride( )
{
cout<<"ride normal bike"<<endl;
}
~NormalBike( )
{
cout<<"--------destroy normal bike"<<endl;
}
};
class IFactory
{
public:
virtual boost::shared_ptr<IBike> create( ) = 0 ;
virtual ~IFactory( )
{
}
};
class NormalBikeFactory : public IFactory
{
public:
NormalBikeFactory( )
{
cout<<"normal bike factory created----"<<endl;
}
boost::shared_ptr<IBike> create( )
{
boost::shared_ptr<IBike> bike( new NormalBike( ) );
return bike;
}
~NormalBikeFactory( )
{
cout<<"-------normal bike factory destroy"<<endl;
}
};
class SmartBikeFactory : public IFactory
{
public:
SmartBikeFactory( )
{
cout<<"smart bike factory created----"<<endl;
}
boost::shared_ptr<IBike> create( )
{
boost::shared_ptr<IBike> bike( new SmartBike( ) );
return bike;
}
~SmartBikeFactory( )
{
cout<<"-------smart bike factory destroy"<<endl;
}
};
int main( void )
{
auto_ptr<IFactory> factory;
factory.reset( new NormalBikeFactory( ) );
boost::shared_ptr<IBike> bike;
bike = factory->create( );
bike->ride( );
factory.reset( new SmartBikeFactory( ) );
bike = factory->create( );
bike->ride( );
return 0;
}