设计模式之(抽象工厂)

View Code
public class FactoryMethod
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
      Factory.condition(2).operation("Hello");
    }

}

class Factory
{
    public static Api condition(int n)
    {
        if (n == 1)
        {
            return new ImpA();
        }
        else if(n==2)
        {
            return new ImpB();
        }
        return null;
    }
}

interface Api
{
    void operation(String a);
}

class ImpA implements Api
{

    @Override
    public void operation(String a)
    {
        System.out.println("ImpletmentA s=" + a);

    }

}

class ImpB implements Api
{

    @Override
    public void operation(String a)
    {
        System.out.println("ImplementB s=" + a);

    }
}

不暴漏具体的实现类,而是通过工厂方法来返回具体的实现类

接口:用于封装关系

posted @ 2013-04-09 19:02  尼姑哪里跑  阅读(135)  评论(0编辑  收藏  举报