8
\$\begingroup\$

I am referring to the book Design Patterns For Dummies to learn about design patterns. I wrote an adapter design pattern, as mentioned in the book. Is it proper or does it need some modification?

#include<iostream>
#include<sstream>

using namespace std;

class AceInterface
{
public:
    virtual void setName(string n) = 0;
    virtual string getName() = 0;
};

class Aceclass : public AceInterface
{ 
    string name;
public:
    void setName(string n){
        name = n;
    }
    string getName() {
        return name;
    }
};

class AcmeInterface
{
public:
    virtual void setFirstName(string fn) = 0;
    virtual void setLastName(string ln) = 0;
    virtual string getFirstName() = 0;
    virtual string getLastName() = 0;
};

class Acmeclass :public AcmeInterface
{
    string First_name;
    string Last_name;
public:
    void setFirstName(string fn) {
        First_name = fn;
    }
    void setLastName(string ln) {
        Last_name = ln;
    }
    string getFirstName(){
        return First_name;
    }
    string getLastName(){
        return Last_name;
    }
};

class AcetoAcmeAdapter:public AcmeInterface
{
    Aceclass aceObject;
    string First_name;
    string Last_name;
public:
    AcetoAcmeAdapter(Aceclass a) {
        aceObject = a;
        istringstream iss(aceObject.getName());
        iss >> First_name ;
        iss >> Last_name;
    }
    void setFirstName(string fn) {
        First_name = fn;
    }
    void setLastName(string ln) {
        Last_name = ln;
    }
    string getFirstName(){
        return First_name;
    }
    string getLastName(){
        return Last_name;
    }
};

int main()
{
    Aceclass aceobj;

    aceobj.setName("Mohandas Gandhi");

    cout << "name from aceobj :" << aceobj.getName() <<endl;

    AcetoAcmeAdapter ace2acme(aceobj);

    cout << "First_name from ace2acme :" << ace2acme.getFirstName() << endl;
    cout << "Last_name from ace2acme :" << ace2acme.getLastName() << endl;

    return 0;
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Minor things:

  1. Don't use using namespace std. Read this.
  2. When you are naming variables, keep it consistent. Either stick with camelCase or underscore.
  3. For the inline functions, keep it on one line. It's easier to read.

    void setName(string n){ name = n; }
    string getName() { return name; }
    
  4. I would suggest, anything over two lines, define outside of class. Particularly the AcetoAcmeAdapter constructor.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you suggestions. from design pattern point of view is this correct way to implement ? because AcetoAcmeAdapter redefines functions of the Acmeclass, i am bit confused there. \$\endgroup\$
    – ART
    Oct 9, 2014 at 4:32
  • 4
    \$\begingroup\$ Well, the functions of AcmeInterface are pure virtual methods. When you put these into a class, you do two things. 1. Don't make an object of that class. 2. Override those pure virtual functions. I think from a design pattern POV, what you have is correct. \$\endgroup\$ Oct 9, 2014 at 4:42
  • \$\begingroup\$ The advice to not use using namespace std; is not helpful if a person is creating a single-file executable: it is perfectly fine for this. \$\endgroup\$ Nov 5, 2021 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.