抽象工厂模式

我的上一篇博文讲述了一个工厂模式的例子,在那个例子里面,只有一间公司的情况,但是,在现实中就不止一间保险公司,而且这些公司的保险类型往往是相同的。在这种情况下,我们应该怎样去实例化不同公司的工厂类呢?

 

其实方法是差不多的,只是它们的接口有点不一样,由于有两间不同的保险公司(例如中国平安Ping和中国人寿PICC),所以每种保险都会有两种类型,则对应两种保险产品的接口;对于工厂类也会不同,由于每种保险都有两种类型,所以工厂类的接口要有两种方法,分别对应两间公司的产品。下面是具体的代码:

 

中国平安Ping的产品接口:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AbstractFactory;

/**
 *
 * @author Administrator
 */
public interface AutoInsurance_Ping {
    abstract String getInsurInfo_Ping();
}
 
中国人寿PICC的产品接口:

 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AbstractFactory;

/**
 *
 * @author Administrator
 */
public interface AutoInsurance_PICC {
    abstract String getInsurInfo_PICC(); 
}


中国平安Ping的司机受伤险:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AbstractFactory;

/**
 *
 * @author Administrator
 */
public class BodyInjur_Ping implements AutoInsurance_Ping{
    private String description;

   public String getInsurInfo_Ping()
   {
	   description = " PingAn\n\nBody Injur Liability: \n\nBodily injury coverage pays for medical bills" +
	                 " lost wages, rehabilitation, treatment and/or" +
	                 " funeral costs for anyone injured or killed " +
	                 " by your car. Such coverage will also pay for" +
	                 " pain and suffering damages when a third " +
	                 " party successfully sues. ";
	   return description;
   }
}


中国人寿PICC的司机受伤险:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AbstractFactory;

/**
 *
 * @author Administrator
 */
public class BodyInjur_PICC implements AutoInsurance_PICC{
    private String description;

   public String getInsurInfo_PICC()
   {
	   description = " PICC\n\nBody Injur Liability: \n\nBodily injury coverage pays for medical bills" +
	                 " lost wages, rehabilitation, treatment and/or" +
	                 " funeral costs for anyone injured or killed " +
	                 " by your car. Such coverage will also pay for" +
	                 " pain and suffering damages when a third " +
	                 " party successfully sues. ";
	   return description;
   }
}


其他保险省略。

 

工厂类的接口:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package AbstractFactory;

/**
 *
 * @author Administrator
 */
public abstract class Producer {

    public abstract AutoInsurance_PICC get_PICC();

    public abstract AutoInsurance_Ping get_Ping();

    public static Producer getProducer(String type) {
        Producer pp = null;
        if (type.equalsIgnoreCase("人身伤亡")) {
            pp = new BodyInjurProducer();
        } else if (type.equalsIgnoreCase("碰撞")) {
            pp = new CollisionProducer();
        } else if (type.equalsIgnoreCase("司机受伤")) {
            pp = new PersonInjurProducer();
        } else if (type.equalsIgnoreCase("综合险")) {
            pp = new ComprehensiveProducer();
        } else if (type.equalsIgnoreCase("财产损失")) {
            pp = new PropertyProducer();
        }
        return pp;
    }
}


司机受伤险的工厂类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AbstractFactory;

/**
 *
 * @author Administrator
 */
public class BodyInjurProducer extends Producer{
    public AutoInsurance_PICC get_PICC()
    {
            return new BodyInjur_PICC();
    }
    public AutoInsurance_Ping get_Ping()
    {
        return new BodyInjur_Ping();
    }
}


其他工厂类省略。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值