疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java 工厂模式学习


 

  工厂模式分三种:简单工厂、工厂方法、抽象工厂。其中抽象工厂是用于多个产品族的情况。所谓产品族就是不同产品组成的有机整体,各不同产品之间相互依赖。打个比方,一台电脑有CPU、主板、内存和硬盘等,这些不同产品组成了一个产品族。就好比Dell和HP要把这些产品进行不同的组装,这时就需要用到抽象工厂。以下是代码演示:

  简单工厂,可以用反射实现:

  首先定义产品的抽象接口

  package com.demo.SimpleFactory;

  public interface CPU {

  public void run();

  }

  定义具体产品

  package com.demo.SimpleFactory;

  public class Intel implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Intel!");

  }

  }

  package com.demo.SimpleFactory;

  public class Amd implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Amd!");

  }

  }

  定义工厂类

  package com.demo.SimpleFactory;

  public class Factory {

  public static CPU create(String str) {

  CPU c = null;

  try {

  c = (CPU) Class.forName("com.demo.SimpleFactory." + str)

  .newInstance();

  } catch (Exception e) {

  e.printStackTrace();

  }

  return c;

  }

  }

  测试如下

  package com.demo.SimpleFactory;

  public class Test {

  public static void main(String[] args){

  CPU c=Factory.create("Amd");

  c.run();

  }

  }

  打印结果:

  This is Amd!

  工厂方法

  首先定义产品的抽象接口

  package com.demo.SimpleFactory;

  public interface CPU {

  public void run();

  }

  定义具体产品

  package com.demo.SimpleFactory;

  public class Intel implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Intel!");

  }

  }

  package com.demo.SimpleFactory;

  public class Amd implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Amd!");

  }

  }

  定义抽象工厂,具体现实由其子类负责

  package com.demo.FactoryMethod;

  public interface FactoryMethod {

  public CPU create();

  }

  具体子类工厂

  package com.demo.FactoryMethod;

  public class AmdFactory implements FactoryMethod {

  @Override

  public CPU create() {

  // TODO Auto-generated method stub

  return new Amd();

  }

  }

  package com.demo.FactoryMethod;

  public class IntelFactory implements FactoryMethod {

  public CPU create() {

  return new Intel();

  }

  }

  测试

  package com.demo.FactoryMethod;

  public class Test {

  public static void main(String[] args) {

  FactoryMethod af=new IntelFactory();

  CPU c=af.create();

  c.run();

  }

  }

  打印结果:

  This is Intel!

  抽象工厂

  首先定义产品的抽象接口

  package com.demo.SimpleFactory;

  public interface CPU {

  public void run();

  }

  定义具体产品

  package com.demo.SimpleFactory;

  public class Intel implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Intel!");

  }

  }

  package com.demo.SimpleFactory;

  public class Amd implements CPU {

  @Override

  public void run() {

  // TODO Auto-generated method stub

  System.out.println("This is Amd!");

  }

  }

  package com.demo.AbstractFactory;

  public interface Memory {

  public void read(String str);

  }

  package com.demo.AbstractFactory;

  public class KingstonMemory implements Memory {

  public KingstonMemory() {

  // TODO Auto-generated constructor stub

  }

  @Override

  public void read(String str) {

  // TODO Auto-generated method stub

  System.out.println("Kingston Read data is:"+str);

  }

  }

  package com.demo.AbstractFactory;

  public class AdataMemory implements Memory {

  public AdataMemory() {

  // TODO Auto-generated constructor stub

  }

  @Override

  public void read(String str) {

  // TODO Auto-generated method stub

  System.out.println("Adata read data is:"+str);

  }

  }

  定义抽象工厂

  package com.demo.AbstractFactory;

  public interface AbstractFactory {

  public CPU createCPU();

  public Memory createMemory();

  }

  不同子类产生不同产品族

  package com.demo.AbstractFactory;

  public class DellFactory implements AbstractFactory {

  @Override

  public CPU createCPU() {

  // TODO Auto-generated method stub

  return new Intel();

  }

  @Override

  public Memory createMemory() {

  // TODO Auto-generated method stub

  return new KingstonMemory();

  }

  }

  package com.demo.AbstractFactory;

  public class HPFactory implements AbstractFactory {

  @Override

  public CPU createCPU() {

  // TODO Auto-generated method stub

  return new Amd();

  }

  @Override

  public Memory createMemory() {

  // TODO Auto-generated method stub

  return new AdataMemory();

  }

  }

  测试

  package com.demo.AbstractFactory;

  public class Test {

  public static void main(String[] args){

  AbstractFactory hp=new HPFactory();

  CPU cpu=hp.createCPU();

  Memory memory=hp.createMemory();

  cpu.run();

  memory.read("Pass");

  }

  }

  打印结果:

  This is Amd!

  Adata read data is:Pass