Skip to content

Commit 0a586dc

Browse files
committed
rename folder
1 parent 1250e50 commit 0a586dc

23 files changed

+469
-0
lines changed

Adapter/ducks/class_adapter/Duck.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef DUCK_H
2+
#define DUCK_H
3+
4+
class Duck {
5+
public:
6+
virtual ~Duck () = default;
7+
virtual void quack() = 0;
8+
virtual void fly() = 0;
9+
};
10+
11+
#endif /* DUCK_H */

Adapter/ducks/class_adapter/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CXX = clang++
2+
CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17
3+
DEPS = Duck.hpp MallardDuck.hpp TurkeyAdapter.hpp Turkey.hpp WildTurkey.hpp
4+
5+
TARGET = classAdapterTestDrive
6+
OBJ = $(TARGET).o
7+
8+
%.o: %.cpp $(DEPS)
9+
$(CXX) -c -o $@ $< $(CFLAGS)
10+
11+
$(TARGET): $(OBJ)
12+
$(CXX) -o $@ $^ $(CFLAGS)
13+
14+
clean:
15+
rm -f *.o $(TARGET)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef MALLARD_DUCK_H
2+
#define MALLARD_DUCK_H
3+
4+
#include "Duck.hpp"
5+
#include <iostream>
6+
7+
class MallardDuck : public Duck {
8+
public:
9+
void quack() override { std::cout << "Quack\n"; }
10+
void fly() override { std::cout << "I'm flying\n"; }
11+
};
12+
13+
#endif /* MALLARD_DUCK_H */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef MALLARD_DUCK_ADAPTER_H
2+
#define MALLARD_DUCK_ADAPTER_H
3+
4+
#include "Turkey.hpp"
5+
#include "MallardDuck.hpp"
6+
#include "SimpleRandom.hpp"
7+
8+
class MallardDuckAdapter : public Turkey, private MallardDuck {
9+
public:
10+
MallardDuckAdapter() = default;
11+
void gobble() override { MallardDuck::quack(); }
12+
void fly() override;
13+
private:
14+
SimpleRandom rand;
15+
16+
};
17+
18+
inline
19+
void
20+
MallardDuckAdapter::fly()
21+
{
22+
if (rand.nextInt(5) == 0)
23+
MallardDuck::fly();
24+
}
25+
26+
#endif /* MALLARD_DUCK_ADAPTER_H */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef SIMPLE_RANDOM_H
2+
#define SIMPLE_RANDOM_H
3+
4+
#include <memory>
5+
#include <random>
6+
#include <ctime>
7+
8+
class SimpleRandom {
9+
public:
10+
using rand_type = std::default_random_engine::result_type;
11+
SimpleRandom() = default;
12+
rand_type nextInt(const unsigned i);
13+
private:
14+
std::default_random_engine e = std::default_random_engine(std::time(nullptr));
15+
std::uniform_int_distribution<unsigned> u;
16+
};
17+
18+
19+
SimpleRandom::rand_type
20+
SimpleRandom::nextInt(const unsigned i)
21+
{
22+
u = std::uniform_int_distribution<unsigned>(0,i);
23+
e.discard(1);
24+
return u(e);
25+
}
26+
27+
#endif /* SIMPLE_RANDOM_H */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef TURKEY_H
2+
#define TURKEY_H
3+
4+
class Turkey {
5+
public:
6+
virtual ~Turkey () = default;
7+
virtual void gobble() = 0;
8+
virtual void fly() = 0;
9+
};
10+
11+
#endif /* TURKEY_H */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef WILD_TURKEY_H
2+
#define WILD_TURKEY_H
3+
4+
#include "Turkey.hpp"
5+
#include <iostream>
6+
7+
class WildTurkey : public Turkey {
8+
public:
9+
void gobble() override { std::cout << "Gobble, gooble\n"; }
10+
void fly() override { std::cout << "I'm flying a short distance\n"; }
11+
};
12+
13+
#endif /* WILD_TURKEY_H */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef WILD_TURKEY_ADAPTER_H
2+
#define WILD_TURKEY_ADAPTER_H
3+
4+
#include "Duck.hpp"
5+
#include "Turkey.hpp"
6+
#include "WildTurkey.hpp"
7+
#include <iostream>
8+
9+
/*
10+
* Multiple Inheritance:
11+
* Inherit publicly from target (Duck) and privately from adaptee (WildTurky) thus the adapter would be a subclass of target but not the adaptee (Design Pattern, 144). The former is an interface (abstract) while the later is an implimentation (concrete).
12+
*/
13+
14+
class WildTurkeyAdapter : public Duck, private WildTurkey {
15+
public:
16+
WildTurkeyAdapter() = default;
17+
void quack() override { WildTurkey::gobble(); }
18+
void fly() override;
19+
};
20+
21+
inline
22+
void
23+
WildTurkeyAdapter::fly()
24+
{
25+
for (int i = 0; i < 5; ++i)
26+
WildTurkey::fly();
27+
}
28+
29+
#endif /* WILD_TURKEY_ADAPTER_H */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "Duck.hpp"
2+
#include "Turkey.hpp"
3+
#include "WildTurkey.hpp"
4+
#include "WildTurkeyAdapter.hpp"
5+
#include "MallardDuck.hpp"
6+
#include "MallardDuckAdapter.hpp"
7+
#include "WildTurkey.hpp"
8+
9+
#include <iostream>
10+
11+
void testDuck(Duck *duck) {
12+
duck->quack();
13+
duck->fly();
14+
}
15+
16+
void testTurkey(Turkey *turkey) {
17+
turkey->gobble();
18+
turkey->fly();
19+
}
20+
21+
int main()
22+
{
23+
auto turkey = WildTurkey();
24+
std::cout << "The Turkey says...\n";
25+
turkey.gobble();
26+
turkey.fly();
27+
28+
auto wildTurkeyAdapter = WildTurkeyAdapter(); // instantiate adapter
29+
std::cout << "\nThe TurkeyAdaptor says...\n";
30+
testDuck(&wildTurkeyAdapter); // pass it as Duck
31+
32+
auto mallardDuckAdapter = MallardDuckAdapter(); // instantiate
33+
std::cout << "\nThe DuckAdaptor says...\n";
34+
for (int i = 0; i < 10; ++i)
35+
testTurkey(&mallardDuckAdapter); // pass it as Turkey
36+
37+
return 0;
38+
}

