principlecppMinor
Adapter design pattern
Viewed 0 times
adapterpatterndesign
Problem
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
#include
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;
}Solution
Minor things:
this.
-
For the inline functions, keep it on one line. It's easier to read.
-
I would suggest, anything over two lines, define outside of class.
Particularly the
- Don't use
using namespace std. Read
this.
- When you are naming variables, keep it consistent. Either stick with
camelCase or underscore.-
For the inline functions, keep it on one line. It's easier to read.
void setName(string n){ name = n; }
string getName() { return name; }-
I would suggest, anything over two lines, define outside of class.
Particularly the
AcetoAcmeAdapter constructor.Code Snippets
void setName(string n){ name = n; }
string getName() { return name; }Context
StackExchange Code Review Q#65153, answer score: 2
Revisions (0)
No revisions yet.