views:

86

answers:

3

Consider Martin Fowler's Patterns Of Enterprise Application Architecture, and the pattern of Front Controller: http://martinfowler.com/eaaCatalog/frontController.html Apparently, it uses the singleton pattern. Well, I have a package of classes in php application that work together (like Zend's Controller Package) and there is one class that makes them all usable and since it resembles much of Front Controller's concepts, I named it PackageName_Front. But it shouldn't be a singleton class (as opposed to Front Controller), so do I still let it have the name Front? If not, what do I name it? Since it's a quite big package, I just need it to follow conventions as much as possible (not in a dogmatic way!) so it would be readable to other developers.

More info: It's not anything related to controllers. It's just an object that works like Zend_Form (which consolidates use of all the other objects like Zend_Form_Element_X and Zend_Validate into one object) But I can't just name it PackageName. It has to be PackageName_Something, and I'm just not sire what Something should be. Maybe "Handler"?... I just wanna make sure when someone reads it's name, doesn't get confused about it's role in the whole Package :)

A: 

The idea behind the singleton pattern is to make sure there is only one instance of an object that is supposed to only exist in a single instance. The front controller falls very well into this category, so it would, perhaps, be wise to make it follow a singleton pattern.

If, however, your code will always make sure it calls the constructor only once, then there is room for your non-singleton pattern object.

My 2 cents here, since I'm not any book author or something.

Slavic
Uh, I added some more info to my question :)
Cg Alive
A: 

Just from a purely design view, it sounds like you're using that PackageName_Front as a Facade when you say:

there is one class that makes them all usable

Fowler's implementation of the pattern says:

The Front Controller consolidates all request handling by channeling requests through a single handler object

This insinuates that a Singleton might be used to implement the Front Controller class, but it certainly doesn't constrain it to use it. He doesn't explicitly mention it though.

I don't think it's important whether or not its a Singleton. Just makes sure its the sole channel for requests, and you'll have successfully used the pattern. :)

Mike
I'll update my question :)
Cg Alive
+1  A: 

Apparently, it [FrontController] uses the singleton pattern.

FrontController does not have to be implemented as Singleton. The book does not suggest anything like this. The example in the book uses a Servlet for the Handler.

Many people nowadays (including Erich Gamma of GoF fame) view the Singleton as a code smell and discourage it's use. In a shared-nothing-architecture the Singleton can only restrict instances inside the current request anyway, so the use in PHP is limited. Global access to an object can be achieved without the Singleton pattern, either through the (evil) global keyword or static methods. Global access always creates coupling. The better way would be to use Dependency Injection, which has the added benefit of providing less coupling and thus better maintainability.

so do I still let it have the name Front? If not, what do I name it? Since it's a quite big package, I just need it to follow conventions as much as possible (not in a dogmatic way!)

There is no such convention about naming classes Front classes to my knowledge. What you describe could be a Facade or a Gateway though. Also, are you sure you cannot name the class after the PackageName? After all, the Zend_Form package has a Zend_Form class, too.

Gordon
I'm not sure why I wouldn't name it PackageName! I don't know, it just doesn't sound right! I do have many other packages named after their PackageName and they do sound right! But in this case, ... I dunno :D
Cg Alive
@CgAlive well, name it after what it does. If you think Front is a good name, name it Front. Personally, I dont find this that descriptive though, but in lack of a better name, do so.
Gordon
Yeah I used Front and I think it kinda sounds right so far! It's functionality looks like a "Front"! :"D
Cg Alive