Design mode-Factory mode (Simple Factory)

Source: Internet
Author: User

First, what is the Factory mode:

In the development of human society, at first people's life needs are confession self-sufficiency, food, clothing, furniture are made by themselves, so that people have to contact with many industries, very troublesome. Then there are a variety of "factories" that are replaced by you to produce what you want, and if you need to tell them, they will give you production so that you can devote a lot of effort to other important things.

In the program world, as before, whenever we need any instance object, is the new out of their own, such a disadvantage will be associated with a lot of classes, coupling high (to new a dog, will introduce the dog class, to new a cat, will introduce Cat class), then we can completely like the real world, Give this job to the "factory" to do, we want to create what type of instance, as long as the factory passed some parameters to inform them, the factory will create an instance object for you. This is the idea of Factory mode. The factory model is divided into three types: simple Factory mode, factory method mode, abstract Factory mode . This article explains the simple factory model.

Second, simple Factory mode:

  

  Simple Factory mode is the simplest form of Factory mode. Its factory class is a concrete factory class with logical judgments that determine the different parameters you pass in and create different instances. This is not a problem, but this is not perfect , Please see the example:

//Abstract product class: define a class of products, interfaces, or abstract classes  Public Interface animal{    publicvoid// interface only declares methods, does not implement }
 //  Specific product class 1:dog class implements animal interface  public  class  Dog implements  Span style= "color: #000000;" > animal{ public  void   run () {System.out.println ( "Dog run!    "); }}
 //  Specific product class 2:cat class implements animal interface  public  class  Cat implements  Span style= "color: #000000;" > animal{ public  void   run () {System.out.println ( "Cat run!    "); }}
 //  specific factory class: Incoming parameters, producing different animals.  public  class   factory{ public   Animal Create (String            STR) { if  (Str.equals ("Dog"  return  new   Dog ();  if  (Str.equals ("Cat"  return  new   Cat (); }}
// Main program: the class to use for the instance  Public class Main () {    publicstaticvoid  main (string[] args)    {          New  Factory ();         // incoming dog parameter, production object        Cat cat = (cat) fact.create ("Cat");}    }

The disadvantage of this simple factory example is that when the product category increases, the programmer, in addition to creating this specific product class, has to add the judgment logic in the factory class, that is, to modify the existing classes, which is not conducive to maintenance. If we change the requirements, our principle is to create new classes, write new requirements in new places, and not modify the original code. So, we improved the simple factory model and came up with a factory method model. What is the factory approach pattern? Please see the next article.

Design mode-Factory mode (Simple Factory)

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.