设计模式之(抽象工厂)

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);

    }
}

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

接口:用于封装关系

原文地址:https://www.cnblogs.com/anbylau2130/p/3010785.html