stijn0713 Posted February 7, 2013 Share Posted February 7, 2013 (edited) 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 February 7, 2013 by stijn0713 Quote Link to comment https://forums.phpfreaks.com/topic/274173-factory-method-abstract-factory-pattern/ Share on other sites More sharing options...
Hall of Famer Posted February 7, 2013 Share Posted February 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274173-factory-method-abstract-factory-pattern/#findComment-1410877 Share on other sites More sharing options...
Christian F. Posted February 7, 2013 Share Posted February 7, 2013 (edited) 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 February 7, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/274173-factory-method-abstract-factory-pattern/#findComment-1410885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.