设计模式--策略模式

7 篇文章 0 订阅
7 篇文章 0 订阅

在面向对象程序设计过程中,我们常常说道利用多态这个特性,我觉得策略模式是最最原始的利用多态的一个设计模式了。
拿一个现实中的例子来澄清整个过程吧,比如两个人A,B同时去沃尔玛买一袋米,对于这袋米的销售价格,沃尔玛方面是这样规定的:

  • 普通用户:原价100元
  • 会员用户:原价基础上打八折

    所以针对不同的角色我们需要套用不同的价格算法。这时候我们引入策略模式就恰如其分了。
    首先我们定义一个大米类:

class Rice{
    private double price;
    private Discount discount;

    public void setPrice(double price) {
        this.price = price;
    }

    public void setDiscount(Discount discount) {
        this.discount = discount;
    }

    public double getPrice() {
        return discount.getfinalPrice(price);
    }
}

然后我们的定义了一个策略接口叫做Discount

interface Discount{
    public double getfinalPrice(double price);
}

然后在实现两个策略,分别对应普通客户和VIP客户。

class NormalCusDiscount implements Discount{

    @Override
    public double getfinalPrice(double price) {
        return price;
    }
}

class VIPCusDiscount implements Discount{

    @Override
    public double getfinalPrice(double price) {
        return price *0.8;
    }
}

然后我们在客户端调用的时候就可以对大米对象注入不同的策略对象。大米对象执行相同的逻辑将得到不同的结果。

public static void main(String[] args) {
        Rice rice = new Rice();
        rice.setPrice(100.0f);

        Discount discount = new VIPCusDiscount();
        rice.setDiscount(discount);
        System.out.println("the final price for VIP is: " +rice.getPrice());

        rice.setDiscount(new NormalCusDiscount());
        System.out.println("the final price for Normal cus is: " +rice.getPrice());

}

输出结果:

the final price for VIP is: 80.0
the final price for Normal cus is: 100.0

可以看到在Rice的getPrice中我们就是利用多态这个特性针对接口编程。利用不同的策略算出最后的价格的。
按住惯例我们再用C++语言和C语言来实现一遍吧。先看C++的实现吧。

class Discount{
public:
    virtual double getFinalPrice(double price) = 0;
    virtual ~Discount(){}
};

class Rice{
public:
    void setPrice(double price){
        this->price = price;
    }

    void setDiscount(Discount *discount){
        this->discount = discount;
    }

    double getPrice(){
        return discount->getFinalPrice(price);
    }
private:
    double price;
    Discount* discount;
};

class NormalCusDiscount : public Discount{
    double getFinalPrice(double price){
        return price *1.0;
    }
};

class VIPCusDiscount :public Discount{
    double getFinalPrice(double price){
        return price *0.8;
    }
};

int main(int argc, const char * argv[]) {

    Rice * rice = new Rice();
    rice->setPrice(100.0f);
    rice->setDiscount(new NormalCusDiscount());
    cout << "the price for normal Cus is :" << rice->getPrice()<< endl;
    rice->setDiscount(new VIPCusDiscount());
    cout << "the price for VIP Cus is :" << rice->getPrice()<< endl;

    return 0;
}

现在我们再来看一看C语言的实现吧

typedef struct _Discount{
    double (*getFinalPrice)(double price);
}Discount;

double getFinalPrice_Normal(double price){
    return price;
}

double getFinalPrice_VIP(double price){
    return price * 0.8;
}

Discount * newNormalCusDiscount(){
    Discount * discount = (Discount *)malloc(sizeof(Discount));
    discount->getFinalPrice =getFinalPrice_Normal;
    return discount;
}

Discount *newVIPCusDiscount(){
    Discount *discount = (Discount*)malloc(sizeof(Discount));
    discount->getFinalPrice =getFinalPrice_VIP;
    return discount;
}

typedef struct _Rice{
    double price;
    Discount *discount;
    void (*setPrice)(struct _Rice * rice,double price);
    void (*setDiscount)(struct _Rice * rice,Discount * discount);
    double (*getPrice)(struct _Rice * rice);
}Rice;


void setPrice(struct _Rice * rice,double price){
    if(rice == NULL)
        return;
    rice->price = price;
}
void setDiscount(struct _Rice * rice,Discount * discount){
    if(rice == NULL || discount == NULL)
        return;
    rice->discount = discount;
}
double getPrice(struct _Rice * rice){
    return rice->discount->getFinalPrice(rice->price);
}

Rice * newRice(){
    Rice * rice = (Rice *)malloc(sizeof(Rice));
    if (rice == NULL){
        return NULL;
    }
    memset(rice, 0, sizeof(Rice));
    rice->setPrice = setPrice;
    rice->setDiscount =setDiscount;
    rice->getPrice =getPrice;
    return rice;

}

int main(int argc, char** argv){

    Rice * rice = newRice();
    rice->setPrice(rice, 100.0f);
    rice->setDiscount(rice, newNormalCusDiscount());
    printf("the final price for Normal Cus is %.2f\n", rice->getPrice(rice));
    rice->setDiscount(rice,newVIPCusDiscount());
    printf("the final price for VIP Cus is %.2f\n", rice->getPrice(rice));
    return 0;
}

以上就是用Java,C++,C语言实现的策略模式了。可以说策略模式使用频率是相当高的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值