#1
  1. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124

    Advice on removing the overuse of the singleton pattern


    I love the singleton design pattern and have used it where I thought a single instance would be sensible.

    I have a singleton to manage database connections (creates or returns an instance of a PDO object, supports multiple connections by use of a textual key)

    I have a singleton as a registry of run-time settings

    For these two situations the pattern works perfectly for me. Then I started to over use the pattern.

    In my framework the front controller calls a static method in the desired module/action for a list of filters the module/action requires. The filters are then executed in order.

    The concept of each filter is to modify the request, or the response [of the module/action] (or both) or cut execution short (never getting to module/action, eg userr/permissions/anti-xsrf, etc).

    It's an elegant model and works well, but I have created a singleton to hold the request object and a singleton to hold the response object. Each filter works on these objects.

    This seemed to work fine until I changed the requirements of the api - the front controller should be able to handle multiple requests and each module/action should also be capable of initiating requests to other modules/actions passing it's own 'request' parameters (some in a 'sudo' type of way).

    Both of these scenarios interfere the with the singular request/response objects.

    My initial idea was to create a non-singular object for each request/response and pass this along from the front controller, through each filter and to the module/action...and back up the chain again. But this seemed a bit messy. I'm in a situation where many objects that are not in a parent-child relationship require access to the same object.....but the global scope issue of singletons is causing the usual headache.

    Does anyone know of any design patterns I could look at that would get round this?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    I'm not sure if I'm understanding you correctly but if you want to be able to pass around multiple requests you could do it like so:
    PHP Code:
    class SomeController extends Base {
      public function 
    run($Request$Response$RequestStack) {
        
    $blah $Request->getSomeData();
        
    $Response->setSomeData($blah 'hi'); (you could also just return the response instead of doing it by reference)
        
    $RequestStack->add(new Request(...));

    Then in your "front" controller or framework class you would just continue to loop over the request stack and dispatch them to the appropriate controllers... Each controller would be passed the same response object so data could be pushed into that from each one. You may have to be a little clever in your RequestStack when looping over it to flag requests as "processed" once they have been dispatched.

    It would be easier for me to visualize if I could see your code.

    Hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo