zoukankan      html  css  js  c++  java
  • 工厂模式和抽象工厂模式

    工厂模式: 定义一个用于创建对象的接口,让他的子类决定实例化哪个类.使类的实例化延迟到子类.

    1.普通的工厂模式:

    public class GoodProduct implements Product {
    private String prodInfo = "Iam the good prod";
    public String getProductInfo() {
    // TODO Auto-generated method stub
    return this.prodInfo;
    }

    public void setProdInfo(String prodInfo) {
    // TODO Auto-generated method stub
    this.prodInfo = prodInfo;
    }
    }

    public class BadProduct implements Product {

    public String getProductInfo() {
    // TODO Auto-generated method stub
    return "Iam the bad product";
    }
    public void setProdInfo(String prodInfo) {
    // TODO Auto-generated method stub

    }
    }

    public class ProdCreator implements Creator {

    public <t extends Product> t getProd(Class<t> c) {
    // TODO Auto-generated method stub
    t ret = null;
    try {
    ret = (t) Class.forName(c.getName()).newInstance();
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    return ret;
    }
    }
    }

    2。多个工厂的工厂模式:

    public class BadProdCreator {
    public Product getProd(){
    return new BadProduct();
    }
    }

    goodProdCreator 同理。

    调用方法:

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ProdCreator pc = new ProdCreator();
    GoodProduct gp = pc.getProd(GoodProduct.class);
    System.out.println(gp.getProductInfo());
    BadProduct bp = pc.getProd(BadProduct.class);
    System.out.println(bp.getProductInfo());
    gp = SimpleCreator.getProd(GoodProduct.class);
    bp = SimpleCreator.getProd(BadProduct.class);
    System.out.println(gp.getProductInfo());
    System.out.println(bp.getProductInfo());
    BadProdCreator bpc = new BadProdCreator();
    System.out.println(bpc.getProd().getProductInfo());
    }

    抽象工厂模式:为创建一组相关或者相互依赖的对象提供接口,无需指定具体的类。

    public class GoodLargeProd extends AbstractGoodProd{

    public String getProductInfo() {
    // TODO Auto-generated method stub
    return "I am good large Prod";
    }

    public void setProdInfo(String prodInfo) {
    // TODO Auto-generated method stub
    }

    }

    public abstract class abstractCreator {
      public abstract Product getLargeProduct();
      public abstract Product getSmallProduct();
    }

    public class GoodProdCreator extends abstractCreator {

    @Override
    public Product getLargeProduct() {
    // TODO Auto-generated method stub
    return new GoodLargeProd();
    }

    @Override
    public Product getSmallProduct() {
    // TODO Auto-generated method stub
    return new GoodSmallProd();
    }
    }

    说明的一点是: 抽象工厂模式(以上面的例子来说)扩充产品的small,large等需要改abstractcreator 的接口,但加个badprod等就比较方便。

    用工厂模式的creator 也可以创建对象。

    public class GoodProdNormalCreator implements Creator {

    public <t extends Product> t getProd(Class<t> c) {
    // TODO Auto-generated method stub
    t ret = null;
    try {
    ret = (t)Class.forName(c.getName()).newInstance();
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    return ret;
    }
    }

    }

     这样可以创建任何的prod的之类(报错多级子类)。

  • 相关阅读:
    Eclipse 软件 Java 解决:出现的editor does not contain a main type错误框 问题
    Oracle SQL高级编程——分析函数(窗口函数)全面讲解
    瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示
    spring jdbctemplate调用存储过程,返回list对象
    利用jquery操作Radio方法小结
    Oracle临时表
    JdbcUtil
    整理oracle 树形查询
    Js:消息弹出框、获取时间区间、时间格式、easyui datebox 自定义校验、表单数据转化json、控制两个日期不能只填一个
    Java对象之间的深度复制拷贝
  • 原文地址:https://www.cnblogs.com/thinkqin/p/3807155.html
Copyright © 2011-2022 走看看