Skip to content

Commit 8fa92c6

Browse files
Eder DuranEder Duran
authored andcommitted
Adapter Example
1 parent d0ce78d commit 8fa92c6

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

src/Adapter/Conceptual/Output.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Client: I can work just fine with the Target objects:
2+
Target: The default target's behavior.
3+
4+
Client: The Adaptee class has a weird interface. See, I don't understand it:
5+
Adaptee: .eetpadA eht fo roivaheb laicepS
6+
7+
Client: But I can work with it via the Adapter:
8+
Adapter: (TRANSLATED) Special behavior of the Adaptee.

src/Adapter/Conceptual/main.cc

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <algorithm>
4+
/**
5+
* EN: Adapter Design Pattern
6+
*
7+
* Intent: Provides a unified interface that allows objects with incompatible
8+
* interfaces to collaborate.
9+
*
10+
* RU: Паттерн Адаптер
11+
*
12+
* Назначение: Позволяет объектам с несовместимыми интерфейсами работать вместе.
13+
*/
14+
/**
15+
* EN: The Target defines the domain-specific interface used by the client code.
16+
*
17+
* RU: Целевой класс объявляет интерфейс, с которым может работать клиентский
18+
* код.
19+
*/
20+
class Target
21+
{
22+
public:
23+
virtual ~Target() = default;
24+
25+
virtual std::string Request() const
26+
{
27+
return "Target: The default target's behavior.";
28+
}
29+
};
30+
/**
31+
* EN: The Adaptee contains some useful behavior, but its interface is
32+
* incompatible with the existing client code. The Adaptee needs some adaptation
33+
* before the client code can use it.
34+
*
35+
* RU: Адаптируемый класс содержит некоторое полезное поведение, но его
36+
* интерфейс несовместим с существующим клиентским кодом. Адаптируемый класс
37+
* нуждается в некоторой доработке, прежде чем клиентский код сможет его
38+
* использовать.
39+
*/
40+
class Adaptee
41+
{
42+
public:
43+
std::string SpecificRequest() const
44+
{
45+
return ".eetpadA eht fo roivaheb laicepS";
46+
}
47+
};
48+
/**
49+
* EN: The Adapter makes the Adaptee's interface compatible with the Target's
50+
* interface.
51+
*
52+
* RU: Адаптер делает интерфейс Адаптируемого класса совместимым с целевым
53+
* интерфейсом.
54+
*/
55+
class Adapter : public Target
56+
{
57+
private:
58+
Adaptee *adaptee_;
59+
60+
public:
61+
Adapter(Adaptee *adaptee) : adaptee_(adaptee)
62+
{
63+
}
64+
std::string Request() const override
65+
{
66+
std::string to_reverse = this->adaptee_->SpecificRequest();
67+
std::reverse(to_reverse.begin(), to_reverse.end());
68+
return "Adapter: (TRANSLATED) " + to_reverse;
69+
}
70+
};
71+
/**
72+
* EN: The client code supports all classes that follow the Target interface.
73+
*
74+
* RU: Клиентский код поддерживает все классы, использующие целевой интерфейс.
75+
*/
76+
void ClientCode(const Target *target)
77+
{
78+
std::cout << target->Request();
79+
}
80+
81+
int main()
82+
{
83+
std::cout << "Client: I can work just fine with the Target objects:\n";
84+
Target *target = new Target;
85+
ClientCode(target);
86+
std::cout << "\n\n";
87+
Adaptee *adaptee = new Adaptee;
88+
std::cout << "Client: The Adaptee class has a weird interface. See, I don't understand it:\n";
89+
std::cout << "Adaptee: " << adaptee->SpecificRequest();
90+
std::cout << "\n\n";
91+
std::cout << "Client: But I can work with it via the Adapter:\n";
92+
Adapter *adapter = new Adapter(adaptee);
93+
ClientCode(adapter);
94+
std::cout << "\n";
95+
96+
delete target;
97+
delete adaptee;
98+
delete adapter;
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)