• java设计模式之factory模式


    factory模式:即工厂模式,工厂大家应该很了解,主要是根据用户需求生产产品的,举个在学生时代很亲近生活的简单例子,如:中午下课了和同学们一起去食堂吃饭,走到xxx食堂窗口,里面阿姨会问吃什么、然后你会说吃什么什么的,完了阿姨会和厨子去沟通我们所需要的东西,我们完全不用考虑我们需要的xxx是怎么做的,我们只需要等结果就可以了,当然食堂里面有很多同学,需要的都是各不相同的,那么这里的食堂可以简单的理解为"工厂"。直接进入代码部分:

    一、产品接口的定义:墨盒(接口) 

     1 package edu.aeon.model.factory;
     2 /**
     3  * 
     4  * @author lzj
     5  * @create 2017年10月22日 上午2:54:13
     6  * @describe 墨盒接口的定义
     7  */
     8 public interface Ink {
     9     //打印墨盒颜色
    10     public String getColor();
    11 }

    二、产品1的定义:彩色墨盒

     1 package edu.aeon.model.factory;
     2 /**
     3  * 
     4  * @author lzj
     5  * @create 2017年10月22日 上午2:54:30
     6  * @describe ColorInk实现Ink
     7  */
     8 public class ColorInk implements Ink {
     9 
    10     @Override
    11     public String getColor() {
    12         return "使用彩色墨盒打印";
    13     }
    14 
    15 }

    三、产品2的定义

     1 package edu.aeon.model.factory;
     2 /**
     3  * 
     4  * @author lzj
     5  * @create 2017年10月22日 上午2:54:49
     6  * @describe GreyInk实现Ink
     7  */
     8 public class GreyInk implements Ink {
     9 
    10     @Override
    11     public String getColor() {
    12         return "使用灰色墨盒打印";
    13     }
    14 
    15 }

    四、生产墨盒类产品的工厂

     1 package edu.aeon.model.factory;
     2 /**
     3  * 
     4  * @author lzj
     5  * @create 2017年10月22日 上午2:56:22
     6  * @describe Ink工厂的实现
     7  */
     8 public class InkFactory {
     9     public Ink getInk(String str){
    10         //根据参数返回Ink接口的实例
    11         if(str.equalsIgnoreCase("color")){
    12             return new ColorInk();
    13         }else{
    14             return new GreyInk();
    15         } 
    16         
    17     }
    18 }

    五、测试产品

      a):普通类测试代码

     1 package edu.aeon.model.factory.test;
     2 
     3 import edu.aeon.model.factory.Ink;
     4 import edu.aeon.model.factory.InkFactory;
     5 
     6 /**
     7  * @author lzj
     8  * @create 2017年10月22日 上午2:59:33
     9  * @describe 墨盒工厂测试类
    10  * 当需要彩色墨盒时生产彩色墨盒对象
    11  * 当需要灰色墨盒时生产灰色墨盒对象
    12  */
    13 public class FactoryTest {
    14 
    15     /**
    16      * at 2017年10月22日 上午2:59:33 by lzj
    17      * @Parameters1 String[] args
    18      * @Returns void
    19      */
    20     public static void main(String[] args) {
    21         //创建InkFactory的实例,获得工厂实例
    22         InkFactory factory=new InkFactory();
    23         Ink p=null;
    24         //使用工厂获得彩色墨盒对象
    25         p=factory.getInk("color");
    26         System.out.println(p.getColor());
    27         System.out.println("=================");
    28         //使用工厂获得灰色墨盒对象
    29         p=factory.getInk("grey");
    30         System.out.println(p.getColor());
    31     }
    32 
    33 }

       测试结果为:

      

      b):通过配置文件

      配置文件配置代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="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/beans 
     7     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
     8     http://www.springframework.org/schema/tx 
     9     http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
    10     http://www.springframework.org/schema/aop 
    11     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    12     <bean id="color" class="edu.aeon.model.factory.ColorInk" />
    13     <bean id="grey" class="edu.aeon.model.factory.GreyInk" />
    14 </beans>

      具体测试类:

     1 package edu.aeon.model.factory.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 import edu.aeon.model.factory.ColorInk;
     6 import edu.aeon.model.factory.GreyInk;
     7 import edu.aeon.model.factory.Ink;
     8 /**
     9  * @author lzj
    10  * @create 2017年10月22日 上午3:10:39
    11  * @describe 通过配置文件得到具体产品的对象
    12  */
    13 public class FactoryTestByXML {
    14 
    15     /**
    16      * at 2017年10月22日 上午3:10:39 by lzj
    17      * @Parameters1 String[] args
    18      * @Returns void
    19      */
    20     public static void main(String[] args) {
    21         ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
    22         Ink cl = (ColorInk) cxt.getBean("color");
    23         Ink gr = (GreyInk) cxt.getBean("grey");
    24         System.out.println(cl.getColor());
    25         System.out.println("====================");
    26         System.out.println(gr.getColor());
    27     }
    28 }

      测试结果为:

      

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    算术运算
    数据分析
    科学计算
    面向对象
    文件操作-py
    pillow图像处理
    模块
    固定翼飞行过程产生的阻力
    修改行间距等基本操作
    文件操作
  • 原文地址:https://www.cnblogs.com/aeon/p/7712137.html
Copyright © 2020-2023  润新知