I don't like singletons, but I have to compromise now (but only as temporary measure).

However I don't want to go completely singleton. I'd rather want to use this pattern:

interface NameThisInterface {

  //Returns the currently centralized object.
  static function current();

  //Centralizes the instance.
  function centralize();

}


class A implements NameThisInterface {
   ...
}


$obj = new A;


$obj->centralize();

...


A::current(); //gets $obj.

Basically A would be my "Application" class. All controllers, models and views etc will access need only one instance of it. But for the sake of easier unit-testing among other reasons I want to have the application object stored as a reference to all MVC classes. So with the pattern above, the conversion at this later point in time will be easier and some of the unit testing might be easier.

Is there a generally accepted name for this pattern?

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top