Java中工厂、配置文件和反射学习

学习程序的最好方式就是编代码实现它,这里我们假设一个场景为要开一个晚会,晚会的流程为:1.晚会开始;2.唱歌;3.舞蹈;4.小品;5结束。

一、传统方法

1、新建Java项目:party

2、在party项目中新建类EveningParty

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class EveningParty {  
  4.     public static void main(String[] args)  
  5.     {  
  6.           
  7.     }  
  8. }  
在EveningParty类中实现晚会的流程,新建process函数,在process函数中实现整个晚会流程,具体代码为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void process()  
  2.     {  
  3.         //1.晚会开始  
  4.         //2.唱歌  
  5.         //3.舞蹈  
  6.         //4.小品  
  7.         //5.晚会结束  
  8.         System.out.println("晚会开始");  
  9.           
  10.         //唱歌  
  11.         Singable singable = new Liudehua();  
  12.         singable.sing();  
  13.           
  14.         //跳舞  
  15.         Dancleable dancleable = new Xiaohudui();  
  16.         dancleable.dance();  
  17.           
  18.         //小品  
  19.         Essayable essayable = new Zhaobenshan();  
  20.         essayable.essay();  
  21.           
  22.         }  

这里出现了三个类sIngable、Dancleable、和Essayable,为新建的接口,为了程序扩展方面,需要建立三个接口 sIngable、Dancleable、和Essayable,分别用于实现唱歌跳舞和小品,具体的代码为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. /** 
  4.  * @author Leo Chu 
  5.  *Singable接口用于实现唱歌 
  6.  * 2014-8-24 
  7.  */  
  8. public interface Singable {  
  9.     public void sing();  
  10. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. /** 
  4.  * @author Leo Chu 
  5.  *Dancleable接口用于实现跳舞 
  6.  * 2014-8-24 
  7.  */  
  8. public interface Dancleable {  
  9.     public void dance();  
  10. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. /** 
  4.  * @author Leo Chu 
  5.  *Essayable接口用于实现小品 
  6.  * 2014-8-24 
  7.  */  
  8. public interface Essayable {  
  9.     public void essay();  
  10. }  

3.分别定义歌手、舞者和小品表演者分别实现上述接口

3.1定义Liudehua类实现Singable接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Liudehua implements Singable {  
  4.   
  5.   
  6.     public void sing() {  
  7.         // TODO Auto-generated method stub  
  8.         System.out.println("刘德华演唱中国人");  
  9.     }  
  10.   
  11. }  

3.2定义Naying实现Sinable接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Naying implements Singable  
  4. {  
  5.     public void sing()  
  6.     {  
  7.         System.out.println("那英演唱:雾里看花");  
  8.     }  
  9. }  

3.3定义Xiaohudui实现Dancleable接口
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Xiaohudui implements Dancleable {  
  4.   
  5.       
  6.     public void dance() {  
  7.         // TODO Auto-generated method stub  
  8.         System.out.println("小虎队舞蹈:爱");  
  9.     }  
  10.   
  11. }  

3.4定义Chenailian实现Dancleable接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Chenailian implements Dancleable {  
  4.   
  5.     public void dance() {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("陈爱莲舞蹈:天鹅舞");  
  8.     }  
  9.   
  10. }  

3.5定义Zhaobenshan实现Essayable接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Zhaobenshan implements Essayable {  
  4.   
  5.     public void essay() {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("赵本山小品:卖乖");  
  8.     }  
  9.   
  10. }  

3.6定义Liuqian实现Essayable接口
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class Liuqian implements Essayable {  
  4.   
  5.     public void essay() {  
  6.         // TODO Auto-generated method stub  
  7.         System.out.println("刘谦魔术:水中捞月");  
  8.     }  
  9.   
  10. }  
4.修改主函数中代码

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class EveningParty {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         new EveningParty().process();  
  7.     }  
  8.       
  9.     public void process()  
  10.     {  
  11.         //1.晚会开始  
  12.         //2.唱歌  
  13.         //3.舞蹈  
  14.         //4.小品  
  15.         //5.晚会结束  
  16.         System.out.println("晚会开始");  
  17.           
  18.         //唱歌  
  19.         Singable singable = new Liudehua();  
  20.     singable.sing();  
  21.           
  22.         //跳舞  
  23.         Dancleable dancleable = new Xiaohudui();  
  24.         dancleable.dance();  
  25.           
  26.         //小品  
  27. //      Essayable essayable = new Zhaobenshan();  
  28.         essayable.essay();  
  29.                       
  30.         System.out.println("晚会结束");  
  31.   
  32.     }  
  33.   
  34. }  

OK,程序可以正常运行。

但是程序这么写的话会出现一个问题:每次更好演员的时候需要修改晚会类的代码,这就给程序的扩展和维护带来了不便。那应该怎么做呢?可不可以吧这些麻烦的修改部分外包给一个代理去做,由代理类完成演员的修改,这就是我们下面要介绍的一种方法:用Factory类做代理负责代码的修改。

二/利用factory类,把需要变化的部分放到factory类中

1.在上面程序的基础上新建Factory类,

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. import java.util.ResourceBundle;  
  4.   
  5. public class Factory {  
  6.     public  Singable getSingable()  
  7.     {  
  8.         return new Liudehua();  
  9.     }  
  10.       
  11.     public  Dancleable getDancleable()   
  12.     {  
  13.         return new Xiaohudui();  
  14.     }  
  15.       
  16.     public static Essayable getEssayable()  
  17.     {  
  18.         return new Zhaobenshan();  
  19.     }  
  20. }  

2.修改晚会类代码

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. public class EveningParty {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         new EveningParty().process();  
  7.     }  
  8.       
  9.     public void process()  
  10.     {  
  11.         //1.晚会开始  
  12.         //2.唱歌  
  13.         //3.舞蹈  
  14.         //4.小品  
  15.         //5.晚会结束  
  16.         System.out.println("晚会开始");  
  17.           
  18. //      //唱歌  
  19. //      Singable singable = new Liudehua();  
  20. //      singable.sing();  
  21. //        
  22. //      //跳舞  
  23. //      Dancleable dancleable = new Xiaohudui();  
  24. //      dancleable.dance();  
  25. //        
  26. //      //小品  
  27. //      Essayable essayable = new Zhaobenshan();  
  28. //      essayable.essay();  
  29.           
  30.         Factory factory = new Factory();  
  31.         factory.getSingable().sing();  
  32.         factory.getDancleable().dance();  
  33.         factory.getEssayable().essay();  
  34.           
  35.           
  36.           
  37.         System.out.println("晚会结束");  
  38.   
  39.     }  
  40.   
  41. }  

