---
title: "工厂设计模式"
author: "dsotbs936"
date: "2010-07-23T22:37:20Z"
tags: ["Apple", "EJB", "F#", "XML", "设计模式"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-07-23T22:37:20Z"
id: 720101
url: "https://dsotbs936.iteye.com/blog/720101"
---

为什么要有工厂设计模式？  
  • 解耦合  
  • 一看见接口就立刻想到工厂模式  
3.1、代码一  
 简单工厂设计：  
interface Fruit{  
 public void eat() ;  
}  
class Apple implements Fruit{  
 public void eat(){  
  System.out.println("吃苹果。。。") ;  
 }  
};  
class Orange implements Fruit{  
 public void eat(){  
  System.out.println("吃橘子。。。") ;  
 }  
};  
class Factory{  
 public static Fruit getInstance(String className){  
  Fruit f = null ;  
  if("apple".equals(className)){  
   f = new Apple() ;  
  }  
  if("orange".equals(className)){  
   f = new Orange() ;  
  }  
  return f ;  
 }  
};  
public class FactoryDemo01{  
 public static void main(String args\[]){  
  Fruit f = Factory.getInstance("apple") ;  
  f.eat() ;  
 }  
};  
 以上代码虽然完成了基本的工厂设计，但是发现如果现在接口增加了一个子类之后，需要修改工厂。  
3.2、代码二  
 以上代码如果增加了一个子类要修改工厂，那么下面使用Class类完成对象的实例化工作，使用之后，子类可以任意增加，而不用修改工厂类。  
interface Fruit{  
 public void eat() ;  
}  
class Apple implements Fruit{  
 public void eat(){  
  System.out.println("吃苹果。。。") ;  
 }  
};  
class Orange implements Fruit{  
 public void eat(){  
  System.out.println("吃橘子。。。") ;  
 }  
};  
class Factory{  
 public static Fruit getInstance(String className){  
  Fruit f = null ;  
  try{  
   f = (Fruit)Class.forName(className).newInstance() ;  
  }catch(Exception e){}  
  return f ;  
 }  
};  
public class FactoryDemo02{  
 public static void main(String args\[]){  
  Fruit f = Factory.getInstance("Apple") ;  
  f.eat() ;  
 }  
};  
3.3、代码三  
 应该定义一个配置文件，配置文件之中定义了接口的全部子类，以后直接使用配置文件就可以知道可以使用那些子类。  
 以后如果要增加子类的时候直接修改配置文件就可以增加子类了。  
 Properties类：属性操作类。  
import java.io.* ;  
import java.util.* ;  
interface Fruit{  
 public void eat() ;  
}  
class Apple implements Fruit{  
 public void eat(){  
  System.out.println("吃苹果。。。") ;  
 }  
};  
class Orange implements Fruit{  
 public void eat(){  
  System.out.println("吃橘子。。。") ;  
 }  
};  
class Cherry implements Fruit{  
 public void eat(){  
  System.out.println("吃樱桃。。。") ;  
 }  
};  
class Factory{  
 public static Fruit getInstance(String className){  
  Fruit f = null ;  
  try{  
   f = (Fruit)Class.forName(className).newInstance() ;  
  }catch(Exception e){}  
  return f ;  
 }  
};  
class Init{  
 public Properties init()  throws Exception{  
  // 读取全部的属性  
  Properties pro = new Properties() ;  
  File f = new File("fruit.xml") ;  
  if(f.exists()){  
   // 直接读取  
   pro.loadFromXML(new FileInputStream(f)) ;  
  }else{  
   // 设置若干个初始值  
   pro.setProperty("a","Apple") ;  
   pro.setProperty("o","Orange") ;  
   // 把内容保存在文件之中  
   pro.storeToXML(new FileOutputStream(f),"FRUIT") ;  
  }  
  return pro ;  
 }  
};  
public class FactoryDemo03{  
 public static void main(String args\[]) throws Exception{  
  Properties p = new Init().init() ;  
  System.out.println(p) ;  
  BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)) ;  
  System.out.print("输入选择的类名称：") ;  
  String str = buf.readLine() ;  
  Fruit f = Factory.getInstance(p.getProperty(str)) ;  
  f.eat() ;  
 }  
};  
 某一个子类的增加并不会影响程序的运行，程序认的只是key，而具体的value可以任意更改。  
 最早思路：以上的代码，达到配置文件与程序的相互分离。  
 现在思路：配置文件只接写在程序中：ANNOTION。EJB 3.0  
4、总结  
 只要是看见了接口，就要考虑工厂，有了工厂，就要考虑扩充子类的情况，扩充了子类就需要考虑到底如何知道所有的子类的完整的包.类名称。  
 永远记住：最好的程序设计就是：程序只认key，而具体的value可以任意的更改。  
代理设计模式、工厂设计模式是使用最多的