(1)设计模式-工厂模式

伪代码

//接口
interface A
{
  run();
}

//子类
public A1 implements A
{
  run(){"111"};
}

public A2 implements A
{
  run(){"222"};
}

public A3 implements A
{
  run(){"333"};
}
//工厂类,用来在调用时返回创建对象
Factory
{
    public A choose(String str)
    {
    if(str.equals("X"))
    {
        retrun new A1();
    }
    if(str.equals("Y"))
    {
        retrun new A2();
    }
    if(str.equals("X"))
    {
      retrun new A3();
    }
}

mian
{
    Factory factory =new Factory();
    //利用多态,输出子类方法
    A a= factory.choose("X");
    a.run();
}
原文地址:https://www.cnblogs.com/buchizaodian/p/9076390.html