[设计模式]工厂模式

工厂模式

工厂模式演变一:

 1 package cn.itcast.factorydemo01;
 2 
 3 //声明一个接口
 4 interface Fruit {
 5     //接口中定义一个eat()方法
 6     public void eat();
 7 }
 8 //声明两个类实现这个接口
 9 class Apple implements Fruit{
10     public void eat() {
11         System.out.println("吃苹果");
12     }
13 }
14 class Orange implements Fruit{
15     public void eat(){
16         System.out.println("吃橘子");
17     }
18 }
19 //声明一个工厂类,用于获得一个水果Fruit的实例
20 class Factory{
21     public static Fruit getInstance(String className){
22         Fruit f = null;
23         if(className.equals("apple")){
24             f = new Apple();
25         }
26         if(className.equals("orange")){
27             f = new Orange();
28         }
29         return f;
30     }
31 }    
32     
33 public class FactoryDemo01 {
34     public static void main(String[] args) {
35         //调用工厂类Factory中的getInstance()方法
36         Fruit f = Factory.getInstance("apple");
37         f.eat();
38     }
39 }

工厂模式演变二:

 1 package cn.itcast.factorydemo02;
 2 
 3 //声明一个接口
 4 interface Fruit {
 5     //接口中定义一个eat()方法
 6     public void eat();
 7 }
 8 //声明两个类实现这个接口
 9 class Apple implements Fruit{
10     public void eat() {
11         System.out.println("吃苹果");
12     }
13 }
14 class Orange implements Fruit{
15     public void eat(){
16         System.out.println("吃橘子");
17     }
18 }
19 
20 //声明一个工厂类,用于获得一个水果Fruit的实例
21     /*
22     class Factory{
23         public static Fruit getInstance(String className){
24             Fruit f = null;
25             if(className.equals("apple")){
26                 f = new Apple();
27             }
28             if(className.equals("orange")){
29                 f = new Orange();
30             }
31             return f;
32         }
33     }    
34     */
35 class Factory{
36     public static Fruit getInstance(String className){
37         Fruit f = null;
38         //利用反射 由一个类的名称 得到一个类的实例对象
39         try {
40             f = (Fruit) Class.forName(className).newInstance();
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44         return f;
45     }
46 }
47 public class FactoryDemo02 {
48     public static void main(String[] args) {
49         //调用工厂类Factory中的getInstance()方法
50         //因为这个地方用到的是Class类反射生成的一个对象,所以这个地方getInstace(String name)
51         //这个name对应的是一个类路径.在哪个包下的什么类中
52         Fruit f = Factory.getInstance("cn.itcast.factorydemo02.Apple");
53         f.eat();
54     }
55     //这种方式的缺点是getInstance中要书写的类的名称太长.
56 }

工厂模式演变三:

 1 package cn.itcast.factorydemo03;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.util.InvalidPropertiesFormatException;
 9 import java.util.Properties;
10 
11 //声明一个接口
12 interface Fruit {
13     //接口中定义一个eat()方法
14     public void eat();
15 }
16 //声明两个类实现这个接口
17 class Apple implements Fruit{
18     public void eat() {
19         System.out.println("吃苹果");
20     }
21 }
22 class Orange implements Fruit{
23     public void eat(){
24         System.out.println("吃橘子");
25     }
26 }
27 class Factory{
28     public static Fruit getInstance(String className){
29         Fruit f = null;
30         //利用反射 由一个类的名称 得到一个类的实例对象
31         try {
32             f = (Fruit) Class.forName(className).newInstance();
33         } catch (Exception e) {
34             e.printStackTrace();
35         }
36         return f;
37     }
38 }
39 //定义一个属性操作类
40 class PropertiesOperate{
41     private Properties pro = null;
42     //在F盘new一个文件,代码和配置文件分离,通过配置文件控制代码的运行结果,这个是工厂模式的一个重要设计思想.
43     private File file = new File("F:"+File.separator+"fruit.properties");
44     //构造方法
45     public PropertiesOperate(){
46         this.pro = new Properties();
47         if(file.exists()){//如果属性(配置)文件存在
48             try {
49                 pro.loadFromXML(new FileInputStream(file));//读取这个文件
50             } catch (Exception e) {
51                 e.printStackTrace();
52             } 
53         }else{//如果这个属性(配置)文件不存在
54             this.save();
55         }
56         
57     }
58     private void save(){
59         //Properties对象pro的setProperty方法设置文件中的内容
60         this.pro.setProperty("apple", "cn.itcast.factorydemo03.Apple");
61         this.pro.setProperty("orange", "cn.itcast.factorydemo03.Orange");
62         try {
63             this.pro.storeToXML(new FileOutputStream(this.file), "Fruit");
64         } catch (Exception e) {
65             e.printStackTrace();
66         } 
67     }
68     public Properties getProperties(){
69         return this.pro;
70     }
71 }
72 public class FactoryDemo03 {
73     public static void main(String[] args) {
74         Properties pro = new PropertiesOperate().getProperties() ;
75         Fruit f = Factory.getInstance(pro.getProperty("orange"));
76         f.eat();
77     }
78 }

=================================华丽丽的分割线=======================================

posted @ 2014-10-02 22:48  SummerChill  阅读(204)  评论(0编辑  收藏  举报