我的上一篇博文讲述了一个工厂模式的例子,在那个例子里面,只有一间公司的情况,但是,在现实中就不止一间保险公司,而且这些公司的保险类型往往是相同的。在这种情况下,我们应该怎样去实例化不同公司的工厂类呢?
其实方法是差不多的,只是它们的接口有点不一样,由于有两间不同的保险公司(例如中国平安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();
}
}
其他工厂类省略。