zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:企业应用开发的思考和策略_AbstractFactory

    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class BetterPrinter implements Output
    {
        private String[] printData = new String[MAX_CACHE_LINE * 2];
        // 用以记录当前需打印的作业数
        private int dataNum = 0;
        public void out()
        {
            // 只要还有作业,继续打印
            while(dataNum > 0)
            {
                System.out.println("高速打印机正在打印:" + printData[0]);
                // 把作业队列整体前移一位,并将剩下的作业数减1
                System.arraycopy(printData , 1, printData, 0, --dataNum);
            }
        }
        public void getData(String msg)
        {
            if (dataNum >= MAX_CACHE_LINE * 2)
            {
                System.out.println("输出队列已满,添加失败");
            }
            else
            {
                // 把打印数据添加到队列里,已保存数据的数量加1。
                printData[dataNum++] = msg;
            }
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class BetterPrinterFactory
        implements OutputFactory
    {
        public Output getOutput()
        {
            // 该工厂只负责产生BetterPrinter对象
            return new BetterPrinter();
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class Computer
    {
        private Output out;
    
        public Computer(Output out)
        {
            this.out = out;
        }
        // 定义一个模拟获取字符串输入的方法
        public void keyIn(String msg)
        {
            out.getData(msg);
        }
        // 定义一个模拟打印的方法
        public void print()
        {
            out.out();
        }
        public static void main(String[] args)
        {
            // 使用OutputFactoryFactory工厂类创建OutputFactory
            OutputFactory of = OutputFactoryFactory
                .getOutputFactory("better");
            // 调用OuputFactory的方法获取Output对象,
            // 并将Output对象传入,创建Computer对象
            Computer c = new Computer(of.getOutput());
            c.keyIn("轻量级Java EE企业应用实战");
            c.keyIn("疯狂Java讲义");
            c.print();
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public interface Output
    {
        // 接口里定义的属性只能是常量
        int MAX_CACHE_LINE = 50;
        // 接口里定义的只能是public的抽象实例方法
        void out();
        void getData(String msg);
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public interface OutputFactory
    {
        // 仅定义一个方法用于返回输出设备。
        Output getOutput();
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author Yeeku.H.Lee kongyeeku@163.com
     * @version 1.0
     */
    public class OutputFactoryFactory
    {
        // 仅定义一个方法用于返回输出设备。
        public static OutputFactory getOutputFactory(
            String type)
        {
            if (type.equalsIgnoreCase("better"))
            {
                return new BetterPrinterFactory();
            }
            else
            {
                return new PrinterFactory();
            }
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    // 让Printer类实现Output
    public class Printer implements Output
    {
        private String[] printData = new String[MAX_CACHE_LINE];
        // 用以记录当前需打印的作业数
        private int dataNum = 0;
        public void out()
        {
            // 只要还有作业,继续打印
            while(dataNum > 0)
            {
                System.out.println("打印机打印:" + printData[0]);
                // 把作业队列整体前移一位,并将剩下的作业数减1
                System.arraycopy(printData , 1, printData, 0, --dataNum);
            }
        }
        public void getData(String msg)
        {
            if (dataNum >= MAX_CACHE_LINE)
            {
                System.out.println("输出队列已满,添加失败");
            }
            else
            {
                // 把打印数据添加到队列里,已保存数据的数量加1。
                printData[dataNum++] = msg;
            }
        }
    }
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class PrinterFactory
    	implements OutputFactory
    {
    	public Output getOutput()
    	{
    		// 该工厂只负责产生Printer对象
    		return new Printer();
    	}
    }
    

      

  • 相关阅读:
    WebStrom
    设计模式之6大原则
    tortoiseSVN 合并代码方法
    SpannableString属性详解
    TortoiseSVN设置比较工具为BeyondCompare
    Android 扩大view点击范围
    activity 与 fragment生命周期
    记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
    spring boot admin + spring boot actuator + erueka 微服务监控
    spring boot actuator 简单使用
  • 原文地址:https://www.cnblogs.com/tszr/p/12373042.html
Copyright © 2011-2022 走看看