学习程序的最好方式就是编代码实现它,这里我们假设一个场景为要开一个晚会,晚会的流程为:1.晚会开始;2.唱歌;3.舞蹈;4.小品;5结束。
1、新建java项目:party
2、在party项目中新建类EveningParty
package com.gmail.lsgjzhuwei; public class EveningParty { public static void main(String[] args) { } }在EveningParty类中实现晚会的流程,新建process函数,在process函数中实现整个晚会流程,具体代码为:
public void process() { //1.晚会开始 //2.唱歌 //3.舞蹈 //4.小品 //5.晚会结束 System.out.println("晚会开始"); //唱歌 Singable singable = new Liudehua(); singable.sing(); //跳舞 Dancleable dancleable = new Xiaohudui(); dancleable.dance(); //小品 Essayable essayable = new Zhaobenshan(); essayable.essay(); }
package com.gmail.lsgjzhuwei; /** * @author Leo Chu *Singable接口用于实现唱歌 * 2014-8-24 */ public interface Singable { public void sing(); }
package com.gmail.lsgjzhuwei; /** * @author Leo Chu *Dancleable接口用于实现跳舞 * 2014-8-24 */ public interface Dancleable { public void dance(); }
package com.gmail.lsgjzhuwei; /** * @author Leo Chu *Essayable接口用于实现小品 * 2014-8-24 */ public interface Essayable { public void essay(); }
3.1定义Liudehua类实现Singable接口
package com.gmail.lsgjzhuwei; public class Liudehua implements Singable { public void sing() { // TODO Auto-generated method stub System.out.println("刘德华演唱中国人"); } }
3.2定义Naying实现Sinable接口
package com.gmail.lsgjzhuwei; public class Naying implements Singable { public void sing() { System.out.println("那英演唱:雾里看花"); } }
package com.gmail.lsgjzhuwei; public class Xiaohudui implements Dancleable { public void dance() { // TODO Auto-generated method stub System.out.println("小虎队舞蹈:爱"); } }
3.4定义Chenailian实现Dancleable接口
package com.gmail.lsgjzhuwei; public class Chenailian implements Dancleable { public void dance() { // TODO Auto-generated method stub System.out.println("陈爱莲舞蹈:天鹅舞"); } }
3.5定义Zhaobenshan实现Essayable接口
package com.gmail.lsgjzhuwei; public class Zhaobenshan implements Essayable { public void essay() { // TODO Auto-generated method stub System.out.println("赵本山小品:卖乖"); } }
package com.gmail.lsgjzhuwei; public class Liuqian implements Essayable { public void essay() { // TODO Auto-generated method stub System.out.println("刘谦魔术:水中捞月"); } }4.修改主函数中代码
package com.gmail.lsgjzhuwei; public class EveningParty { public static void main(String[] args) { new EveningParty().process(); } public void process() { //1.晚会开始 //2.唱歌 //3.舞蹈 //4.小品 //5.晚会结束 System.out.println("晚会开始"); //唱歌 Singable singable = new Liudehua(); singable.sing(); //跳舞 Dancleable dancleable = new Xiaohudui(); dancleable.dance(); //小品 // Essayable essayable = new Zhaobenshan(); essayable.essay(); System.out.println("晚会结束"); } }
但是程序这么写的话会出现一个问题:每次更好演员的时候需要修改晚会类的代码,这就给程序的扩展和维护带来了不便。那应该怎么做呢?可不可以吧这些麻烦的修改部分外包给一个代理去做,由代理类完成演员的修改,这就是我们下面要介绍的一种方法:用Factory类做代理负责代码的修改。
1.在上面程序的基础上新建Factory类,
package com.gmail.lsgjzhuwei; import java.util.ResourceBundle; public class Factory { public Singable getSingable() { return new Liudehua(); } public Dancleable getDancleable() { return new Xiaohudui(); } public static Essayable getEssayable() { return new Zhaobenshan(); } }
package com.gmail.lsgjzhuwei; public class EveningParty { public static void main(String[] args) { new EveningParty().process(); } public void process() { //1.晚会开始 //2.唱歌 //3.舞蹈 //4.小品 //5.晚会结束 System.out.println("晚会开始"); // //唱歌 // Singable singable = new Liudehua(); // singable.sing(); // // //跳舞 // Dancleable dancleable = new Xiaohudui(); // dancleable.dance(); // // //小品 // Essayable essayable = new Zhaobenshan(); // essayable.essay(); Factory factory = new Factory(); factory.getSingable().sing(); factory.getDancleable().dance(); factory.getEssayable().essay(); System.out.println("晚会结束"); } }
用这种方法不用修改晚会类,但是还是要修改代码,还有一种方法就是将演员的实例类写入配置文件
1.在上面代码的基础上,在src目录下新建party.properties类,将演员的实例类写入配置文件,代码为:
Singable = com.gmail.lsgjzhuwei.Liudehua Dancleable = com.gmail.lsgjzhuwei.Xiaohudui Essayable = com.gmail.lsgjzhuwei.Zhaobenshan2.修改Factory类中调用演员类的代码:
package com.gmail.lsgjzhuwei; import java.util.ResourceBundle; public class Factory { public static Singable getSingable() { String className = ResourceBundle.getBundle("party").getString("Singable"); try { Object obj = Class.forName(className).newInstance(); return (Singable) obj; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("对象不正确"); } } public static Dancleable getDancleable() { String className = ResourceBundle.getBundle("party").getString("Dancleable"); try { Object obj = Class.forName(className).newInstance(); return (Dancleable) obj; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("对象不正确"); } } public static Essayable getEssayable() { String className = ResourceBundle.getBundle("party").getString("Essayable"); try { Object obj = Class.forName(className).newInstance(); return (Essayable) obj; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("对象不正确"); } } }
原文地址:http://blog.csdn.net/longshengguoji/article/details/38795945