Design mode: Simple Factory mode

Source: Internet
Author: User

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

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
Simple Factory mode

Simple Factory mode (also known as the Static Factory mode), easy factory production by static method, the client hides the details of the product.

If Fox Studios (foxfilmfactory) can produce movies (IMovie), and there are many movies, there are action movies (Actionmovie), there are love movies (Lovemovie), then, we audience (audienceclient) We don't need to know exactly how these movies are made, we just have to let the film company show us.

The following is a UML class diagram to represent the relationship between them

IMovie interface

Package Org.leihuang.simplefactory;public interface IMovie {public    void play ();


Actionmovie class

Package Org.leihuang.simplefactory;public class Actionmovie implements IMovie {    @Override public    Void Play () { C2/>system.out.println ("Hum-ha-XI!");}    }


Lovemovie class

Package Org.leihuang.simplefactory;public class Lovemovie implements imovie{    @Override public    Void Play () {        System.out.println ("Elocho!");}    }


Foxfilmfactory class

Package Org.leihuang.simplefactory;public class Foxfilmfactory {public    static IMovie Createmovie (String name)            throws Instantiationexception, Illegalaccessexception,            classnotfoundexception {        return (IMovie) class.forname (name). newinstance ();    }}


Audienceclient class

Package Org.leihuang.simplefactory;public class Audienceclient {public    static void Main (string[] args) throws Instantiationexception,            illegalaccessexception, classnotfoundexception {        Foxfilmfactory.createmovie (" Org.leihuang.simplefactory.LoveMovie "). Play ();        Foxfilmfactory.createmovie ("Org.leihuang.simplefactory.ActionMovie"). Play ();    }}


From the above we can see that the audience does not need to know how the film is specifically produced, just tell Fox what we want to see the movie, then Fox will go to produce for you, and then you look.

2014-12-03 14:51:37

Brave,happy,thanksgiving!


Design mode: Simple Factory 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.