110 likes | 130 Views
Explore the concept of the Factory Method, its participants, and its application through a hypothetical scenario. Discover the pros and cons of using the Factory Method and how it can simplify object instantiation.
E N D
Factory Method By Judith Mziray And Jerry Cipolla
Overview • Big picture view of the Factory Method • Participants of the Factory Method • Case study/hypothetical • Defining or classifying the elements within the hypothetical • Pro’s and Con’s of the Factory Method • Hypothetical applied to a diagram/structure • Questions ??
Factory Method – Big Picture • Creates or manufactures objects, just like a real factory. • The factory method is used when its just not possible for a CLASS to predict what CLASS of OBJECTS will be needed and what exact INSTANTIATION or action is needed. • Class DELEGATES to a SUBCLASS which picks the appropriate object to instantiate, given the specific circumstances.
Participants • Product – defines the interface of objects the factory method creates • ConcreteProduct – implements the Product interface • Creator – declares the factory method, which returns an object of type Product • ConcreteCreator – overrides the factory method to return an instance of a ConcreteProduct
Factory Method- getting rid of the beauracracy • Hypothetical: Imagine you are a special forces soldier in a far off country. Intelligence and the politicians know something is going to happen within the operation. However, what specifically needs to happen must be decided on a split second decision. You can shoot your gun, you can shoot your bazooka, or you can throw a grenade. It just depends on what the enemy is doing, which cannot be predicted.
Hypothetical continued • Intelligence and the politicians must rely on your judgment. • Look at it this way, intelligence and the politicians are the classes, and you, the special forces soldier in the field, are the creator. Intelligence has delegated to you to pick the appropriate subclass that will instantiate the correct object. In other words, you pick the subclass to make the correct instantiation such as a gun that will shoot, a grenade and pull the pin, or a bazooka to shoot.
Hypothetical defined • Class - Politicians – delegating authority down the line • Class - Intelligence – delegating authority to you • Delegating Subclass/Creator – You, the special forces soldier, are the delegating subclass or creator that chooses which object to pick, the gun, the grenade, or the bazooka, which all will be instantiated.
Object and Instantiation • Gun (object) -- shoot (instantiation) • Grenade (object) - pull the pin (instantiation) • Bazooka (object) – shoot (instantiation)
Pro’s and Con’s -Factory Method • Lets a class defer instantiation to subclasses • Eliminates the need to bind application-specific classes into your code • Disadvantage is sub-classing may become too complex or tedious for the client in later additions to the code.
Structure instantiation class Intelligence & Politicians Pull pin Grenade Special forces (YOU) Sub-class object