SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 41 of 41
  1. #26
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dreamscape View Post
    I completely disagree. And so does Fowler
    What we keep calling singleton he calls the registry singleton.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  2. #27
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The New Guy View Post
    Strictly speaking yes. But, I like to think of it as such because it is trying to solve the same problem of dependency and even when you pass it around it can become as spread out as a natural global. Treating it as a global I tend to be less liberal with it.
    You miss the point. The difference between a global and a local variable is not the value they contain, but the way in which the value is addressed. I'm not saying that global values are a problem - only that global variables are.

    Quote Originally Posted by dreamscape View Post
    I completely disagree. And so does Fowler, at least in POEAA where he states he prefers to access his registry through static methods.
    It would appear that we don't use the word in the same meaning. What term would you use to describe a registry which is passed around, rather than accessed through static/global means?

    Quote Originally Posted by Dr Livingston View Post
    To me it would be better just to encapsulate the parameters in question, within a container and leave it that...
    That's a registry in my vocabulary.

  3. #28
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    virginia
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whats the verdict on making your registry class a singleton so you dont have to pass it around as a parameter? Does it negate the benefits of using a registry over a singleton?

  4. #29
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > That's a registry in my vocabulary.

    For the most part, I suppose both could be seen as the same thing? Personally, I make a (slight) distinction between the two...

    In the case of Registry, it's application wide which you distribute freely, whereas the container I was talking about is layer centric. Using a Registry in a lot of cases to simply contain a parameter for the sake of distributing it to a lower layer causes bloat towards the Registry, more so when you realise that the contained parameter isn't disposed of properly afterwards...

    With the container you can dispose of it in a regulated manner, regardless of what is contained within; Typically there is nothing vital contained within anyways, unlike your Registry, for example?

  5. #30
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    virginia
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joflow View Post
    whats the verdict on making your registry class a singleton so you dont have to pass it around as a parameter? Does it negate the benefits of using a registry over a singleton?
    Nevermind this. I think I've settled on using a ServiceLocator for these sorts of things. But before I commit to it..it is considered good practice right?

  6. #31
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A Registry is a container which holds instances of the more common objects that you'll need through out your application, pre defined at the beginning (think bootstrap file). On the other hand, a Service Locator doesn't actually contain those instances, until you require them;

    So the Service Locator has to have knowledge on how to create those object instances it's self; Common practices are to either use Dependency Injection, or Factories; Once you have the object instance created, you store it in the Service Locator so you don't need to go through the creation process a second time, if called upon.

    In answer to your question, depends on the complexity of your application. For most situations, you can get away with the Registry. If you want to use a Service Locator at a later date, be sure that the Service Locator has the same type as the Registry.

    The benifits of the Service Locator should be obvious therefore, but are there any architectural differences between the two, from the point of view of the application? No, in my view

  7. #32
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    virginia
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    A Registry is a container which holds instances of the more common objects that you'll need through out your application, pre defined at the beginning (think bootstrap file). On the other hand, a Service Locator doesn't actually contain those instances, until you require them;

    So the Service Locator has to have knowledge on how to create those object instances it's self; Common practices are to either use Dependency Injection, or Factories; Once you have the object instance created, you store it in the Service Locator so you don't need to go through the creation process a second time, if called upon.

    In answer to your question, depends on the complexity of your application. For most situations, you can get away with the Registry. If you want to use a Service Locator at a later date, be sure that the Service Locator has the same type as the Registry.

    The benifits of the Service Locator should be obvious therefore, but are there any architectural differences between the two, from the point of view of the application? No, in my view

    I've decided to use the servicelocator with factory methods. Seems to combine the benefit of lazy instantiation (if I'm using that term in the right context) and single-instance enforcement of the singleton with the non-global nature of the registry. My question is..the private array that holds data/objects in my ServiceLocator is static so that I dont have to pass the ServiceLocator object around as an parameter. Does this make my ServiceLocator a pseudo-global like a Singleton?

  8. #33
    SitePoint Member
    Join Date
    Jul 2005
    Location
    Montreal
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    joflow,

    To answer your question, Yes.

    You won't have a choice but to create a singleton at some point, whether it be a ServiceLocator or an Application or a Configuration class. There is nothing wrong with singletons as many have pointed out. In fact, Config objects are good practice in my opinion.

    There is however one drawback of singletons... it's not easy to subclass them. That is why they should remain basic and well defined.

  9. #34
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dimitry_z View Post
    There is however one drawback of singletons... it's not easy to subclass them. That is why they should remain basic and well defined.
    Easier than you'd perhaps think.

    PHP Code:
    class Singleton {
       protected static 
    $instance;
       function 
    getInstance() {
          if (
    null === self::$instance) {
             
    self::$instance = new self;
          }
          return 
    self::$instance;
       }
    }

    class 
    FooSingleton {
       function 
    getInstance() {
          if (
    null === self::$instance) {
             
    self::$instance = new self;
          }
          return 
    self::$instance;
       }
    }

    FooSingleton::getInstance() === Singleton::getInstance(); 

  10. #35
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    In the case of Registry, it's application wide which you distribute freely, whereas the container I was talking about is layer centric.
    So when you use the word "container" and I use the word "registry", we mean the same thing. And when you use the word "registry" and I use the word "static registry", we mean the same thing.

    I can accept that it's me who have the terminology mixed up - most people seem to use registry in your sense - but then what word should be used to describe what I mean? "container" just seems a bit too vague.

    Quote Originally Posted by Dr Livingston View Post
    With the container you can dispose of it in a regulated manner, regardless of what is contained within; Typically there is nothing vital contained within anyways, unlike your Registry, for example?
    I don't understand that.

  11. #36
    SitePoint Addict rvdavid's Avatar
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can accept that it's me who have the terminology mixed up - most people seem to use registry in your sense - but then what word should be used to describe what I mean? "container" just seems a bit too vague.
    It's because Fowler defines Registry as such. http://www.martinfowler.com/eaaCatalog/registry.html

  12. #37
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The term container from the domain point of view, isn't a good description or term I know, but that's how I look upon it. It's just that; A container?

    Nothing particularly special about it really...

    > I don't understand that.

    Well, the container being what it is, can be treated uniformedly in a manner that it can be seen as just another container, and treated as such? You pretty much can abuse it in any way you see fit, where as you couldn't do likewise with a Registry... You wouldn't want to in any case.

    Kyber, maybe you could clarify what you don't understand?

  13. #38
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I can'y find why global variables in PHP have to be considered problematic. And mainly when they are always used. Like for the example the database initialization. Also they are fast and are straightforward to use.
    The only problem I see, is that eventually someone can replace them with another value by mistake (if a second coder is involved ???), which always is found pretty fast.

  14. #39
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA View Post
    Actually I can'y find why global variables in PHP have to be considered problematic.
    Time to do some more reading
    http://c2.com/cgi/wiki?GlobalVariablesConsideredHarmful
    http://c2.com/cgi/wiki?GlobalVariablesAreBad
    http://en.wikipedia.org/wiki/Global_variables
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  15. #40
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very good URLS.
    Here is what I actually meant:
    Why the Convenience of Global Variables Sometimes Outweighs the Potential Problems
    • When global variables represent facilities that truly are available throughout the program, their use simplifies the code. (Even in these cases, it's wise to consider using the SingletonPattern to control access to this facility; if this facility ever becomes non-global it will make such changes significantly easier.)
    • Some programming languages provide no support or minimal support for non-global variables.
    • In a very small program, using globals can be the simplest thing that works. (But beware that small programs have a tendency to turn into large programs.)
    • Some people jump through very complicated hoops to avoid using globals. Many uses of the SingletonPattern are just thinly veiled globals. If something really should be a global, make it a global. Don't do something complicated because you might need it someday. If a global variable exists, I would assume that it is used. If it is used, there are methods associated with it. Colocate those methods in a single class and one has created a singleton. It really is better to specify all of the rules for use of a global variable in one place where they can be reviewed for consistency. The veil may be thin, but it is valuable.

  16. #41
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston View Post
    Kyber, maybe you could clarify what you don't understand?
    I'll try.

    Quote Originally Posted by Dr Livingston View Post
    With the container you can dispose of it in a regulated manner, regardless of what is contained within;
    Is this in contrast to a global variable? If so, then yes I agree, which is exactly why I propose that the registry is passed in as an object, rather than accessed through global means.

    Quote Originally Posted by Dr Livingston View Post
    Typically there is nothing vital contained within anyways (...)
    Vital by what measure? The global scope?

    Quote Originally Posted by Dr Livingston View Post
    (...) unlike your Registry, for example?
    But it appears that my "registry" == your "container". That would make your above comparison self-contradictory?

    As already mentioned, I think we have a basic misunderstanding, because I have used the word "registry" in a broader sense than you do. We either need to agree that "registry" can encompass both a global and a local registry, or we need a new word to describe the latter. I don't like "container".

    Quote Originally Posted by Dr Livingston View Post
    The term container from the domain point of view, isn't a good description or term I know, but that's how I look upon it. It's just that; A container?
    The problem is that "container" is such a generic term that it fits about any object. I suppose that "parameter object" could be used, but I still think that's a bit too generalized. I'm simply suggesting to use a registry/service locator, which is passed-in, rather than accessed through global means. Is this such a unique pattern that there are no names to describe it?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •