Skip to content

Commit 79e971b

Browse files
author
jason_yao
committed
update
1 parent 01f23e2 commit 79e971b

406 files changed

Lines changed: 11615 additions & 9574 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
#ifndef _HFDP_CPP_ADAPTER_DUCK_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_DUCK_HPP_
22
#define _HFDP_CPP_ADAPTER_DUCK_HPP_
33

44
#include "Ducks.hpp"
55

66
namespace HeadFirstDesignPatterns {
7-
namespace Adapter {
8-
namespace Ducks {
7+
namespace Adapter {
8+
namespace Ducks {
99

10-
class Duck {
10+
class Duck {
1111

12-
public: virtual ~Duck() {
13-
}
14-
public: virtual void fly() const = 0;
15-
public: virtual void quack() const = 0;
16-
};
12+
public:
13+
virtual ~Duck() {
14+
}
15+
public:
16+
virtual void fly() const = 0;
17+
public:
18+
virtual void quack() const = 0;
19+
};
1720

18-
} // namespace Ducks
19-
} // namespace Adapter
21+
} // namespace Ducks
22+
} // namespace Adapter
2023
} // namespace HeadFirstDesignPatterns
2124

2225
#endif
Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
1-
#ifndef _HFDP_CPP_ADAPTER_DUCK_ADAPTER_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_DUCK_ADAPTER_HPP_
22
#define _HFDP_CPP_ADAPTER_DUCK_ADAPTER_HPP_
33

44
#include "Ducks.hpp"
55
#include <process.h>
66

77
namespace HeadFirstDesignPatterns {
8-
namespace Adapter {
9-
namespace Ducks {
10-
11-
class DuckAdapter : public Turkey {
12-
13-
private: std::auto_ptr< const Duck > _duck;
14-
private: int _random;
15-
16-
private: DuckAdapter( const DuckAdapter& ); // Disable copy constructor
17-
private: void operator=( const DuckAdapter& ); // Disable assignment operator
18-
19-
public: explicit DuckAdapter( const Duck* duck ) :
20-
_duck ( duck ) { assert( duck );
21-
srand( _getpid() );
22-
_random = rand() % 5;
23-
if( _random == 0 )
24-
_random = 1;
25-
}
26-
public: void fly() const { assert( _duck );
27-
for( int i = 0; i < _random; i++ ) {
28-
_duck->fly();
29-
}
30-
}
31-
public: void gobble() const { assert( _duck );
32-
_duck->quack();
33-
}
34-
};
35-
36-
} // namespace Ducks
37-
} // namespace Adapter
8+
namespace Adapter {
9+
namespace Ducks {
10+
11+
class DuckAdapter : public Turkey {
12+
13+
private:
14+
std::auto_ptr< const Duck > _duck;
15+
private:
16+
int _random;
17+
18+
private:
19+
DuckAdapter(const DuckAdapter&); // Disable copy constructor
20+
private:
21+
void operator=(const DuckAdapter&); // Disable assignment operator
22+
23+
public:
24+
explicit DuckAdapter(const Duck* duck) :
25+
_duck(duck) {
26+
assert(duck);
27+
srand(_getpid());
28+
_random = rand() % 5;
29+
30+
if (_random == 0) {
31+
_random = 1;
32+
}
33+
}
34+
public:
35+
void fly() const {
36+
assert(_duck);
37+
38+
for (int i = 0; i < _random; i++) {
39+
_duck->fly();
40+
}
41+
}
42+
public:
43+
void gobble() const {
44+
assert(_duck);
45+
_duck->quack();
46+
}
47+
};
48+
49+
} // namespace Ducks
50+
} // namespace Adapter
3851
} // namespace HeadFirstDesignPatterns
3952

4053
#endif

HeadFirstDesignPatterns/c_plusplus/Silver/Adapter/Ducks/Ducks.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@ include "Ducks.hpp"
22

33
using namespace HeadFirstDesignPatterns::Adapter::Ducks;
44

5-
void testDuck( const Duck* duck ) {
6-
duck->quack();
7-
duck->fly();
5+
void testDuck(const Duck* duck) {
6+
duck->quack();
7+
duck->fly();
88
}
99

10-
int main( int argc, char* argv[] ) {
11-
12-
std::auto_ptr< MallardDuck > duck( new MallardDuck() );
13-
10+
int main(int argc, char* argv[]) {
11+
std::auto_ptr< MallardDuck > duck(new MallardDuck());
1412
#ifdef _DUCK_ADAPTER_
15-
std::auto_ptr< Turkey > duckAdapter( new DuckAdapter( duck.get() ) );
16-
17-
for( int i = 0; i < 10; i++ ) {
18-
std::cout << "The DuckAdapter says..." << std::endl;
19-
duckAdapter->gobble();
20-
duckAdapter->fly();
21-
}
22-
#else
23-
std::auto_ptr< WildTurkey > turkey( new WildTurkey() );
24-
std::auto_ptr< Duck > turkeyAdapter( new TurkeyAdapter( turkey.get() ) );
13+
std::auto_ptr< Turkey > duckAdapter(new DuckAdapter(duck.get()));
2514

26-
std::cout << "The Turkey says..." << std::endl;
27-
turkey->gobble();
28-
turkey->fly();
15+
for (int i = 0; i < 10; i++) {
16+
std::cout << "The DuckAdapter says..." << std::endl;
17+
duckAdapter->gobble();
18+
duckAdapter->fly();
19+
}
2920

30-
std::cout << std::endl << "The Duck says..." << std::endl;
31-
testDuck( duck.get() );
32-
33-
std::cout << std::endl << "The TurkeyAdapter says..." << std::endl;
34-
testDuck( turkeyAdapter.get() );
21+
#else
22+
std::auto_ptr< WildTurkey > turkey(new WildTurkey());
23+
std::auto_ptr< Duck > turkeyAdapter(new TurkeyAdapter(turkey.get()));
24+
std::cout << "The Turkey says..." << std::endl;
25+
turkey->gobble();
26+
turkey->fly();
27+
std::cout << std::endl << "The Duck says..." << std::endl;
28+
testDuck(duck.get());
29+
std::cout << std::endl << "The TurkeyAdapter says..." << std::endl;
30+
testDuck(turkeyAdapter.get());
3531
#endif
36-
37-
return 0;
32+
return 0;
3833
}

HeadFirstDesignPatterns/c_plusplus/Silver/Adapter/Ducks/Ducks.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef _HFDP_CPP_ADAPTER_BIRDS_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_BIRDS_HPP_
22
#define _HFDP_CPP_ADAPTER_BIRDS_HPP_
33

44
#include "../../Standard.h"
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
#ifndef _HFDP_CPP_ADAPTER_MALLARD_DUCK_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_MALLARD_DUCK_HPP_
22
#define _HFDP_CPP_ADAPTER_MALLARD_DUCK_HPP_
33

44
#include "Ducks.hpp"
55

66
namespace HeadFirstDesignPatterns {
7-
namespace Adapter {
8-
namespace Ducks {
7+
namespace Adapter {
8+
namespace Ducks {
99

10-
class MallardDuck : public Duck {
10+
class MallardDuck : public Duck {
1111

12-
public: void fly() const {
13-
std::cout << "I'm flying" << std::endl;
14-
}
15-
public: void quack() const {
16-
std::cout << "Quack" << std::endl;
17-
}
18-
};
12+
public:
13+
void fly() const {
14+
std::cout << "I'm flying" << std::endl;
15+
}
16+
public:
17+
void quack() const {
18+
std::cout << "Quack" << std::endl;
19+
}
20+
};
1921

20-
} // namespace Ducks
21-
} // namespace Adapter
22+
} // namespace Ducks
23+
} // namespace Adapter
2224
} // namespace HeadFirstDesignPatterns
2325

2426
#endif
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
#ifndef _HFDP_CPP_ADAPTER_TURKEY_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_TURKEY_HPP_
22
#define _HFDP_CPP_ADAPTER_TURKEY_HPP_
33

44
#include "Ducks.hpp"
55

66
namespace HeadFirstDesignPatterns {
7-
namespace Adapter {
8-
namespace Ducks {
7+
namespace Adapter {
8+
namespace Ducks {
99

10-
class Turkey {
10+
class Turkey {
1111

12-
public: virtual ~Turkey() {
13-
}
14-
public: virtual void gobble() const = 0;
15-
public: virtual void fly() const = 0;
16-
};
12+
public:
13+
virtual ~Turkey() {
14+
}
15+
public:
16+
virtual void gobble() const = 0;
17+
public:
18+
virtual void fly() const = 0;
19+
};
1720

18-
} // namespace Ducks
19-
} // namespace Adapter
21+
} // namespace Ducks
22+
} // namespace Adapter
2023
} // namespace HeadFirstDesignPatterns
2124

2225
#endif
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
#ifndef _HFDP_CPP_ADAPTER_TURKEY_ADAPTER_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_TURKEY_ADAPTER_HPP_
22
#define _HFDP_CPP_ADAPTER_TURKEY_ADAPTER_HPP_
33

44
#include "Ducks.hpp"
55

66
namespace HeadFirstDesignPatterns {
7-
namespace Adapter {
8-
namespace Ducks {
9-
10-
class TurkeyAdapter : public Duck {
11-
12-
private: const Turkey* _turkey;
13-
14-
private: TurkeyAdapter( const TurkeyAdapter& ); // Disable copy constructor
15-
private: void operator=( const TurkeyAdapter& ); // Disable assignment operator
16-
17-
public: explicit TurkeyAdapter( const Turkey* turkey ) :
18-
_turkey( turkey ) { assert( turkey );
19-
}
20-
public: void fly() const { assert( _turkey );
21-
for( int i = 0; i < 5; i++ ) {
22-
_turkey->fly();
23-
}
24-
}
25-
public: void quack() const { assert( _turkey );
26-
_turkey->gobble();
27-
}
28-
};
29-
30-
} // namespace Ducks
31-
} // namespace Adapter
7+
namespace Adapter {
8+
namespace Ducks {
9+
10+
class TurkeyAdapter : public Duck {
11+
12+
private:
13+
const Turkey* _turkey;
14+
15+
private:
16+
TurkeyAdapter(const TurkeyAdapter&); // Disable copy constructor
17+
private:
18+
void operator=(const TurkeyAdapter&); // Disable assignment operator
19+
20+
public:
21+
explicit TurkeyAdapter(const Turkey* turkey) :
22+
_turkey(turkey) {
23+
assert(turkey);
24+
}
25+
public:
26+
void fly() const {
27+
assert(_turkey);
28+
29+
for (int i = 0; i < 5; i++) {
30+
_turkey->fly();
31+
}
32+
}
33+
public:
34+
void quack() const {
35+
assert(_turkey);
36+
_turkey->gobble();
37+
}
38+
};
39+
40+
} // namespace Ducks
41+
} // namespace Adapter
3242
} // namespace HeadFirstDesignPatterns
3343

3444
#endif
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
#ifndef _HFDP_CPP_ADAPTER_WILD_TURKEY_HPP_
1+
#ifndef _HFDP_CPP_ADAPTER_WILD_TURKEY_HPP_
22
#define _HFDP_CPP_ADAPTER_WILD_TURKEY_HPP_
33

44
#include "Ducks.hpp"
55

66
namespace HeadFirstDesignPatterns {
7-
namespace Adapter {
8-
namespace Ducks {
7+
namespace Adapter {
8+
namespace Ducks {
99

10-
class WildTurkey : public Turkey {
10+
class WildTurkey : public Turkey {
1111

12-
public: void fly() const {
13-
std::cout << "I'm flying a short distance" << std::endl;
14-
}
15-
public: void gobble() const {
16-
std::cout << "Gobble gobble" << std::endl;
17-
}
18-
};
12+
public:
13+
void fly() const {
14+
std::cout << "I'm flying a short distance" << std::endl;
15+
}
16+
public:
17+
void gobble() const {
18+
std::cout << "Gobble gobble" << std::endl;
19+
}
20+
};
1921

20-
} // namespace Ducks
21-
} // namespace Adapter
22+
} // namespace Ducks
23+
} // namespace Adapter
2224
} // namespace HeadFirstDesignPatterns
2325

2426
#endif

0 commit comments

Comments
 (0)