Yes and no.
The important thing is that the 'decorator' needs to act as much as possible as the original, except for where you want it to differ.
In typed languages, this is often done by making both Book
and MyBookDecorator
implementing the same interface (commenly called IBook
. All the methods the interface declares are then implemented as follows.
class MyBookDecorator implements IBook {
construct(IBook book) {
this->book = book;
}
int methodWeDoNotWantToChange(string arg1) {
return this->book->methodWeDoNotWantToChange(arg1);
}
int getWord(int page, int line, int word) {
string word = this->book->getWord(page, line, book);
if (unsafeWords.contains(word) { return "*******"; }
return word;
}
}
In particular:
* you need to implement all methods that (I)Book
does, with the same signature.
* the constructor should take an IBook‘, and not a
Book` so that you can combine multiple decorators.
* methods you do not care about are implemented as pass-through.
* methods you do care about are often also pass-through, but with extra work before/after.
You can define extra methods, but then you couple the user to your specific decprator, which is not a good idea.