Skip to content

Commit 2cf40c6

Browse files
committed
Добавьте файлы проекта.
1 parent 473046c commit 2cf40c6

18 files changed

+602
-0
lines changed

BodyPart.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "BodyPart.h"
2+
3+
BodyPart::BodyPart(Brain* brain)
4+
{
5+
this->brain = brain;
6+
}
7+
8+
void BodyPart::changed()
9+
{
10+
brain->somethingHappenedToBodyPart(this);
11+
}

BodyPart.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include "Brain.h"
3+
4+
// sbstract base collegue
5+
class BodyPart abstract
6+
{
7+
protected:
8+
Brain* brain; // body part knows about brain (mediator)
9+
10+
public:
11+
BodyPart(Brain* brain);
12+
virtual void changed();
13+
};

Brain.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "Brain.h"
2+
3+
#include "Ear.h"
4+
#include "Eye.h"
5+
#include "Face.h"
6+
#include "Hand.h"
7+
#include "Leg.h"
8+
9+
Brain::Brain()
10+
{
11+
createBodyParts();
12+
}
13+
14+
class Ear;
15+
16+
void Brain::createBodyParts()
17+
{
18+
ear = new Ear(this);
19+
eye = new Eye(this);
20+
face = new Face(this);
21+
hand = new Hand(this);
22+
leg = new Leg(this);
23+
}
24+
25+
26+
void Brain::somethingHappenedToBodyPart(BodyPart* bodyPart)
27+
{
28+
// I'm so sorry, Barbara Liskov ;)
29+
if (dynamic_cast<Ear*>(bodyPart) != nullptr)
30+
{
31+
string heardSounds = ((Ear*)bodyPart)->getSounds();
32+
33+
if (heardSounds.find("stupid") != std::string::npos)
34+
{
35+
// attacking offender
36+
leg->stepForward();
37+
hand->hitPersonNearYou();
38+
leg->kick();
39+
}
40+
else if (heardSounds.find("cool") != std::string::npos)
41+
{
42+
face->smile();
43+
}
44+
else
45+
{
46+
cout << "OK, I give you another try\n";
47+
}
48+
}
49+
else if (dynamic_cast<Eye*>(bodyPart) != nullptr)
50+
{
51+
cout << "brain can analyze what you see and can react appropriately using different body parts\n";
52+
}
53+
else if (dynamic_cast<Hand*>(bodyPart) != nullptr)
54+
{
55+
Hand* h = (Hand*)bodyPart;
56+
57+
if (hand->doesItHurt())
58+
{
59+
leg->stepBack();
60+
}
61+
else if (hand->isItNice()) {
62+
leg->stepForward();
63+
hand->embrace();
64+
}
65+
else
66+
{
67+
cout << "OK, you can touch this :)\n";
68+
}
69+
}
70+
else if (dynamic_cast<Leg*>(bodyPart) != nullptr)
71+
{
72+
cout << "leg can also feel something if you would like it to\n";
73+
}
74+
}

Brain.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
class Ear;
4+
class Eye;
5+
class Face;
6+
class Hand;
7+
class Leg;
8+
class BodyPart;
9+
10+
// Mediator
11+
class Brain
12+
{
13+
public:
14+
Ear* ear;
15+
Eye* eye;
16+
Face* face;
17+
Hand* hand;
18+
Leg* leg;
19+
public:
20+
Brain();
21+
private:
22+
void createBodyParts();
23+
public:
24+
void somethingHappenedToBodyPart(BodyPart* bodyPart);
25+
};

Ear.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "Ear.h"
2+
3+
Ear::Ear(Brain* brain) : BodyPart(brain) {}
4+
5+
void Ear::hearSomething()
6+
{
7+
cout << "Enter what you hear (try to type \"cool\" or \"stupid\"): ";
8+
getline(cin, sounds);
9+
10+
changed();
11+
}
12+
13+
string Ear::getSounds()
14+
{
15+
return sounds;
16+
}

Ear.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <string>
4+
using namespace std;
5+
6+
#include "Brain.h"
7+
#include "BodyPart.h"
8+
9+
class Ear : public BodyPart
10+
{
11+
string sounds;
12+
public:
13+
Ear(Brain* brain);
14+
void hearSomething();
15+
string getSounds();
16+
};

Eye.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "Eye.h"
2+
3+
Eye::Eye(Brain* brain) : BodyPart(brain) {}
4+
5+
void Eye::seeSomething()
6+
{
7+
cout << "Enter what you see: ";
8+
getline(cin, thingsAround);
9+
10+
changed();
11+
}

