When to use Factory method pattern?
Please provide me some specific idea when to use it in project? and how it is a better way over new keyword?
The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created.
The Singleton pattern ensures that only one instance of the class exists and typically provides a well-known, i.e., global point for accessing it. The Factory pattern defines an interface for creating objects (no limitation on how many) and usually abstracts the control of which class to instantiate.
The factory pattern aims to solve a fundamental problem in instantiation – i.e., the creation of a concrete object of a class – in object-oriented programming. In principle, creating an object directly within the class that needs or should use this object is possible, but very inflexible.
Use a factory method (not abstract factory) when you want to reuse common functionality with different components.
Example: Imagine you have an M16 rifle. Something like this:
public class M16 { private Scope scope = new StandardScope(); private SecondaryWeapon secondary = new Bayonet(); private Camouflage camo = new DesertCamo(); public double getMass() { // Add the mass of the gun to the mass of all the attachments. } public Point2D shootAtTarget(Point2D targetPosition) { // Very complicated calculation taking account of lots of variables such as // scope accuracy and gun weight. } }
You may be satisfied with it for a while, thinking that you wont want to change anything. But then you have to do a secret nightime stealth mission in the jungle, and you realise that your attachments are completely inappropriate. You really need a NightVision scope, JungleCamo and a GrenadeLauncher secondary weapon. You will have to copy past the code from your original M16......not good extensibility.....Factory Method to the rescue!
Rewrite your M16 class:
public abstract class M16 { private Scope scope = getScope(); private SecondaryWeapon secondary = getSecondaryWeapon(); private Camouflage camo = getCamouflage(); public double getMass() { // Add the mass of the gun to the mass of all the attachments. } public Point2D shootAtTarget(Point2D targetPosition) { // Very complicated calculation taking account of lots of variables such as // scope accuracy and gun weight. } // Don't have to be abstract if you want to have defaults. protected abstract Scope getScope(); protected abstract SecondaryWeapon getSecondaryWeapon(); protected abstract Camouflage getCamouflage(); } //Then, your new JungleM16 can be created with hardly any effort (and importantly, no code //copying): public class JungleM16 : M16 { public Scope getScope() { return new NightVisionScope(); } public SecondaryWeapon getSecondaryWeapon() { return new GrenadeLauncher(); } public Camouflage getCamouflage() { return new JungleCamo(); } }
Main idea? Customise and swap out composing objects while keeping common functionality.
An actually useful place to use it: You have just designed a really cool GUI, and it has a really complicated layout. It would be a real pain to have to layout everything again if you wanted to have different widgets. So.....use a factory method to create the widgets. Then, if you change your mind (or someone else want to use your class, but use different components) you can just subclass the GUI and override the factory methods.
I have two cases where I tend to use it:
Examples:
First case could be that you want to have a factory creating SqlCommand
objects, where you automatically attach a valid SqlConnection
before returning the command object.
Second case is if you have an interface defined and determine at execution time which exact implementation of the interface to use (for instance by specifying it in a configuration file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With