Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm attempting to understand how "switch as a code smell" applies when the proper code path is determined by some observable piece of data.

My Webapp object sets an internal "host" object based on the hostname of the current request. Each Host subclass corresponds to one possible hostname and application configuration: WwwHost, ApiHost, etc. What is an OOP way for a host subclass to accept responsibility for a specific hostname, and for Webapp to get an instance of the appropriate subclass?

Currently, the hostname check and Host instantiation exists within the Webapp object. I could move the test into a static method within the Host subclasses, but I would still need to explicitly list those subclasses in Webapp unless I restructure further. It seems like any solution will require new subclasses to be added to some centralized list.

share|improve this question

3 Answers 3

For the example you describe, I don't think you're experiencing a code smell.

Typically, similar switch statements are scattered throughout a program. If you add or 
remove a clause in one switch, you often have to find and repair the others too. 

Switches are not inherently bad - which is even mentioned in the article you linked. Duplication of switch statements is bad. Ditto with if/else-if blocks. It violates DRY principles.

If you have a single spot where the switch statement invokes the correct instantiation, then it sounds like your code is OK. Take a look at the Factory Pattern for additional information about your case.

share|improve this answer

To mantain a "well written code" you should think about applying the factory pattern.

The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The essence of the Factory method 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

WikiPedia

Here's an implementation in PHP

share|improve this answer

You should simply create instances of the desired subclass of Host:

class Host
{
    private $name;
    private $type; //the name of the specific host type

    private function __construct($name, $type)
    {
        $this->name = $name;
        $this->type = $type;
    }

    public static function getInstance($name)
    {
        return new self($name, get_called_class());
    }
}

class wwwHost extends Host
{ 
    //code specific to the wwwHost class 
}

class apiHost extends Host
{ 
    //code specific to the apiHost class
}

$myWwwHost = wwwHost::getInstance('aWwwHostName');
$myapiHost = apiHost::getInstance('anApiHostName');
share|improve this answer
    
Where would you place the logic that instantiates the correct Host based on the hostname of the incoming request? –  Adam Backstrom Jul 2 '12 at 10:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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