-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_factory.cpp
85 lines (75 loc) · 1.2 KB
/
simple_factory.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
#include <iostream>
#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 BikeFactory
{
public:
enum BikeCate
{
NORMAL_BIKE,
SMART_BIKE
};
static boost::shared_ptr<IBike> create( const BikeCate type );
};
boost::shared_ptr<IBike> BikeFactory::create( const BikeCate type )
{
boost::shared_ptr<IBike> bike;
if( type == NORMAL_BIKE )
{
bike.reset( new NormalBike( ) );
}
else if( type == SMART_BIKE )
{
bike.reset( new SmartBike( ) );
}
return bike;
}
int main( void )
{
boost::shared_ptr<IBike> bike;
bike = BikeFactory::create( BikeFactory::NORMAL_BIKE );
bike->ride( );
bike = BikeFactory::create( BikeFactory::SMART_BIKE );
bike->ride( );
return 0;
}