Simple Factory mode Abstract Factory mode Builder mode Factory Method mode Prototype mode Singleton mode Registry of Singleton Mode Simple factory ModeThe Simple Factory mode (also known as the Static Factory mode) allows a Simple Factory to produce products through the Static method, hiding the details of the products on the client.
Assume that FoxFilmFactory is able to produce IMovie, and there are many movies, ActionMovie and LoveMovie. Then, our audience (AudienceClient) we don't need to know how these movies are made. We just need to let movie companies show them to us.
The following uses 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 () {System. out. println (HUM haha !); }}
LoveMovie class
Package org. leihuang. simplefactory; public class LoveMovie implements IMovie {@ Override public void play () {System. out. println (Love Tiger Oil !); }}
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 produced. They just need to tell fox what movie we want to watch, and then fox will produce it for you, and then you just want to watch it.