Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12299446
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: Java

2015-07-14 16:20:11

1、ICPU.java 接口,定义公共方法Calculate


  1. package cn.javass.dp.abstractfactory.example1;
  2. /**
  3. * CPU的接口
  4. */
  5. public interface ICPU
  6. {
  7.     /**
  8.      * 示意方法,CPU具有运算的功能
  9.      */
  10.     public void Calculate();
  11. }


2、详细CPU类

IntelCPU.java


  1. package cn.javass.dp.abstractfactory.example1;
  2. /**
  3. * Intel的CPU实现
  4. */
  5. public class IntelCPU implements ICPU
  6. {
  7.     /**
  8.      * CPU的针脚数目
  9.      */
  10.     private int pins = 0;
  11.     /**
  12.      * 构造方法,传入CPU的针脚数目
  13.      * @param pins
  14.      * CPU的针脚数目
  15.      */
  16.     public IntelCPU(int pins)
  17.     {
  18.         this.pins = pins;
  19.     }
  20.     public void Calculate()
  21.     {
  22.         System.out.println("now in Intel CPU,pins=" + pins);
  23.     }
  24. }


AMDCPU.java


  1. package cn.javass.dp.abstractfactory.example1;
  2. /**
  3. * AMD的CPU实现
  4. */
  5. public class AMDCPU implements ICPU
  6. {
  7.     /**
  8.      * CPU的针脚数目
  9.      */
  10.     private int pins = 0;
  11.     /**
  12.      * 构造方法,传入CPU的针脚数目
  13.      *
  14.      * @param pins
  15.      * CPU的针脚数目
  16.      */
  17.     public AMDCPU(int pins)
  18.     {
  19.         this.pins = pins;
  20.     }
  21.     public void Calculate()
  22.     {
  23.         System.out.println("now in AMD CPU,pins=" + pins);
  24.     }
  25. }


3、CPU工厂类

CPUFactory.java


  1. package cn.javass.dp.abstractfactory.example1;
  2. /**
  3. * 创建CPU的简单工厂
  4. */
  5. public class CPUFactory
  6. {
  7.     /**
  8.      * 创建CPU接口对象的方法
  9.      *
  10.      * @param type
  11.      * 选择CPU类型的参数
  12.      * @return CPU接口对象的方法
  13.      */
  14.     public static ICPU CreateCPU(int type)
  15.     {
  16.         ICPU cpu = null;
  17.         // 根据参数来选择并创建相应的CPU对象
  18.         if (type == CpuType.INTEL)
  19.         {
  20.             cpu = new IntelCPU(1156);
  21.         } else if (type == CpuType.AMD)
  22.         {
  23.             cpu = new AMDCPU(939);
  24.         }
  25.         return cpu;
  26.     }
  27. }


4、工具、client类

CpuType.java


  1. package cn.javass.dp.abstractfactory.example1;
  2. public class CpuType
  3. {
  4.     public static int INTEL = 1;
  5.     public static int AMD = 2;
  6. }


Client.java


  1. package cn.javass.dp.abstractfactory.example1;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. public class Client
  6. {
  7.     public static void main(String[] args) throws IOException
  8.     {
  9.         int cpuType = 0;
  10.         ICPU cpu = null;
  11.          System.out.print("Starting ....... \n");
  12.          
  13.         for(;;)
  14.         {
  15.             InputStreamReader isr=new InputStreamReader(System.in);
  16.             BufferedReader br=new BufferedReader(isr);
  17.             //if(br.readLine().equalsIgnoreCase(""))
  18.             // continue;
  19.             
  20.             cpuType = Integer.parseInt(br.readLine());
  21.             if(cpuType == CpuType.AMD)
  22.             {
  23.                 cpu = CPUFactory.CreateCPU(cpuType);
  24.                 cpu.Calculate();
  25.                 System.out.print("--------------------------------------\n");
  26.             }
  27.             
  28.             if(cpuType == CpuType.INTEL)
  29.             {
  30.                 cpu = CPUFactory.CreateCPU(cpuType);
  31.                 cpu.Calculate();
  32.                 System.out.print("--------------------------------------\n");
  33.             }
  34.         }
  35.         
  36.     }
  37. }



image


 

参考文献:

《研磨设计模式》王斌、陈臣

阅读(2144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~