SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member threed's Avatar
    Join Date
    Mar 2004
    Location
    Bulgaria
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dependencies design question

    Hi guys,

    Lets say we have class Form, each form consist of Elements, they consist of Filters and so on.

    All of them are using single instance of Registry class.


    Can you please tell me if there is proper design pattern helping the access to this registry object from the Form and its children classes.

    Or i have to call setRegistry($registry) for each object.

    Thanks in advance !

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    there are several patterns to deal with this.

    Singleton is the simplest but there are many reasons to avoid it... just google it or search these forums.

    You can make your registry static, though this isn't without its problems.

    The most complete solution is Dependency Injection, but also has some limitations. See the link in my sig (i also provide another alternative).


    Really it depends on the needs of your application as to which is best. In simple applications the singleton pattern allows very fast development and and easy interface, but for larger, continually developed projects it brings several issue.

  3. #3
    SitePoint Member threed's Avatar
    Join Date
    Mar 2004
    Location
    Bulgaria
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Form is quite complex. Also they can be many instances of the Form in the same time, each with their own registry. So i guess Singleton will not be very good idea ?

    I will read more Dependency Injection now. Thanks

  4. #4
    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 threed View Post
    Or i have to call setRegistry($registry) for each object.
    Is this a problem though? Have you tried it?

  5. #5
    SitePoint Member threed's Avatar
    Join Date
    Mar 2004
    Location
    Bulgaria
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    No is not a problem at all. I just post quite simple example and i am wondering if there is more clever solutions than passing the object everytime.

    But i guess i am gonna finish like that

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This article gives a excellent overview as to what your options are: http://martinfowler.com/articles/injection.html

    Personally I favour DI all the time, although I'm not sure if I would want my component coupled to the registry object, even if abstract...

    Whatever variables are required by the form and it's sub-components I would pass those variables in directly, via it's API.

    Cheers,
    Alex
    The only constant in software is change itself

  7. #7
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really sure why there's a need for the registry pattern. It seem some what unnecessary.

    PHP Code:
    class Form
    {
        protected 
    $_elements = array();
        public function 
    addElement(Element $element)
        {
            
    $this->_elements[$element->getName()] = $element;
        }
        
        public function 
    getElement($name)
        {
            return 
    $this->_elements[$name];
        }
        
        public function 
    getElements()
        {
            return 
    $this->_elements;
        }
    }

    class 
    Element
    {
        protected 
    $_name null;
        protected 
    $_filters = array();
        public function 
    getName()
        {
            return 
    $this->_name;
        }
        
        public function 
    addFilter(Filter $filter)
        {
            
    $this->_filter[$filter->getName()] = $filter;
        }
        
        public function 
    getFilter($name)
        {
            return 
    $this->_filters[$name];
        }
        
        public function 
    getFilters()
        {
            return 
    $this->_filters;
        }
    }

    class 
    Filter
    {
        protected 
    $_name null;
        public function 
    getName()
        {
            return 
    $this->_name;
        }

    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you make a factory that creates new elements. In the factory during element creation, it assigns the newly created element to the registry? Obviously the factory would need an instance of the registry, probably passed in through it's constructor.

  9. #9
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @threed, I'd suggest just passing arguments around "manually" until (1) you
    find a pattern in there, (2) feel a need for automation or (3) see that something
    isn't DRY (Don't Repeat Yourself). Later on, you may resort to other solutions,
    like DI containers.
    I'm under construction | http://igstan.ro/


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
  •