From wikipedia:

Servant is used for providing some behavior to a group of classes. Instead of defining that behavior in each class - or when we cannot factor out this behavior in the common parent class - it is defined once in the Servant.

Please take a look at MoveServant and Movable example in the wiki page. What can be the reason so that I will not want to put moveBy and moveTo in the parent class for all shapes? In other words why parent class implementing the functionality is worse compared with servant implementing the functionality.

BTW in this sentence "Servant is used for providing some behavior to a group of classes." group of classes means classes that belong the the same type (class hierarchy)?

  • The obvious reason for not putting them in the parent class is because then you have a parent class and are using inheritance, which is a bad place to end up. – David Arno Oct 10 '15 at 13:05

"Servant is used for providing some behavior to a group of classes." group of classes means classes that belong the the same type (class hierarchy)?

Group of classes may or may not be in the same inheritance hierarchy.

Notice that in the WikiPedia example Movable is an interface rather than a class, and there is no parent class for shapes. In a more complex situation, classes can implement the interface on interest, while residing in different inheritance hierarchies. In such case, there is no common parent that would hold the common methods.

enter image description here

Of course, it's possible to create a new parent class and unite the hierarchies. It's better to avoid deep class hierarchies, though.

p.s. If it were C#, I would consider MoverServant to carry extension methods.

  • As an aside, what about Java's default implementations? – Deduplicator Oct 10 '15 at 8:17
  • @Deduplicator Java's default implementations could do the trick (afaiu). C++'s multiple inheritance could to it too. At the same time, extension methods, Java's default implementation, multiple inheritance don't have the ability to choose the servant on the fly in case there are different variants of the servant. – Nick Alexeev Oct 10 '15 at 8:26
  • When they implement Movable interface that means they inherit from Movable which means they are from the same hierarchy. :) – Narek Oct 10 '15 at 12:57
  • 3
    @Narek, no an interface is a contract; not a parent class. Classes implement an interface; they do not inherit from it. Of course this doesn't apply to Java 8, which turned interfaces into multi-inheritance abstract classes with the introduction of default implementations. That is an edge case though. – David Arno Oct 10 '15 at 13:03
  • @DavidArno in C++ you don't have an interface at all. All are classes and all implementations are via inheritance. So it is just a matter of a term you use. – Narek Oct 10 '15 at 21:21

I also can't see any advantages from provided simple example. But consider next possibilities 1. Possibility to change/switch behavior in runtime by providing other Servant. 2. Encapsulation. If you don't want to expose new method in your class hierarchy. 3. There is no common parent class or you don't want to implement the method for all children? This is debatable case. Because you should cast your object to interface so it's not a good idea to have such design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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