Factory mode of Java design pattern

Source: Internet
Author: User
Tags getcolor

Factory mode: That is, Factory mode, factory everyone should know very well, mainly based on user needs to produce products, in the student era very close to the life of a simple example, such as: Noon class and students go to the cafeteria to eat, go to the xxx canteen window, inside aunt will ask what to eat, and then you will say what to eat or what, The aunt will and cook to communicate what we need, we do not have to consider the xxx we need to do, we only need to wait for the results, of course, there are many students in the cafeteria, the need is different, then the canteen here can be simply understood as "factory." Go directly to the Code section:

First, the definition of product interface: Ink Cartridges (interface)

1  Packageedu.aeon.model.factory;2 /**3  * 4  * @authorLzj5 * @create October 22, 2017 morning 2:54:136 * @describe definition of cartridge interface7  */8  Public InterfaceInk {9     //Print cartridge ColorTen      PublicString GetColor (); One}

Second, the definition of product 1: Color ink Cartridges

1  Packageedu.aeon.model.factory;2 /**3  * 4  * @authorLzj5 * @create October 22, 2017 morning 2:54:306 * @describe colorink Implement ink7  */8  Public classColorinkImplementsInk {9 Ten @Override One      PublicString GetColor () { A         return"Print with a color cartridge"; -     } -  the}

Iii. Definition of Product 2

1  Packageedu.aeon.model.factory;2 /**3  * 4  * @authorLzj5 * @create October 22, 2017 morning 2:54:496 * @describe greyink Implement ink7  */8  Public classGreyinkImplementsInk {9 Ten @Override One      PublicString GetColor () { A         return"Print with gray cartridges"; -     } -  the}

Four, the production of ink-type products factory

1  Packageedu.aeon.model.factory;2 /**3  * 4  * @authorLzj5 * @create October 22, 2017 morning 2:56:226 * @describe The implementation of the Ink factory7  */8  Public classInkfactory {9      PublicInk Getink (String str) {Ten         //returns an instance of the ink interface based on a parameter One         if(Str.equalsignorecase ("Color")){ A             return NewColorink (); -}Else{ -             return NewGreyink (); the         }  -          -     } -}

Five, test products

A): Common class Test code

1  Packageedu.aeon.model.factory.test;2 3 ImportEdu.aeon.model.factory.Ink;4 Importedu.aeon.model.factory.InkFactory;5 6 /**7  * @authorLzj8 * @create October 22, 2017 morning 2:59:339 * @describe Cartridge Factory test classTen * Production of color cartridge objects when color cartridges are required One * Produce gray cartridge objects when gray cartridges are required A  */ -  Public classFactorytest { -  the     /** - * at October 22, 2017 a.m. 2:59:33 by Lzj - * @Parameters1 string[] args - * @Returns void +      */ -      Public Static voidMain (string[] args) { +         //Create an instance of Inkfactory to get a factory instance AInkfactory factory=Newinkfactory (); atInk p=NULL; -         //use factory to get color cartridge objects -P=factory.getink ("Color"); - System.out.println (P.getcolor ()); -System.out.println ("================="); -         //use factory to get gray cartridge objects inP=factory.getink ("Grey"); - System.out.println (P.getcolor ()); to     } +  -}

The test results are:

  

b): Via Configuration file

The configuration file configuration code is as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5 Xmlns:tx= "Http://www.springframework.org/schema/tx"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd8 Http://www.springframework.org/schema/tx9 http://www.springframework.org/schema/tx/spring-tx-2.0.xsdTen HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP One http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "> A     <BeanID= "Color"class= "Edu.aeon.model.factory.ColorInk" /> -     <BeanID= "Grey"class= "Edu.aeon.model.factory.GreyInk" /> - </Beans>

Specific test class:

1  Packageedu.aeon.model.factory.test;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 ImportEdu.aeon.model.factory.ColorInk;6 ImportEdu.aeon.model.factory.GreyInk;7 ImportEdu.aeon.model.factory.Ink;8 /**9  * @authorLzjTen * @create October 22, 2017 morning 3:10:39 One * @describe to get specific product objects through configuration files A  */ -  Public classFactorytestbyxml { -  the     /** - * at October 22, 2017 a.m. 3:10:39 by Lzj - * @Parameters1 string[] args - * @Returns void +      */ -      Public Static voidMain (string[] args) { +ApplicationContext cxt=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); AINK cl = (colorink) cxt.getbean ("Color"); atInk gr = (greyink) cxt.getbean ("Grey"); - System.out.println (Cl.getcolor ()); -System.out.println ("===================="); - System.out.println (Gr.getcolor ()); -     } -}

The test results are:

  

Factory mode of Java design pattern

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.