Monday, December 24, 2012

Using an Action in a Factory

We’ve all seen the Factory (or Factory Method) Pattern. The Factory Pattern is used to encapsulate the creational logic for a set of related classes, classes that implement a particular interface or inherit from the same base class.

From Wikipedia:
The factory method pattern is an object-oriented creational design pattern to implement the concept of factories and deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The essence of this pattern is to "Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.
One way to implement the pattern is to use a Enum or some other designator to specify the type of object to be created.  The factory method then uses either if statements or a switch statement to create the proper type of object or decorate it appropriately based on the setting of the designator. This works well when the creational logic is simple or the number of things created is small. Alternatively, you could have a factory method per type, but that can have the effect of moving the selection logic to the calling class which defeats the purpose.

In answering a recent StackOverflow question I ran across this issue and it made me think of alternative ways that you could handle this.  Using an Action<T> as the designator seemed to me to be a perfect solution in the case in question.  Using an Action allows you to separate the creational logic for each item into a separate method, avoiding overly long switch or set of if statements.

For example:

public static class WeaponFactory
    public static IWeapon Create<T>(Action<T> decorator) where T : IWeapon, new()
        var weapon = new T();
        return weapon;

    public static void SwordOfDestiny(Sword sword)
        sword.Damage = DamageLevel.Infinite;

    public static void NormalSword(Sword sword)

    public static void TinSword(Sword sword)
        sword.Damage = DamageLevel.Minor;

Invoked as:

var weapon = WeaponFactory.Create(WeaponFactory.SwordOfDestiny);

Using this allows you to keep the creational logic completely separated and avoid the long switch statement. I’d probably use in conjunction with a dictionary, say Dictionary<Person,Action<IWeapon>> to give me the simplicity of tying the type of the thing to be created to some other aspect/setting and yet still be able to use the decorator pattern within the factory.

Friday, December 7, 2012

Things My Dogs Teach Me

I haven’t written anything for awhile and, though after last night’s deployment I’m not sure I have the capacity to think about code, I did feel that it was time to write something. I’ve been working from home for the past several months. Funny, you would think that would give me more time, but for most of that time I’ve actually been putting more time in on the job with more concentrated effort. One thing it does do, though; it allows me to spend more time in the company of my dogs.

There are two things about my dogs that amaze me and challenge me at the same time.

First, my dogs love to chase squirrels. They live for it. Often I have to get up from my basement office (it’s a split level so I have windows, it’s not as dreary as it sounds) to go let them out for fear that they’ll burst through the back door to get at those pesky lawn rats. The fact that I’ve got a bird feeder in the back yard which is as attractive to the squirrels as it is to the chickadees and nuthatches only increases their opportunities for squirrel chasing.

My dogs love chasing squirrels as much as I love writing code…but they are horrible at it. They have never even come close to catching a squirrel (thankfully). The cat is a different story. We have a regular chipmunk/bird graveyard out back, but I digress. My point is that, despite their repeated failures they have never been tempted to give up on what they love, on the occupation that they were destined for seemingly. Their passion for squirrel chasing surpasses their regular and repeated failures at it.

How is your passion for what you do? You need to tend to your passion. Feed it, encourage it to grow, nurture it. Your passion will sustain you through inevitable times of failure. Every day that I get to write code is a good day. I’m committed to keeping that true by maintaining my passion.

Second, my dogs always know when they’ve done something wrong. They rarely need me to reprimand them. It doesn’t matter how comfortable they’ve been on the couch, when I walk into the room they hop down and slink away without me having to say a word. Now, my dogs aren’t lap dogs and they’ve been taught that furniture is for people (and small animals – I did mention the cat, right). They are clear on the rules. Sometimes they just choose to disobey them when no one is looking.

The important thing or, at least, one important thing is that they aren’t even tempted to shift the blame or deny it when they’ve done something wrong. Oh, that I had as much integrity as my dogs.

How about you? Are you willing to own up to your mistakes? I know that it is a constant challenge to me to not try and find some scapegoat to blame for my errors. Now, I’m not suggesting that we should be like my dogs in that they repeat their mistakes, but integrity demands that we take responsibility for when we mess up. Don’t excuse yourself because others are also culpable (it was the cat!). Don’t try to pretend that it wasn’t you (I see that dog hair on the couch!). Don’t blame the rules or the situation (but the toy poodle gets to get up here!).  Own up to it. Accept your responsibility, learn from it, and move on.

Those are just a couple of things I’ve been learning from my dogs. I hope they’ve been helpful to you as well.  Next time, I’ll try to have some code for you.