Design pattern: Factory method mode

Source: Internet
Author: User

Original address: http://leihuang.org/2014/12/03/factory-method/

Creational mode

The production of objects needs to consume system resources, so how to produce, manage and manipulate objects efficiently is always a subject to be discussed, the creational model is related to the establishment of objects, and the model under this classification gives some guiding principles and the direction of design. Listed below are all belong to the creational mode

    • Simple Factory Mode
    • Abstract Factory Mode
    • Builder mode
    • Factory method Mode
    • Prototype mode
    • Singleton mode
    • Registry of Singleton Mode
Basic concepts

When a class cannot anticipate what kind of object to create or a class requires subclasses to specify the created object, we need to use the Factory method pattern. In short, Factory method can produce different instances according to different conditions. Of course these different instances usually belong to the same type and have a common parent class. Factory method encapsulates the specific process of creating these instances, simplifies the application of the client, and improves the extensibility of the program, allowing for the creation of new classes to be created with minimal changes in the future. Often we use the factory method as a standard way to create objects, and when we find that more flexibility is needed, we begin to consider converting to other creation patterns.

Factory method Mode Structure

    1. Product: An abstract class of products that need to be created.
    2. Concreteproduct:product Sub-category, a series of specific products.
    3. Creator: Abstract creator interface that declares the factory Method that returns the product type object.
    4. Concretecreator: The specific creator, overriding factory Method in creator, returns an instance of the Concreteproduct type.
Code implementation

It is better to write code directly than to read so much.

To make a distinction between a simple factory and an abstract factory, we also take the example of a Fox film company.

First define a movie abstract class, and then inherit two specific movie subclasses, action slices (actionmovie), love Slices (lovemovie). Then there is an abstract factory (foxfilmfactory), which has two subcategories, one producing Kung fu films (actionfoxfactory), one production of love films (Lovefoxfactory). Then our audience (audienceclient), Just ask the abstract factory to provide the film, do not need to control it specifically how to classify.

The following is a class structure diagram.

Movie abstract class--film collectively

Package Org.leihuang.factorymethod;public Abstract class Movie {public    abstract void Play ();

Actionmovie subclass of Abstract class--action movie

Package Org.leihuang.factorymethod;public class Actionmovie extends Movie {    @Override public    Void Play () {        System.out.println ("Hum-ha-XI!");}    }

Lovemovie inherit movie--Love Movie

Package Org.leihuang.factorymethod;public class Lovemovie extends Movie {    @Override public    Void Play () {        SYSTEM.OUT.PRINTLN ("Elocho!");}    }

Foxfilmfactory Abstract class--Fox head Office

Package Org.leihuang.factorymethod;public Abstract class Foxfilmfactory {public    abstract Movie Createmovie (String name);}

Actionfoxfactory inherit Foxfilmfactory class--the department that produces action film

Package Org.leihuang.factorymethod;public class Actionfoxfactory extends Foxfilmfactory {    @Override    public Movie Createmovie (String name) {        movie movie = null;        try {            movie = (movie) class.forname (name). newinstance ();        } catch (Instantiationexception e) {            E.printstacktrace ();        } catch (Illegalaccessexception e) {            e.printstacktrace ();        } catch (ClassNotFoundException e) {            E.printstacktrace ();        }        return movie;}    }

Lovefoxfactory inheriting abstract class--the Department of producing Love films

Package Org.leihuang.factorymethod;public class Lovefoxfactory extends Foxfilmfactory {    @Override    public Movie Createmovie (String name) {        movie movie = null;        try {            movie = (movie) class.forname (name). newinstance ();        } catch (Instantiationexception e) {            E.printstacktrace ();        } catch (Illegalaccessexception e) {            e.printstacktrace ();        } catch (ClassNotFoundException e) {            E.printstacktrace ();        }        return movie;}    }

Audienceclient-Audience

Package Org.leihuang.factorymethod;public class Audienceclient {public    static void Main (string[] args) {        Foxfilmfactory foxaction = new Actionfoxfactory ();        Foxaction.createmovie ("Org.leihuang.factorymethod.ActionMovie"). Play ();        Foxfilmfactory foxlove = new Lovefoxfactory ();        Foxlove.createmovie ("Org.leihuang.factorymethod.LoveMovie"). Play ();    }}

2014-12-03 23:55:17

Brave,happy,thanksgiving!


Design pattern: Factory method mode

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.