Eye.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <string>
4+
using namespace std;
5+
6+
#include "Brain.h"
7+
#include "BodyPart.h"
8+
9+
class Eye : public BodyPart
10+
{
11+
string thingsAround;
12+
public:
13+
Eye(Brain* brain);
14+
void seeSomething();
15+
};

Face.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "Face.h"
2+
3+
Face::Face(Brain* brain) : BodyPart(brain) {}
4+
5+
void Face::smile()
6+
{
7+
cout << "FACE: Smiling :)\n";
8+
}

Face.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <iostream>
3+
using namespace std;
4+
5+
#include "Brain.h"
6+
#include "BodyPart.h"
7+
8+
class Face : public BodyPart
9+
{
10+
public:
11+
Face(Brain* brain);
12+
void smile();
13+
};

Hand.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Hand.h"
2+
3+
Hand::Hand(Brain* brain) : BodyPart(brain) {}
4+
5+
void Hand::feelSomething()
6+
{
7+
cout << "What you feel is soft? (Yes/No) ";
8+
string answer;
9+
cin >> answer;
10+
11+
if (answer != "" && answer[0] == 'Y')
12+
{
13+
isSoft = true;
14+
}
15+
16+
cout << "What you feel is hurting? (Yes/No) ";
17+
cin >> answer;
18+
if (answer != "" && answer[0] == 'Y')
19+
{
20+
isHurting = true;
21+
}
22+
23+
changed();
24+
}
25+
26+
void Hand::hitPersonNearYou()
27+
{
28+
cout << "HAND: Just hit offender...\n"; // ñòóêíóòü îáèä÷èêà
29+
}
30+
31+
void Hand::embrace()
32+
{
33+
cout << "HAND: Embracing what is in front of you...\n"; // îáíèìàøêè
34+
}
35+
36+
bool Hand::doesItHurt()
37+
{
38+
return isHurting;
39+
}
40+
41+
bool Hand::isItNice()
42+
{
43+
return !isHurting && isSoft;
44+
}

Hand.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include <iostream>
3+
using namespace std;
4+
5+
#include "Brain.h"
6+
#include "BodyPart.h"
7+
8+
class Hand : public BodyPart
9+
{
10+
bool isSoft;
11+
bool isHurting;
12+
13+
public:
14+
Hand(Brain* brain);
15+
void feelSomething();
16+
void hitPersonNearYou();
17+
void embrace();
18+
bool doesItHurt();
19+
bool isItNice();
20+
};

Leg.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "Leg.h"
2+
3+
Leg::Leg(Brain* brain) : BodyPart(brain) {}
4+
5+
6+
void Leg::kick()
7+
{
8+
cout << "LEG: Just kicked offender in front of you...\n";
9+
}
10+
11+
void Leg::stepBack()
12+
{
13+
cout << "LEG: Stepping back...\n";
14+
}
15+
16+
void Leg::stepForward()
17+
{
18+
cout << "LEG: Stepping forward...\n";
19+
}

Leg.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <iostream>
3+
using namespace std;
4+
5+
#include "Brain.h"
6+
#include "BodyPart.h"
7+
8+
class Leg : public BodyPart
9+
{
10+
public:
11+
Leg(Brain* brain);
12+
void kick();
13+
void stepBack();
14+
void stepForward();
15+
};

Main.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
#include "Brain.h"
5+
#include "BodyPart.h"
6+
#include "Ear.h"
7+
#include "Eye.h"
8+
#include "Face.h"
9+
#include "Hand.h"
10+
#include "Leg.h"
11+
12+
// http://cpp-reference.ru/patterns/behavioral-patterns/mediator/
13+
14+
int main()
15+
{
16+
system("title Mediator Pattern Example");
17+
18+
Brain* human = new Brain();
19+
20+
string line = "start";
21+
22+
while (line != "")
23+
{
24+
cout << "Enter body part ('ear','eye','hand' or empty to exit): ";
25+
getline(cin, line);
26+
27+
if (line == "ear" || line == "Ear")
28+
{
29+
human->ear->hearSomething();
30+
}
31+
else if (line == "Eye" || line == "eye")
32+
{
33+
human->eye->seeSomething();
34+
}
35+
else if (line == "Hand" || line == "hand")
36+
{
37+
human->hand->feelSomething();
38+
}
39+
// ...
40+
}
41+
}

0 commit comments

Comments
 (0)