Java 设计模式(三)工厂模式

一、简介

工厂模式细分下有抽象工厂、抽象方法、简单工厂等等(这个不算一种设计模式,但也经常使用),它们的共同点就在于统一管理对象的创建,这样在修改时只需修改工厂类或工厂方法即可。

二、代码示例

要求(不要在意与上一节要求相同):
商家做活动要求有多种活动方案作为备选使用:
1、满30元减5元,满50元减10元,满100元减30元
2、满30元打9.5折,满50元打9折,满100元打8折
3、…..更多活动待扩展

商品接口与方案接口及方案类型常数:

//商品接口
public interface IProduct {
    int getPrice();
    String getDescription();
}
//方案接口
public interface IScheme {
    //对价格进行处理
    int processPrice(int price);
    //对商品描述添加活动标记
    String processDescription(String desc);
}
//方案类别常数
public enum SchemeType {
    DISCOUNT,DECREACE;
}

商品实现类(仅包含构造函数和get方法):

public class ProductImpl implements IProduct{
    private String description;
    private int price;
    public ProductImpl(String description,int price) {
        this.description = description;
        this.price = price;
    }
    public String getDescription(){
        return this.description;
    }
    public int getPrice(){
        return this.price;
    }
}

满减活动方案:

//活动方案:满10减5元,满50减10元,满100减30元
public class DecreaseScheme implements IScheme {
    @Override
    //处理价格
    public int processPrice(int price) {
        if(10<=price&&price<50){
            price -= 5;
        }else if(price<100){
            price -= 10;
        }else{
            price -= 30;
        }
        return price;
    }
    @Override
    //添加活动标记
    public String processDescription(String desc) {
        return desc+"--满减活动商品";
    }
}

折扣活动方案:

//活动方案:满10元打9.5折,满50元打9折,满100元打8折
public class DiscountScheme implements IScheme{
    @Override
    //处理价格
    public int processPrice(int price) {
        if(10<=price&&price<50){
            price *= 0.95;
        }else if(price<100){
            price *= 0.9;
        }else{
            price *= 0.8;
        }
        return price;
    }
    @Override
    //添加活动标记
    public String processDescription(String desc) {
        return desc+"--折扣活动商品";
    }
}

方案工厂:

public class SchemeFactory {
    //根据方案类别类型返回不同的方案
    public static IScheme newScheme(SchemeType type){
        switch (type) {
        case DISCOUNT:
            return new DiscountScheme();
        case DECREACE:
            return new DecreaseScheme();
        default:
            return null;
        }
    }
}

活动商品类(这里使用装饰模式装饰普通商品)

//活动商品类
public class ActiveProduct implements IProduct{
    private IScheme scheme;
    private  IProduct product;
    public ActiveProduct(IProduct product,SchemeType type) {
        this.product = product;
        this.scheme = SchemeFactory.newScheme(type);
    }
    //获得处理后的价格
    public int getPrice() {
        return scheme.processPrice(product.getPrice());
    }
    //获得处理后的商品描述
    public String getDescription(){
        return scheme.processDescription(product.getDescription());
    }
}

场景模拟:

public class FactoryTest {
    //获取随机数的工具
    static Random random = new Random(); 
    public static void main(String[] args) {
        IProduct product;
        //50个测试用例
        for(int i=0;i<50;i++){
            //初始化普通商品
            product = new ProductImpl(getRandomDesc(), getRandomPrice());
            //通过活动方案进行商品包装
            product = new ActiveProduct(product,getRandomType());
            System.out.println("----------------------");
            System.out.println("desc:"+product.getDescription());
            System.out.println("price:"+product.getPrice());
        }
    }
    //获得随机商品名
    static String getRandomDesc(){
        return (char)(random.nextInt(26)+65)+"";
    }
    //获得随机商品价格
    static int getRandomPrice(){
        return random.nextInt(150); 
    }
    //获得随机活动类型
    static SchemeType getRandomType(){
        return random.nextInt(10)>5?SchemeType.DECREACE:SchemeType.DISCOUNT;
    }
}

结果打印:

----------------------
desc:R--折扣活动商品
price:55
----------------------
desc:R--满减活动商品
price:64
----------------------
desc:Z--折扣活动商品
price:61
----------------------
desc:L--折扣活动商品
price:47
----------------------
desc:C--满减活动商品
price:67

分析:
整个示例通过方案工厂产生不同的方案,由活动商品类对普通商品进行包装,以实现活动方案的灵活切换。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值