java学习

java学习

 

设计模式之简单工厂

package simpleFactory;
public interface People {
void say();
}
package simpleFactory;
public class Man implements People{
public void say() {
System.out.println("男人");
}
}
package simpleFactory;
public class Woman implements People{
public void say() {
System.out.println("女人");
}
}
package simpleFactory;
public class SimpleFactory {
public static People create(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
Class class1 = Class.forName(className);
return (People) class1.newInstance();
}
}
package simpleFactory;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
People man = SimpleFactory.create("simpleFactory.Man");
People woman = SimpleFactory.create("simpleFactory.Woman");
man.say();
woman.say();
}
}

posted on 2017-09-01 14:18 杨军威 阅读(99) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