Deep Polymorphism in Perl

23rdSep. × ’08

While writing some code for work and exploring our class layout, I’ve already implemented some polymorphism.  For instance, if you pull up an object for a service, it goes ahead and gives you the appropriate class for that service.  All you have to do is instantiate the service object, you don’t need to care about the class.  This is very elementary stuff, and works very well.

However, I’d like to take that a step further and go ahead and do multiple levels of polymorphism.  We also currently have polymorphism on our DomainService objects, which define types of services provided with a domain (mail, dns, http, https, jabber, etc).  As you may have just noticed, there’s already a place for some deeper polymorphism there, as the http and https domain service types will generally share quite a lot of code.  We’re currently using a method similar to this article on perlmonks to achieve our one-level of polymorphism, but I’m not so sure I want to have to hack up the base class every time I want to add another level of polymorphism (not saying that it will happen often, but it shouldn’t be necessary).

I’m currently investigating options, but if anyone has done this, I would love to see some examples!  And of course, I will share examples if I figure it out on my own :D

Update: Well, I feel a bit silly for making this post when the solution was so easy.  The problem was more that I was thinking about the wrong approach.

Currently we have some classes like this:

  • DomainService
  • DomainService::Http
  • DomainService::Https
  • DomainService::Dns

We instantiate DomainService and it polymorphs into the lower types.  Currently everything is up in the Domain object and the polymorph modules are empty stubs, but with refactoring this will change.  However, I didn’t want to duplicate code between the Http and Https types, considering they’re almost entirely identical, so I was thinking of having DomainService polymorph into DomainService::Http which would polymorph into DomainService::Http::Http and DomainService::Http::Https.  However, I came to my senses and realized that’s not the correct approach.  The correct approach is to do something like this:

  • DomainService
  • DomainService::HttpBase
  • DomainService::Http
  • DomainService::Https

Where DomainService::Http and DomainService::Https are subclasses of DomainService::HttpBase which is itself a subclass of DomainService.  So, Domain polymorphs into DomainService::Http and magically inherits the DomainService::HttpBase class.  Duh.

Leaving this here for posterity if nothing else :)

This entry was posted in perl and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Jess
    Posted September 29, 2008 at 6:36 pm | Permalink

    Facinating…I didn’t understand any of that, but I did want to make sure that you made it home in one piece on Saturday night.

  2. Jess
    Posted September 29, 2008 at 6:38 pm | Permalink

    Facinating…just wanted to make sure you made it home in one piece saturday night.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>