码迷,mamicode.com
首页 > 编程语言 > 详细

Design Pattern :Factory and Reflect in java

时间:2015-02-11 23:16:31      阅读:242      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!