OK,也是可以运行的

用这种方法不用修改晚会类,但是还是要修改代码,还有一种方法就是将演员的实例类写入配置文件

三、映射方法

1.在上面代码的基础上,在src目录下新建party.properties类,将演员的实例类写入配置文件,代码为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Singable = com.gmail.lsgjzhuwei.Liudehua  
  2. Dancleable = com.gmail.lsgjzhuwei.Xiaohudui  
  3. Essayable = com.gmail.lsgjzhuwei.Zhaobenshan  
2.修改Factory类中调用演员类的代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. import java.util.ResourceBundle;  
  4.   
  5. public class Factory {  
  6.     public static Singable getSingable()  
  7.     {  
  8.         String className = ResourceBundle.getBundle("party").getString("Singable");  
  9.         try {  
  10.             Object obj = Class.forName(className).newInstance();  
  11.             return (Singable) obj;  
  12.         }  catch (Exception e) {  
  13.             // TODO Auto-generated catch block  
  14.             e.printStackTrace();  
  15.             throw new RuntimeException("对象不正确");  
  16.         }     
  17.     }  
  18.       
  19.     public static  Dancleable getDancleable()   
  20.     {  
  21.         String className = ResourceBundle.getBundle("party").getString("Dancleable");  
  22.         try {  
  23.             Object obj = Class.forName(className).newInstance();  
  24.             return (Dancleable) obj;  
  25.         } catch (Exception e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.             throw new RuntimeException("对象不正确");  
  29.         }  
  30.           
  31.     }  
  32.       
  33.     public static Essayable getEssayable()  
  34.     {  
  35.         String className = ResourceBundle.getBundle("party").getString("Essayable");  
  36.         try {  
  37.             Object obj = Class.forName(className).newInstance();  
  38.             return (Essayable) obj;  
  39.         } catch (Exception e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.             throw new RuntimeException("对象不正确");  
  43.         }  
  44.     }  
  45. }  

3.不用修改晚会类代码,执行程序,OK正确。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值