1

Consider this example of polymorphism. I have two different API, IWrite and IRead, and then a single implementation of these.

interface IRead
{
    Entity Find(int id);
}

interface IWrite
{
   void Persist(Entity);
}

class SomeRDBMSRepository : IRead, IWrite
{
   public void Persist(Entity entity)
   {
      ...persist it
   }

   public Entity Find(int id)
   {
      ...return an entity
   }
}

Because the implementation has all the details of how to implement the interfaces, then we could easily use the same type, SomeRDBMSRepository. We could have several clients of the APIs, maybe like these:

 class FindUserQueryHandler
    {
        public FindUserQueryHandler(IRead readEntities)
        {
        }

        public User Handle(int id)
        {
            return _readEntities.Find(id);
        }
    }
class RegisterUserCommandHandler
{
    public RegisterUserCommandHandler(IWrite writeEntities)
    {
    }

    public void Handle(User user)
    {
        _writeEntities.Persist(user);
    }
}

When we want to stress the idea of polymorphism, then we simply create new types that provide different functionality. We give this example when we have, let's say, a requirement that the users must be queried from a different repository, then we could create a new type, SomeNoSQLRepository : IRead and in this way FindUserQueryHandler can use an instance of this implementation without caring about its actual details, as long as the type implements the IRead API. Also FindUserQueryHandler doesn't have to do anything special (ie to change)

Someone asked me a while ago for an example of polymorphism (cough / interview) and I gave him the example of SomeRDBMSRepository because I think this type has different roles or responsibilities for different clients. For a query type is the reader, while for the command is the writer. So my reason was this type is poly because it has the IRead form and the IWrite form in the same time. It seems I was wrong.

While reading the polymorphism definition from Wikipedia, I could adapt it to the meaning that if we can use any type in the place where some API is expected, as long as that type implements the clearly defined API, then that code is polymorphic. The mechanics don't really mater. C# or Java requires to specify that a class implements an interface, but for a language like Go there is enough to be a match of the method signatures between the type and interface.

Wikipedia also says that a polymorphic type is a type that has operations that can accept objects of different types. In my example the query and command types are polymorphic types by this definition.

Basically my question is: is there a taxonomy for SomeRDBMSRepository? How wrong I was?

  • God object? – gnat Jan 22 '17 at 18:19
  • 1
    @gardenhead has a good answer. Also see en.wikipedia.org/wiki/Subtyping. Polymorphism in OOP is often referred to as inclusion polymorphism in broader contexts, because there are also other kinds of polymorphism. – Erik Eidt Jan 23 '17 at 6:28
  • 2
    What you have there is often referred to as multiple inheritance (multiple interface inheritance in Java and C# as they don't have full multiple inheritance, I have not seem many languages that do full multiple inheritance properly). Polymorphism usually refers to multiple implementations inheriting from a single interface, to that one implementation can stand in for another. – ctrl-alt-delor Jan 23 '17 at 23:17
  • That's what my interviewer said about inheritance, but I didn't agree much with him, because what is missing here is the behaviour/implementation inheritance, unlike C++. I reviewed the Gamma et. all introductory chapter of their book and I believe the whole point is, like you said, the ability to substitute an object to another, so the clients don't depend on a particular object, but on interfaces. Interface in Gamma is different from the interface from C# or Java. Gamma considers the object's interface the totatity of its public methods. A type is a name to denote a particular interface. – Adrian Iftode Jan 24 '17 at 7:27
3

First of all, your definition is wrong. You are using the term type incorrectly. Here is your definition

polymorphism is the ability of different types to share the same interface

Types don't have interfaces; interfaces are types. Polymorphism is in fact the ability of expressions to take on multiple types. In OOP, the expressions are objects and the types are interfaces.

The example you gave is correct. An object of class SomeRDBMSRepository actually has three types: SomeRDBMSRepository, IRead, and IWrite, so it is polymorphic (trimorphic?). Your interviewer was wrong in this instance, which unfortunately happens quite often in tech interviews.

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.