Jump to content

factory method& abstract factory pattern


stijn0713

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.