- #include <iostream>
- using namespace std;
-
- class Target {
- public:
- virtual void targetMethod() {
- cout << "Original Method." << endl;
- }
- };
-
- class Adaptee {
- public:
- void Method() {
- cout << "Adaptee Method." << endl;
- }
- };
-
- // 継承 Adapter
- class DeriveAdapter : public Target, public Adaptee {
- public:
- virtual void targetMethod() {
- Method(); // Adaptee::method()
- }
- };
-
- // 包含 Adapter
- class InclusionAdapter : public Target {
- public:
- virtual void targetMethod() {
- _adaptee.Method();
- }
- private:
- Adaptee _adaptee;
- };
-
- // client
- int main() {
- Target *t = new Target();
- t->targetMethod();
- // 継承 Adapter
- Target *t1 = new DeriveAdapter();
- t1->targetMethod();
- // 包含 Adapter
- Target *t2 = new InclusionAdapter();
- t2->targetMethod();
-
- delete t;
- delete t1;
- delete t2;
- return 0;
- }
-
Original Method.
Adaptee Method.
Adaptee Method.
- #include <iostream>
- using namespace std;
-
- // The 'Target' class
- class Target {
- public:
- virtual ~Target() { }
- virtual void Request() {
- cout << "Called Target Request()" << endl;
- }
- };
-
- // The 'Adaptee' class
- class Adaptee {
- public:
- void SpecificRequest() {
- cout << "Called SpecificRequest()" << endl;
- }
- };
-
- // The 'Adapter' class
- class Adapter : public Target {
- public:
- Adapter() {
- _adaptee = new Adaptee();
- }
- virtual ~Adapter() {
- delete _adaptee;
- }
- virtual void Request() {
- _adaptee->SpecificRequest();
- }
- private:
- Adaptee *_adaptee;
- };
-
- // client
- int main() {
- Target *target = new Adapter();
- target->Request();
- delete target;
- return 0;
- }
-
Called SpecificRequest()
- #include <iostream>
- #include <string>
- #include <ctype.h>
- using namespace std;
-
- // The 'Target' class
- class Compound {
- public:
- // constructor
- Compound(const string &chemical) {
- _chemical = chemical;
- }
- virtual void Display() {
- cout << endl << "Compound: " << _chemical << " ------ " << endl;
- }
- protected:
- string _chemical;
- float _boilingPoint;
- float _meltingPoint;
- double _molecularWeight;
- string _molecularFormula;
- };
-
- // The 'Adaptee' class
- class ChemicalDatabank {
- public:
- // 'legacy API'
- float GetCriticalPoint(string compound, const string point) {
- ToLower(compound);
- // Melting Point
- if (point == "M") {
- if (compound == "water") {
- return 0.0f;
- }
- else if (compound == "benzene") {
- return 5.5f;
- }
- else if (compound == "ethanol") {
- return -114.1f;
- }
- else {
- return 0.0f;
- }
- }
- // Biling Point
- else {
- if (compound == "water") {
- return 100.0f;
- }
- else if (compound == "benzene") {
- return 80.1f;
- }
- else if (compound == "ethanol") {
- return 78.3f;
- }
- else {
- return 0.0f;
- }
- }
- }
- string GetMolecularStructure(string compound) {
- string ret;
- ToLower(compound);
- if (compound == "water") {
- ret = "H2O";
- }
- else if (compound == "benzene") {
- ret = "C6H6";
- }
- else if (compound == "ethanol") {
- ret = "C2H5OH";
- }
- return ret;
- }
- double GetMolecularWeight(string compound) {
- ToLower(compound);
- if (compound == "water") {
- return 18.015;
- }
- else if (compound == "benzene") {
- return 78.1134;
- }
- else if (compound == "ethanol") {
- return 46.0688;
- }
- else {
- return 0.0;
- }
- }
- private:
- void ToLower(string &str) {
- string::iterator it;
- for (it=str.begin(); it!=str.end(); ++it) {
- *it = tolower(*it);
- }
- }
- };
-
- // The 'Adapter' class
- class RichCompound : public Compound {
- public:
- // constructor
- RichCompound(const string &name) : Compound(name) {
- _bank = NULL;
- }
- virtual ~RichCompound() {
- if (_bank != NULL) delete _bank;
- }
- virtual void Display() {
- if (_bank == NULL) _bank = new ChemicalDatabank();
-
- _boilingPoint = _bank->GetCriticalPoint(_chemical, "B");
- _meltingPoint = _bank->GetCriticalPoint(_chemical, "M");
- _molecularWeight = _bank->GetMolecularWeight(_chemical);
- _molecularFormula = _bank->GetMolecularStructure(_chemical);
-
- Compound::Display();
- cout << " Formula: " << _molecularFormula << endl;
- cout << " Weight : " << _molecularWeight << endl;
- cout << " Melting Pt: " << _meltingPoint << endl;
- cout << " Boiling Pt: " << _boilingPoint << endl;
- }
- private:
- ChemicalDatabank *_bank;
- };
-
- // client
- int main() {
- // Non-adapted chemical compound
- Compound *unknown = new Compound("Unknown");
- unknown->Display();
- // Adapted chemical compounds
- Compound *water = new RichCompound("Water");
- water->Display();
- Compound *benzene = new RichCompound("Benzene");
- benzene->Display();
- Compound *ethanol = new RichCompound("Ethanol");
- ethanol->Display();
-
- delete unknown;
- delete water;
- delete benzene;
- delete ethanol;
- return 0;
- }
-
Compound: Unknown ------
Compound: Water ------
Formula: H2O
Weight : 18.015
Melting Pt: 0
Boiling Pt: 100
Compound: Benzene ------
Formula: C6H6
Weight : 78.1134
Melting Pt: 5.5
Boiling Pt: 80.1
Compound: Ethanol ------
Formula: C2H5OH
Weight : 46.0688
Melting Pt: -114.1
Boiling Pt: 78.3