Name that pattern or pattern(s)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Name that pattern or pattern(s)

Post by alex.barylski »

I asked this quesiton on several other forums, but recevied little in response:
You know that helper macro in MFC (pardon my ignorance but I haven't used MFC in probalby 6-7 years) that is used for creating object at runtime, used in Views, I think it's RUNTIME_CLASS or something???
What is the name of the closest pattern for that implementation, obviously it's not a singleton and it's not quite a factory or service locator either, owhat is it?

I am implementing (in another language) a object which does:

1. Prevents direct instantiation by virtue of a private constructor
2. The second object which is used to create the said oibject manages the instances to limit only one per class

So I get the benefits of a singleton while still honoring inversion of control or DIP. Without the global introduced using a singleton.

My best research tells me it`s Multiton - which it is not due to the fact it the second object is instantiated and acts like a intelligent service locator, only creating the object if it`s not already created, caching it internally otherwise. You would need to pass this locator into dependents, there is no static to give access to named objects.

Cheers,
Alex
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Name that pattern or pattern(s)

Post by Jenk »

Singleton Factory, or Singleton Registry.

Code: Select all

class SingletonFactory {
  public $objects = array();
  public getInstance($className) {
    if (!isset($this->objects[$className]) {
      $this->objects[$className] = new $className();
    }
    return $this->objects[$className];
  }
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Name that pattern or pattern(s)

Post by alex.barylski »

Hey Jenk, I thought this too initially but those pattern(s) will not meet my requirements.

Objects which are requested must not ever be created normally. Hence the private ctor(). They must not have any getInstance() methods either, so they cannot be instantiated by any client developer. The only way they should ever be instantiated, is through a central commanding object, which would create the object and keep an instance of it internally to limit all future access to:

1. Single instances of requested object
2. Single point of access, whether a singleton or not.

I have searched for "Dictator" or "Commander" and found one reference of such a pattern, but neither really described this patterns intent.

The idea is, to provide a single point of access to any requested object, much like a registry, but prevent any object from ever being instantiated more than normally or ever -- statically, similar to a singleton.

I'm going to have to come up with my own name here I think, but I'm at a loss for ideas. Maybe Executive or perhaps I go with "Dictator". :P

Cheers,
Alex
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Name that pattern or pattern(s)

Post by Jenk »

Or better yet, just don't use a private constructor. Clearly it is causing a hindrance, so make it public. If someone wishes to use a class/object outside of it's intent, let them.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Name that pattern or pattern(s)

Post by alex.barylski »

Jenk: Thank you. After some thought and consideration, I have concluded that you are right and a singleton and abstract factory is all that is needed. Thank you and arborint both, for talking sense into my designs so many times. :)

Cheers,
Alex
Post Reply