1 / 11

Factory Method

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

mervyn
Télécharger la présentation

Factory Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factory Method By Judith Mziray And Jerry Cipolla

  2. 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 ??

  3. 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.

  4. 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

  5. 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.

  6. 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.

  7. 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.

  8. Object and Instantiation • Gun (object) -- shoot (instantiation) • Grenade (object) - pull the pin (instantiation) • Bazooka (object) – shoot (instantiation)

  9. 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.

  10. Structure instantiation class Intelligence & Politicians Pull pin Grenade Special forces (YOU) Sub-class object

  11. Questions ?????

More Related