Simple factory model -- factory method model (Introduction), factory --

Source: Internet
Author: User

Simple factory model -- factory method model (Introduction), factory --
1. Simple factory mode (static factory)

 The simple factory model does not belong to 23 standard design models

The simple factory mode is a special implementation of the factory method mode.

A simple factory is used by a factory class to determine which product class instance to create based on input parameters.

II. Implementation

Product interface or abstract class

Package factory;/** Product interface */public interface Product {// production process public void craftwork (); // Product type public void type ();}

Implementation subclass

Package factory;/** fresh milk class */public class Creamery implements Product {@ Override public void craftwork () {System. out. println ("milk + sterilization = fresh milk! ") ;}@ Override public void type () {System. out. println (" produce milk !! ");}}
Package factory;/** yogurt class */public class Yoghourt implements Product {@ Override public void craftwork () {System. out. println ("milk + yeast + food = yogurt! ") ;}@ Override public void type () {System. out. println (" yogurt is produced! ");}}

Exception

Package factory;/** custom Exception */public class BadException extends Exception {/*****/private static final long serialVersionUID = 1L; public BadException (String message) {super (message );}}

Static Factory

Package factory;/** static factory */public class StaticFactory {// return different products public static Product factory (String str) throws BadException {if (str. specified signorecase ("Creamery") {System. out. println ("======= produce fresh milk! ======== "); Return new Creamery ();} else if (str. equalsIgnoreCase ("Yoghourt") {System. out. println ("======= produce yogurt! ========== "); Return new Yoghourt () ;}else {throw new BadException (" No such product! ");}}}

Test class

Package factory;/** Test class */public class Test {public static void main (String [] args) {try {// you can also use input to create a Product through the factory to pass the value of Product cProduct = StaticFactory. factory ("Creamery"); cProduct. craftwork (); cProduct. type (); // you can use input to create a Product through a factory. The value is Product yProduct = StaticFactory. factory ("Yoghourt"); yProduct. craftwork (); yProduct. type ();} catch (BadException e) {e. printStackTrace ();}}}

Result:

========= Produce fresh milk! ========= Milk + sterilization = fresh milk! Produce fresh milk !! ======= Produce yogurt! =========== Milk + yeast + food = yogurt! Yogurt is produced!
3. Factory method model-Multi-gene factory Model

The factory class is no longer responsible for the creation of all products, but only responsible for the interface that must be implemented by the specific factory subclass

Package factorymethod;/** Product interface */public interface Product {// production process public void craftwork (); // Product type public void type ();}
Package factorymethod;/** fresh milk class */public class Creamery implements Product {@ Override public void craftwork () {System. out. println ("milk + sterilization = fresh milk! ") ;}@ Override public void type () {System. out. println (" produce milk !! ");}}
Package factorymethod;/** yogurt class */public class Yoghourt implements Product {@ Override public void craftwork () {System. out. println ("milk + yeast + food = yogurt! ") ;}@ Override public void type () {System. out. println (" yogurt is produced! ");}}
Package factorymethod; import factory. Product;/** Product factory */public interface ProductFactory {// The return type is Product interface public Product factory ();}
Package factorymethod; import factory. creamery; import factory. product;/** fresh milk factory */public class CreameryFactory implements ProductFactory {@ Override public Product factory () {System. out. println ("======= produce fresh milk! ======== "); Return new Creamery ();}}
Package factorymethod; import factory. product; import factory. yoghourt;/** yogurt factory */public class YoghourtFactory implements ProductFactory {@ Override public Product factory () {System. out. println ("======= produce yogurt! ========== "); Return new Yoghourt ();}}

Test class

Package factorymethod;/** factory method Test class */public class Test {public static void main (String [] args) {// create a product factory object and instantiate it as a fresh milk factory ProductFactory cf = new CreameryFactory (); cf. factory (). craftwork (); cf. factory (). type (); // create the product factory object and instantiate it as the yogurt factory ProductFactory yf = new CreameryFactory (); // call the product factory method yf. factory (). craftwork (); yf. factory (). type ();}}

Result:

========= Produce fresh milk! ========= Milk + sterilization = fresh milk! ========= Produce fresh milk! ========= Produce fresh milk !! ========= Produce fresh milk! ========= Milk + sterilization = fresh milk! ========= Produce fresh milk! ========= Produce fresh milk !!

To add a new product, you only need to add the new product interface subclass and product factory subclass.

Fully supports the open/closed Principle

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.