I was working on implementing an adapter design pattern to utilize the existing classes. My problem is, the classes works almost same, but have different names and less functionalities on another.
For example, each Work class and Homework class has one function that does the same, which is doWork() and doHomework().
I can link these to doThis() in Task interface. But, Work class doesn't have done() function while Homework class has it. How would I take care of this? Just no implementation? Are there any better approach?
class Task {
public:
virtual int doThis() = 0;
virtual bool done() = 0;
};
class Work {
public:
Work();
int doWork();
};
class Homework {
public:
Homework();
int doHomework();
bool done();
bool isDone;
};
class WorkAdapter : public Task, private Work {
public:
WorkAdapter();
int doThis() {
return doWork();
}
virtual bool done() {
// Is this okay not to implment this?
}
};
class HomeworkAdapter : public Task, private Homework {
public:
HomeworkAdapter();
int doThis() {
return doWork();
}
virtual bool done() {
return isDone;
}
};
int main() {
Task *homework = new HomeworkAdapter();
Task *work = new WorkAdapter();
homework->doThis();
bool isHomeworkDone = homework->done();
work->doThis();
bool isWorkDone = work->done(); // This would never be called in my implementation...
}
Aucun commentaire:
Enregistrer un commentaire