首页 > 编程语言 > 详细

Design Pattern :Factory and Reflect in java

时间:2015-02-11 23:15:47      阅读:365      评论:0      收藏:0      [点我收藏+]

interface page
{
    void Render();
}
class pageA implements page
{
    @Override
    public void Render() {
         System.out.println("I am " + this.getClass().getName());
    }
}
class pageB implements page
{
    @Override
    public void Render() {
         System.out.println("I am " + this.getClass().getName());
    }
}
public class factorypatterndemo {
    page getPage(String pageName)
    {
        try {
                @SuppressWarnings("rawtypes")
                Class cls = Class.forName(pageName);
                try {
                    return (page)cls.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args)
    {
         System.out.println(pageA.class.getName());
         factorypatterndemo f = new factorypatterndemo();
         f.getPage("pageA").Render();
         f.getPage("pageB").Render();
    }
}

Design Pattern :Factory and Reflect in java

原文:http://www.cnblogs.com/huaxiaoyao/p/4287012.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!