Design mode-factory method Mode [Java Edition]

Source: Internet
Author: User

The simple factory model described in the previous article has the advantage of decoupling the business from the factory class. However, the factory method contains logical judgments, based on the client's choice of dynamic instantiation of the relevant classes, if you add a new phone model, you need to modify the logical judgment in the factory class, new case to judge the branch of the model, which violates the open-closed design principles .

The open-close principle is that classes, modules, functions, etc. can be extended, but cannot be modified

The factory method model is further abstracted on the basis of the simple factory model, abstracting the original factory from an interface that only corresponds to a method of creating a factory, each of which corresponds to a specific factory class.

The UML for factory method mode is as follows:

Implementation of factory method pattern

Start by creating an "apple Phone" class that defines a way to get the size of your phone

/**@author*/publicabstract class  Applephone {    /**     * Get size     * /     protected  Abstract void getsize ();}

Different models of "mobile phones" for Apple phones

/*** Iphone4 *@authorSONGWZ **/ Public classIphone4extendsapplephone{@Override Public voidGetSize () {System.out.println ("IPhone4 Screen: 3.5 inch"); }}/*** IPhone5 *@authorgt780 **/ Public classIphone5extendsApplephone {@Override Public voidGetSize () {System.out.println ("IPhone5 Screen: 4 inch"); }}/*** IPhone6 *@authorSONGWZ **/ Public classIphone6extendsapplephone{@Override Public voidGetSize () {System.out.println ("IPhone6 Screen: 4.7 inch"); }}

Building a factory interface

/**@author*/publicabstract class  ifactory {        /**     * Create various models of mobile     phone @return         */ protected  Abstract   Applephone Createphone ();}

Different specific plants for different products

/*** IPHONE4 Factory *@authorSONGWZ **/ Public classIphone4factoryextendsifactory{@Override PublicApplephone Createphone () {return NewIphone4 (); }}/*** IPHONE5 Factory *@authorSONGWZ **/ Public classIphone5factoryextendsifactory{@Override PublicApplephone Createphone () {return NewIphone5 (); }}/*** IPHONE6 Factory *@authorSONGWZ **/ Public classIphone6factoryextendsifactory{@Override PublicApplephone Createphone () {return NewIphone6 (); }}

Client Calls

 public  class   Client { public  static  void   main (string[] args) {ifactory ifactory  = new Iphone4factory ();        Applephone ap  = Ifactory.createphone ();        Ap.getsize ();        Ifactory  = new   Iphone5factory ();        AP  = Ifactory.createphone ();        Ap.getsize ();        Ifactory  = new   Iphone6factory ();        AP  = Ifactory.createphone ();    Ap.getsize (); }}

Factory method mode moves the logical judgment of a simple factory to the client code, perfecting the simple factory design pattern.

However, each new product needs to add a product class and a specific factory class, if the product category is large, there will be many classes. We can solve this problem by "reflection" technology.

/*** Create individual plants by reflection *@authorSONGWZ **/ Public classFactory { Public Staticapplephone Createphone (Class cls) {applephone Applephone=NULL; Try{Applephone=(Applephone) Class.forName (Cls.getname ()). Newinstance (); } Catch(Exception e) {System.out.println ("Exception occurred"); }        returnApplephone; }}

Design mode-factory method Mode [Java Edition]

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.