Preliminary Research on Design Mode-factory method mode

Source: Internet
Author: User

Factory Method is an object creation mode. by defining an interface used to create an object, the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to its subclass. Factory methods use abstract interfaces to pass the action of creating an object to a specific subclass.

I. Application scenarios

1. When a class does not know the class of the object it must create, it cannot be instantiated directly because it does not know the class of the object to be created, it can only be done by other classes that know the class, such as the specific implementation of an interface and the subclass of a parent class.

2. When a class wants its subclass to specify the object it creates, this usually happens when the parent class does not know which class to instantiate.

3. When the class delegates the responsibility of creating an object to one of multiple help sub-classes and you want to localize the information of the proxy when the sub-classes are created.

Ii. UML diagram


Iii. Java implementation

Package study. patterns. factorymethod;/*** factory method mode: * The core factory class is no longer responsible for creating all products. * instead, it submits the specific creation work to the subclass, * act as an abstract factory role. * The role is only responsible for providing interfaces that must be implemented by a specific factory class. * The product class should be instantiated. * @ Author qbg */public class FactoryMethodPattern {/*** product class Abstraction: bicycle */abstract class Bike {public void ride () {}}/*** product implementation class: jet Bike */class GaintBike extends Bike {public void ride () {System. out. println ("Riding the Gaint Bike... ") ;}}/*** product implementation class: Merida bicycle */class MeridaBike extends Bike {public void ride () {System. out. println ("Riding the Merida Bike... ") ;}}/*** factory abstract class */interface ifacloud {/*** factory Method */public Bike createBike ();} /*** jet bicycle factory */class GaintBikeFactory implements ifacments {@ Overridepublic Bike createBike () {return new GaintBike ();}} /*** MeridaBikeFactory implements ifacloud {@ Overridepublic Bike createBike () {return new MeridaBike () ;}} public static void main (String [] args) {FactoryMethodPattern fm = new FactoryMethodPattern (); // go to the jet bicycle factory and ask for a jet to ride IFactory factory = fm. new GaintBikeFactory (); Bike gaint = factory. createBike (); gaint. ride (); // go to the Merida bicycle factory and ask a warrior to ride 600 factory = fm. new MeridaBikeFactory (); Bike merida = factory. createBike (); merida. ride ();}}
Iv. Advantages and Disadvantages

Advantages:

1. factory methods no longer bind classes related to specific applications to your code. For example, the specific bicycle implementation in the example is completed by using the abstract of the specific factory and bicycle in the Customer Code, without worrying about the specific bicycle brand, what it looks like.

2. Provide hooks for sub-classes. You can change the product implementation at any time.

3. Connect parallel class layers, which are generally used for factory methods of the tool nature.

Disadvantages:

1. A specific product implementation requires binding a corresponding factory method implementation. If you only want to create a specific ConcreteProduct instance, you have to create a subclass of the Creator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.