Skip to content

Commit cdc39c9

Browse files
author
Ehsan Gazar
committed
Completed all design patterns in c++
1 parent 9dbdc99 commit cdc39c9

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

behavioral/state.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
3+
class Mood
4+
{
5+
public:
6+
virtual ~Mood() { }
7+
virtual void talk()=0;
8+
};
9+
10+
class Happy : public Mood
11+
{
12+
public:
13+
void talk() { std::cout << "Hello world!" << std::endl; }
14+
};
15+
16+
void hello_world(Mood & mood)
17+
{
18+
mood.talk();
19+
}
20+
21+
int main()
22+
{
23+
Happy mood;
24+
hello_world(mood);
25+
return 0;
26+
}

behavioral/strategy.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
class Strategy
5+
{
6+
public:
7+
virtual ~Strategy() { }
8+
virtual std::string format(const std::string &, const std::string &) const=0;
9+
};
10+
11+
class Formatter : public Strategy
12+
{
13+
public:
14+
std::string format(const std::string & s1, const std::string & s2) const
15+
{
16+
return s1 + " " + s2 + "!";
17+
}
18+
};
19+
20+
void hello_world(const Strategy & strategy)
21+
{
22+
std::cout << strategy.format("Hello", "world") << std::endl;
23+
}
24+
25+
int main()
26+
{
27+
hello_world(Formatter());
28+
return 0;
29+
}

behavioral/template-method.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
class HelloWorld
5+
{
6+
public:
7+
virtual ~HelloWorld() { }
8+
void output()
9+
{
10+
write_string("Hello world!");
11+
write_endl();
12+
};
13+
virtual void write_string(const std::string &)=0;
14+
virtual void write_endl()=0;
15+
};
16+
17+
class HelloWorldImpl : public HelloWorld
18+
{
19+
public:
20+
void write_string(const std::string & str)
21+
{
22+
std::cout << str;
23+
}
24+
void write_endl()
25+
{
26+
std::cout << std::endl;
27+
}
28+
};
29+
30+
void hello_world(HelloWorld & hw)
31+
{
32+
hw.output();
33+
}
34+
35+
int main()
36+
{
37+
HelloWorldImpl hw;
38+
hello_world(hw);
39+
return 0;
40+
}

behavioral/visitor.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <vector>
2+
#include <string>
3+
#include <iostream>
4+
5+
class Visitor
6+
{
7+
public:
8+
virtual ~Visitor() { }
9+
virtual void on_recipient(const std::string &)=0;
10+
};
11+
12+
class Recipients
13+
{
14+
std::vector<std::string> recipients;
15+
public:
16+
void add_recipient(const std::string & recipient)
17+
{
18+
recipients.push_back(recipient);
19+
}
20+
void visit(Visitor & visitor) const
21+
{
22+
for(std::vector<std::string>::const_iterator recipient = recipients.begin();
23+
recipient != recipients.end();
24+
++recipient)
25+
{
26+
visitor.on_recipient(*recipient);
27+
}
28+
}
29+
};
30+
31+
class PrintHello : public Visitor
32+
{
33+
public:
34+
void on_recipient(const std::string & recipient)
35+
{
36+
std::cout << "Hello " << recipient << "!" << std::endl;
37+
}
38+
};
39+
40+
void hello_world(Visitor & visitor)
41+
{
42+
Recipients recipients;
43+
recipients.add_recipient("world");
44+
recipients.visit(visitor);
45+
}
46+
47+
int main()
48+
{
49+
PrintHello visitor;
50+
hello_world(visitor);
51+
return 0;
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
class Recipient
5+
{
6+
public:
7+
virtual ~Recipient() { }
8+
virtual std::string name() const=0;
9+
};
10+
11+
class NullRecipient : public Recipient
12+
{
13+
public:
14+
std::string name() const { return "nobody"; }
15+
};
16+
17+
class World : public Recipient
18+
{
19+
public:
20+
std::string name() const { return "world"; }
21+
};
22+
23+
void hello_world(const Recipient & recipient = NullRecipient())
24+
{
25+
std::cout << "Hello " << recipient.name() << "!" << std::endl;
26+
}
27+
28+
int main()
29+
{
30+
hello_world(World());
31+
return 0;
32+
}

structural-patterns/out

-13.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)