Adapter/ducks/object_adapter/Duck.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef DUCK_H
2+
#define DUCK_H
3+
4+
class Duck {
5+
public:
6+
virtual ~Duck () = default;
7+
virtual void quack() = 0;
8+
virtual void fly() = 0;
9+
};
10+
11+
#endif /* DUCK_H */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef DUCK_ADAPTER_H
2+
#define DUCK_ADAPTER_H
3+
4+
#include "Duck.hpp"
5+
#include "Turkey.hpp"
6+
#include "SimpleRandom.hpp" // mimics Java's Random
7+
#include <iostream>
8+
#include <random>
9+
#include <ctime>
10+
11+
class DuckAdapter : public Turkey {
12+
public:
13+
DuckAdapter() = default;
14+
DuckAdapter(Duck *tur) : turkey(tur) { }
15+
void gobble() override { turkey->quack(); }
16+
void fly() override;
17+
private:
18+
Duck *turkey;
19+
SimpleRandom rand; // See header for implementation
20+
};
21+
22+
inline
23+
void
24+
DuckAdapter::fly()
25+
{
26+
if (rand.nextInt(5) == 0)
27+
turkey->fly();
28+
}
29+
30+
#endif /* DUCK_ADAPTER_H */

Adapter/ducks/object_adapter/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CXX = clang++
2+
CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17
3+
DEPS = DuckAdapter.hpp Duck.hpp MallardDuck.hpp SimpleRandom.hpp \
4+
TurkeyAdapter.hpp Turkey.hpp WildTurkey.hpp challenge/Drone.hpp \
5+
challenge/DroneAdapter.hpp challenge/SuperDrone.hpp
6+
TARGET = duckTestDrive
7+
TARGET2 = turkeyTestDrive
8+
OBJ = $(TARGET).o
9+
OBJ2 = $(TARGET2).o
10+
11+
%.o: %.cpp $(DEPS)
12+
$(CXX) -c -o $@ $< $(CFLAGS)
13+
14+
$(TARGET): $(OBJ)
15+
$(CXX) -o $@ $^ $(CFLAGS)
16+
17+
$(TARGET2): $(OBJ2)
18+
$(CXX) -o $@ $^ $(CFLAGS)
19+
20+
clean:
21+
rm -f *.o $(TARGET) $(TARGET2)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef MALLARD_DUCK_H
2+
#define MALLARD_DUCK_H
3+
4+
#include "Duck.hpp"
5+
#include <iostream>
6+
7+
class MallardDuck : public Duck {
8+
public:
9+
void quack() override { std::cout << "Quack\n"; }
10+
void fly() override { std::cout << "I'm flying\n"; }
11+
};
12+
13+
#endif /* MALLARD_DUCK_H */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SIMPLE_RANDOM_H
2+
#define SIMPLE_RANDOM_H
3+
4+
#include <memory>
5+
#include <random>
6+
#include <ctime>
7+
8+
class SimpleRandom {
9+
public:
10+
using rand_type = std::default_random_engine::result_type;
11+
SimpleRandom() = default;
12+
rand_type nextInt(const unsigned i);
13+
private:
14+
std::default_random_engine e = std::default_random_engine(std::time(nullptr));
15+
std::uniform_int_distribution<unsigned> u;
16+
};
17+
18+
19+
inline
20+
SimpleRandom::rand_type
21+
SimpleRandom::nextInt(const unsigned i)
22+
{
23+
u = std::uniform_int_distribution<unsigned>(0,i);
24+
e.discard(1);
25+
return u(e);
26+
}
27+
28+
#endif /* SIMPLE_RANDOM_H */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef TURKEY_H
2+
#define TURKEY_H
3+
4+
class Turkey {
5+
public:
6+
virtual ~Turkey () = default;
7+
virtual void gobble() = 0;
8+
virtual void fly() = 0;
9+
};
10+
11+
#endif /* TURKEY_H */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef TURKEY_ADAPTER_H
2+
#define TURKEY_ADAPTER_H
3+
4+
#include "Duck.hpp"
5+
#include "Turkey.hpp"
6+
#include <iostream>
7+
8+
class TurkeyAdapter : public Duck {
9+
public:
10+
TurkeyAdapter(Turkey *tur) : turkey(tur) { }
11+
void quack() override { turkey->gobble(); }
12+
void fly() override;
13+
private:
14+
Turkey *turkey;
15+
};
16+
17+
inline
18+
void
19+
TurkeyAdapter::fly()
20+
{
21+
for (int i = 0; i < 5; ++i)
22+
turkey->fly();
23+
}
24+
25+
#endif /* TURKEY_ADAPTER_H */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef WILD_TURKEY_H
2+
#define WILD_TURKEY_H
3+
4+
#include "Turkey.hpp"
5+
#include <iostream>
6+
7+
class WildTurkey : public Turkey {
8+
public:
9+
void gobble() override { std::cout << "Gobble, gobble\n"; }
10+
void fly() override { std::cout << "I'm flying a short distance\n"; }
11+
};
12+
13+
#endif /* WILD_TURKEY_H */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef DRONE_H
2+
#define DRONE_H
3+
4+
class Drone {
5+
public:
6+
virtual ~Drone () = default;
7+
virtual void beep() = 0;
8+
virtual void spin_rotors() = 0;
9+
virtual void take_off() = 0;
10+
};
11+
12+
#endif /* DRONE_H */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef DRONE_ADAPTER_H
2+
#define DRONE_ADAPTER_H
3+
4+
#include "../Duck.hpp"
5+
#include "Drone.hpp"
6+
7+
class DroneAdapter : public Duck {
8+
public:
9+
DroneAdapter(Drone *d) : drone(d) {}
10+
virtual ~DroneAdapter() = default;
11+
void fly() override { drone->spin_rotors(); drone->take_off(); }
12+
void quack() override { drone->beep(); }
13+
private:
14+
Drone *drone;
15+
16+
};
17+
18+
#endif /* DRONE_ADAPTER_H */
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef SUPER_DRONE_H
2+
#define SUPER_DRONE_H
3+
4+
#include "Drone.hpp"
5+
#include <iostream>
6+
7+
class SuperDrone : public Drone {
8+
public:
9+
void beep() override { std::cout << "Beep beep beep\n"; }
10+
void spin_rotors() override { std::cout << "Rotors are spinning\n"; }
11+
void take_off() override { std::cout << "Taking off\n"; }
12+
};
13+
14+
#endif /* SUPER_DRONE_H */

0 commit comments

Comments
 (0)