Design Mode: simple factory Mode

Source: Internet
Author: User

Design Mode: simple factory 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 Simple factory Mode

    The 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.


Related Article

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.