Design Mode: Factory method mode

Source: Internet
Author: User

Design Mode: Factory method mode

 

 

Creational Model

 

The generation of objects consumes system resources, so how to efficiently produce, manage, and operate objects has always been a topic worth discussing. The Creational model is related to the establishment of objects, the pattern under this classification provides some guiding principles and design directions. The list below all belongs to the Creational model.

  • Simple Factory mode Abstract Factory mode Builder mode Factory Method mode Prototype mode Singleton mode Registry of Singleton Mode Basic Concepts

    The Factory Method mode is used when a class cannot predict the type of object to be created or when a class needs to be specified by the subclass. to put it simply, the Factory Method can generate different instances according to different conditions. Of course, these different instances generally belong to the same type and share a common parent class. the Factory Method encapsulates the specific process of creating these instances, simplifies the application of the client, and improves the program scalability, this allows you to add new classes with minimal changes in the future. generally, we use Factory Method as a standard Method for object creation. When we find that more flexibility is required, we start to consider converting to other creation modes.

    Factory method Schema

    1. 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 Implementation

      After 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();    }}

       


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.