Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Code Block
class StaffMemberUsingMOP {
    private delegate
    private salary
    StaffMemberUsingMOP(name, age, salary) {
        delegate = new Person(name, age)
        this.salary = salary
    def invokeMethod(String name, args) {
        delegate.invokeMethod(name, args)
    def describe() {
        delegate.describe() + " and has a salary of $salary"

This The result in this case looks like we didn't save much code at all. If however, there were lots of methods from the delegate that we needed to use, this one wouldn't grow in size, whereas the previous approach would become larger. Also, this version will require less maintenance as we don't need to explicitly change it if the Person class changes over time. Note that because the describe method didn't follow the boiler-plate approach of simple delegation (because it needed extra logic), we are still required to implement that method manually.