Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a design pattern to replace huuuge if on object Type

OK so I'm looking a some code which looks roughly like this:

void DoSomething(object o)
{
    if (o is Sometype1) { 
    //cast o to Sometype and do something to it
    }
    else if (o is Sometype2) {
    //cast o to Sometype2 and do something to it
    }
    ...
    else if (o is SometypeN) {
    //cast o to SometypeN and do something to it
    }
}

Now one approach would be to make all the objects o that are used as parameters implement an interface like

interface ICanHaveSomethingDoneToMe
{
    //expose various properties that the DoSomething method wants to access
}

But the problem with this is that I don't want all my objects to implement this interface - the logic for the do something method doesn't really belong with them. What pattern should I be using to deal with this?

I suspect something like a series of implementations of

interface IPropertiesForDoingSomethingTo<T>
{
    //expose various properties that the DoSomething method wants to access
}

might be better. I'd have an implementation for each of the object types that I want to do it to, but then I have this new problem. I would at point need to have a method like

IPropertiesForDoingSomethingTo<T> GetPropsGeneric(T t);

but is this going to need to have a massive switch on it? Should I define a class with loads of methods like

IPropertiesForDoingSomethingTo<Someobject1> GetProps(Someobject1 t);
...
IPropertiesForDoingSomethingTo<Someobject1> GetProps(SomeobjectN t);

This has the problem compared to the generic version that you wouldn't be able to add new types at runtime. Ts there something cunning one can do with a DI container in GetPropsGeneric to resolve the container? Thanks!

like image 895
mcintyre321 Avatar asked Nov 20 '08 17:11

mcintyre321


People also ask

How would you replace factory design pattern in functional programming?

In fact, a large number of OO design patterns were created due to the lack of ability to pass functions around. Most of these can be replaced simply by passing in a function. A short list of them include Command, Factory, and Strategy. Many others can remove a lot of class hierarchy if they accept functions.


1 Answers

any time you see a switch statement (or a series of if-statements) that are checking the type of an object, this is a Big Red Flag for a missing base class or interface. In other words, the code should be relying on polymorphism, not testing the object type

if you cannot change the base class or implement an interface, you're probably left with a dictionary to simulate dynamic dispatching. In C# you could use an anonymous delegate for the method that included the cast

as for the property access, if the properties don't conform and accessing via reflection is not an option, you may need to extract the property values in the method/delegate above and pass them to a generic function instead

like image 195
Steven A. Lowe Avatar answered Nov 15 '22 21:11

Steven A. Lowe