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