Java simple factory mode, java factory Mode

Source: Internet
Author: User

Java simple factory mode, java factory Mode
Head First design pattern Learning

Category

  • Simple Factory)
  • Factory Method)
  • Abstract Factory)

Introduction

  • Simple factory mode:

A simple factory is not a design pattern, but rather a programming habit. It is often called a simple factory pattern because it is often used, you can regard it as a special case of the factory model, but it cannot be ignored because it is not a real design model.

  • Factory method mode:

An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.

  • Abstract Factory mode:

 

   Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.

  • Differences:

    The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

Simple factory class diagram

    

 

 

Simple factory instance

  Product

package com.Observer.model;public  class food {public food(){}}

Product

Package com. Observer. model; public class ChickenPot extends food {public ChickenPot () {System. out. println ("Yellow Chicken ");}}

  

Package com. Observer. model; public class Noodles extends food {public Noodles () {System. out. println ("casserole instant Noodles ");}}

Factory

Package com. observer. model; public class SimpleFoodFactory {public food create (String type) {if (type. equals ("Yellow pheasant") {return new ChickenPot ();} else if (type. equals ("casserole instant Noodles") {return new Noodles () ;}return null ;}}

Test class

Package com. observer. model; public class Test {public static void main (String [] args) {SimpleFoodFactory factory = new SimpleFoodFactory (); food = factory. create ("casserole instant noodles"); food food1 = factory. create ("Yellow pheasant ");}}

Output result:

Braised Chicken in casserole

 Advantages of a simple factory:

If you want to implement changes in the future, you only need to modify the factory class.

Factory mode instance:

The hotel opened in a simple factory is too popular, attracting two agents. Because our code is easy to use, they want to use our code to ensure quality. In this way, we can use the factory model.

Product Type:

package com.Observer.model;public abstract class food {public food(){}}

Product

Package com. Observer. model; public class ChickenPot extends food {public ChickenPot (String name) {System. out. println (name + ": ");}}

 

Package com. Observer. model; public class Noodles extends food {public Noodles (String name) {System. out. println (name + ": casserole instant noodle ");}}

Factory Interface

package com.Observer.model;public interface factoryFood  {food create(String type);}

Factory implementation class

Package com. observer. model;/*** Tongzhou store * @ author Administrator **/public class TongFood implements factoryFood {@ Overridepublic food create (String type) {if (type. equals ("Yellow pheasant") {return new ChickenPot ("Tongzhou branch");} else if (type. equals ("casserole instant Noodles") {return new Noodles ("Tongzhou branch");} return null ;}}

  

Package com. observer. model;/*** Changping store * @ author Administrator **/public class ChangFood implements factoryFood {@ Overridepublic food create (String type) {if (type. equals ("Yellow Chicken") {return new ChickenPot ("Changping Branch");} else if (type. equals ("casserole instant Noodles") {return new Noodles ("Changping Branch");} return null ;}}

Test class

Package com. observer. model; public class Test {public static void main (String [] args) {factoryFood factory = new TongFood (); factory. create ("Yellow Chicken"); factoryFood factory1 = new ChangFood (); factory1.create ("casserole instant noodles ");}}

Output result:

Tongzhou branch: Huangpi chicken Changping Branch: casserole instant noodles

  

Factory method mode:

  Defines an interface for object creation, but the subclass determines which class is to be instantiated. The factory method causes the class to postpone the Instantiation to the subclass.

 

Reference: http://blog.csdn.net/jason0539/article/details/23020989

 

 

 

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.