Jump to content


Photo

factory method& abstract factory pattern


  • Please log in to reply
2 replies to this topic

#1 stijn0713

stijn0713

    Advanced Member

  • Members
  • PipPipPip
  • 231 posts

Posted 07 February 2013 - 03:03 PM

Hello,

I have 2 questions concerning factory patterns:

1) Quite a few times ive seen that they mention 'instantiation of an object at runtime' as a benefit of the factory method.

How is this a benefit ? in my opinion is not a benefit of the factory pattern as instantiation of an object could be perfectly done at runtime without factory aswell,

class uitprinten_Factory
{
		 public static function GetWriter($type='JSON')
		  {
			    $class = 'uitprinten_' . $type;
   if(class_exists($class)) {
				  
	    return new $class();
			    }
		 
			    throw new Exception('Unsupported format');
			    }
}


$aWriter = uitprinten_Factory::GetWriter();



2) second question is about the abstract factory pattern. I read alot of different explanations on this pattern. To what i have established it works like this:

- you need an abstract factory class that defines the interface (e.g. abstract methods) for other factory classes that implement those methods and in those methods objects are constructed (products) from concrete classes. Could anybody point out if i'm correct on this interpretation? furthermore, what is the benefit of this abstract factory pattern?

thanks in advance

Edited by stijn0713, 07 February 2013 - 03:05 PM.


#2 Hall of Famer

Hall of Famer

    OOP Fanboi

  • Members
  • PipPipPip
  • 315 posts
  • LocationIthaca

Posted 07 February 2013 - 06:28 PM

Well for the first question, I am not that sure. The reason to use factory method is that it is standardized and organized, it defines a way to approach a problem. Whether you find it useful or not is really up for your own experience and interpretation. In some applications it helps, in other circumstances it can stink. The wisest way to look at it is to get the patterns to work for you, not to force yourself to use a pattern just because it makes your script look professional.

For the 2nd one, yes you are getting it almost correct. Lets consider the example of an abstract factory VehicleFactory, which have concrete subclasses such as BMW, Benz, Ford, Volvo and Toyota. Each factory is responsible of generating vehicle component objects for various types of vehicles such as getEngine(), GetSteeringWheel(), getBrake(), getTyre() and so on. These methods generate various components objects such as BMWEngine, BenzEngine, FordBrake, VolvoBrake and ToyotaTyre, you can easily manage the creation of such objects using abstract factory pattern.

Welcome to the world of OOPHP! In a perfect script, everything is an object. You cannot be perfect, but you can approach as close as can.

zog841.jpg


#3 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 07 February 2013 - 06:58 PM

The runtime concern is mostly for compiled languages, where runtime and start-up are two distinctly different things. In PHP all objects are initialized at runtime due to the nature of the language.
In a desktop application, however, having to create instances of 200+ objects, of which many may never be used, at start up might be a bit too time and resource consuming. Thus it's better to initialize most of those objects only when you need them, and rather add a few ms to the time it takes to open the associated functionality, than having 2-3 seconds (or more) and a lot of extra memory used when you start the program.

Edited by Christian F., 07 February 2013 - 06:59 PM.

Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com