工厂类

来源:互联网 发布:python 驼峰转 编辑:程序博客网 时间:2024/05/17 06:25

1.简单工厂:

抽象类(接口)实现类工厂测试类

Demo:

    public interface Phoneimpl {    void call();    }       public class XiaoMi implements Phoneimpl{    @Override    public void call() {        // TODO Auto-generated method stub        System.out.println("小米为发烧而生");        }    }    public class HuaWei implements Phoneimpl{    @Override    public void call() {        // TODO Auto-generated method stub        System.out.println("华为改变世界");        }       }    public class Factory {    public static Phoneimpl getInstance(String phone){        Phoneimpl p = null;        if(phone.equals("HuaWei")){            p = new HuaWei();        }else if(phone.equals("XiaoMi")){            p = new XiaoMi();        }else{            System.out.println("no  phone");        }        return p;        }    }    public class Test {    public static void main(String[] args) {        Phoneimpl p = Factory.getInstance("XiaoMi");        p.call();    }    }
0 0
原创粉丝点击