抽奖活动
新用户注册即参与抽奖,奖品为:1.优惠券,2.优酷会员,3.京东卡
public class FreeGoodsController{
public Result<?> send(AwardInfo awardInfo){
int awardType = awardInfo.getAwardType();
if(awardType == 1){
DiscountService discountService = new DiscountService();
discountService.send();
return Result.success();
}
if(awardType == 2){
YouKuService youkuService = new YouKuService;
youkuService.send();
return Result.success();
}
if(awardType == 3){
JDService jdService = new JDService();
jdService.send();
return Result.success();
}
return Result.failed();
}
}
简单工厂模式包含以下特征:
public interface IFreeGoods{
boolean send();
}
public class DiscountService implements IFreeGoods{
public boolean send(){
System.out.println("发放打折券");
return true;
}
}
public class YouKuService implements IFreeGoods{
public boolean send(){
System.out.println("发放优酷会员");
return true;
}
}
public class JDCardService implements IFreeGoods{
public boolean send(){
System.out.println("发放京东卡");
return true;
}
}
public class FreeGoodsServiceFactory{
public static IFreeGoods getInstance(Integer awardType){
if(awardType == 1){
return new DiscountService();
}
if(awardType == 2){
return new YouKuService();
}
if(awardType == 3){
return new JDCardService();
}
return null;
}
}
public class FreeGoodsController{
public Result<?> send(AwardInfo awardInfo){
int awardType = awardInfo.getAwardType();
IFreeGoods freeGoods = FreeGoodsFServiceFactory.getInstance(awardType);
if(freeGoods.send()){
return Result.success();
}else {
return Result.failed();
}
}
}
public interface IFreeGoods{
boolean send();
}
public DiscountFreeGoods implements IFreeGoods{
public boolean send(){
System.out.println("发放打折券");
return true;
}
}
public YouKuFreeGoods implements IFreeGoods{
public boolean send(){
System.out.println("发放优酷会员");
return true;
}
}
public JDFreeGoods implements IFreeGoods{
public boolean send(){
System.out.println("发放京东卡");
return true;
}
}
public interface FreeGoodsFactory{
IFreeGoods createFreeGoods();
}
public class DiscountFreeGoodsFactory implements FreeGoodsFacotry{
public IFreeGoods createFreeGoods(){
return new DiscountFreeGoods();
}
}
public class YouKuFreeGoodsFactory implements FreeGoodsFacotry{
public IFreeGoods createFreeGoods(){
return new YoukuFreeGoods();
}
}
public class JDFreeGoodsFactory implements FreeGoodsFacotry{
public IFreeGoods createFreeGoods(){
return new JDFreeGoods();
}
}
基于上述拉新人注册的需求进行扩展
之前是的奖品为:平台的打折券、优酷的月卡、京东的小礼品
现在的奖品为:平台的打折券、平台的会员卡,优酷的月卡打折券、优酷的月卡,京东的商品打折券、京东会员卡
由现在的需求可以看出,打折券、会员卡可以归为一类,奖品提供方可以归为另一类
打折券发放以后可以购买相关平台的指定商品
会员卡发放以后需要绑定相关平台的用户账户
由此引出了产品族以及产品等级结构的概念
产品等级结构即产品的继承结构,如一个抽象类是打折券,其子类有平台的打折券、优酷的打折券、京东的打折券;
产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品,比如平台的工厂生产平台的打折券,生产平台的会员卡
public interface IFreeGoods{
boolean send();
}
public abstract class DiscountFreeGoods extends IFreeGoods{
// 获取售卖的商品
List getSaleList();
// 计算价格
BigDecimal calc(BigDecimal price);
}
public abstract class VipCardFreeGoods extends IFreeGoods {
boolean bindAccount(Long phoneNumber);
}
public interface FreeGoodsFactory{
DiscountFreeGoods createDiscountFreeGoods();
VipCardFreeGoods createVipCardFreeGoods();
}
public class PlatformDiscountFreeGoods extends DiscountFreeGoods {
public boolean send(){
System.out.println("发放平台打折券");
}
public List getSaleList(){
// 这里返回平台售卖的商品
}
public BigDecimal calc(BigDecimal price){
// 这里计算平台打折券使用后的优惠价格
}
}
public class YoukuDiscountFreeGoods extends DiscountFreeGoods {
public boolean send(){
System.out.println("发放优酷打折券");
}
public List getSaleList(){
// 这里调用优酷的接口返回优酷售卖的商品
}
public BigDecimal calc(BigDecimal price){
// 这里调用用户的接口计算打折券使用后的优惠价格
}
}
public class JDDiscountFreeGoods extends DisCountFreeGoods {
public boolean send(){
System.out.println("发放京东打折券");
}
public List getSaleList(){
// 这里调用京东的接口返回京东售卖的商品
}
public BigDecimal calc(BigDecimal price){
// 这里调用京东的接口计算打折券使用后的优惠价格
}
}
public class PlatformVipCardFreeGoods extends VipCardFreeGoods {
public boolean send(){
System.out.println("发放京东会员卡");
}
public boolean bindAccount(Long phoneNumber) {
// 这里绑定平台账号
}
}
public class YouKuVipCardFreeGoods extends VipCardFreeGoods {
public boolean send(){
System.out.println("发放京东会员卡");
}
public boolean bindAccount(Long phoneNumber) {
// 这里调用优酷的接口,传手机号过去绑定用户账号
}
}
public class JDVipCardFreeGoods extends VipCardFreeGoods {
public boolean send(){
System.out.println("发放京东会员卡");
}
public boolean bindAccount(Long phoneNumber) {
// 这里调用京东的接口,传手机号过去绑定用户账号
}
}
public class PlatformFreeGoodsFactory implements FreeGoodsFactory {
public DiscountFreeGoods createFreeGoods(){
return new PlatformDiscountFreeGoods();
}
public VipCardFreeGoods createVipCardFreeGoods(){
return new PlatformVipCardFreeGoods();
}
}
public class YouKuFreeGoodsFactory implements FreeGoodsFactory {
public DiscountFreeGoods createFreeGoods(){
return new YoukuDiscountFreeGoods();
}
public VipCardFreeGoods createVipCardFreeGoods(){
return new YouKuVipCardFreeGoods();
}
}
public class JDFreeGoodsFactory implements FreeGoodsFactory {
public DiscountFreeGoods createFreeGoods(){
return new JDDiscountFreeGoods();
}
public VipCardFreeGoods createVipCardFreeGoods(){
return new JDVipCardFreeGoods();
}
}
public class Client {
private DiscountFreeGoods discountFreeGoods;
private VipCardFreeGoods vipCardFreeGoods;
public Client(FreeGoodsFactory factory){
this.discountFreeGoods = factory.createDiscountFreeGoods();
this.vipCardFreeGoods = factory.createVipCardFreeGoods();
}
// 省略 get和set
}