Product: the abstract class of the Product to be created. concreteProduct: a subclass of Product, a series of specific products. creator: Specifies the abstract Creator interface, which declares that the Factory Method of the Product type object is returned. concreteCreator: Specifies the Creator. Override the Factory Method in the Creator and return the ConcreteProduct type instance. code ImplementationAfter reading so much, it is better to write code directly.
In order to distinguish between simple factories and abstract factories, we also take the example of the fox film company.
First define a Movie abstract class, and then inherit the sub-classes of two specific movies, ActionMovie and LoveMovie ). then there is an abstract factory (FoxFilmFactory), which has two sub-classes: ActionFoxFactory and LoveFoxFactory ). then our audience (AudienceClient) only asks the abstract factory to provide the film, and does not need to worry about how it is classified.
The following is a class structure diagram.
Movie abstract class-Movie collectively
package org.leihuang.factorymethod;public abstract class Movie { public abstract void play() ;}
Subclass of ActionMovie abstract class-action movies
Package org. leihuang. factorymethod; public class ActionMovie extends Movie {@ Override public void play () {System. out. println (HUM haha !); }}
LoveMovie inherits Movie-love movies
Package org. leihuang. factorymethod; public class LoveMovie extends Movie {@ Override public void play () {System. out. println (Love Tiger Oil !); }}
FoxFilmFactory abstract class -- FOX Corporation
package org.leihuang.factorymethod;public abstract class FoxFilmFactory { public abstract Movie createMovie(String name) ;}
ActionFoxFactory inherits the FoxFilmFactory class-the Department that produces action movies
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 inherits the abstract class-the Department that produces love movies
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(); }}