设计模式 – 策略模式

news/2024/7/5 12:23:36

策略模式

策略模式(Strategy Pattern)是指定义了算法家族、分别封装起来,让它们之间可以互
相替换,此模式让算法的变化不会影响到使用算法的用户。

使用场景

1、假如系统中有很多类,而他们的区别仅仅在于他们的行为不同。
2、一个系统需要动态地在几种算法中选择一种。

案例

购物时有优惠活动,优惠策略会有很多种可能如:领取优惠券抵扣、返现促销、拼团优惠。下面我们用代码来模拟:

在这里插入图片描述

public interface PromotionStrategy {

    void doPromotion();

}

public class CashbackStrategy implements PromotionStrategy {

    public void doPromotion() {
        System.out.println("返现促销,返回的金额转到支付宝账号");
    }

}


public class CouponStrategy implements PromotionStrategy {
    public void doPromotion() {
        System.out.println("领取优惠券,购物价格价格直接减优惠券面值抵扣");
    }
}



public class EmptyStrategy implements PromotionStrategy {

    public void doPromotion() {

        System.out.println("无促销活动");
    }
}




public class GroupbuyStrategy implements PromotionStrategy {

    public void doPromotion() {

        System.out.println("拼团,满 20 人成团,全团享受团购价");

    }
}









public class PromotionActivity {

    private PromotionStrategy promotionStrategy;

    public PromotionActivity(PromotionStrategy promotionStrategy) {
        this.promotionStrategy = promotionStrategy;

    }

    public void execute() {
        promotionStrategy.doPromotion();
    }
}


public class StrategyTest {

    public static void main(String[] args) {
        PromotionActivity activity618 = new PromotionActivity(new CouponStrategy());
        PromotionActivity activity1111 = new PromotionActivity(new CashbackStrategy());
        activity618.execute();
        activity1111.execute();
    }

}

在这里插入图片描述


http://www.niftyadmin.cn/n/4556701.html

相关文章

文件默认权限umask掩码

umask命令 作用:用于显示、设置文件的缺省权限 格式:umask [-S]  -S表示以rwx形式显示新建文件缺省权限 系统的默认掩码是0022 文件创建时的默认权限 0666 - umask 目录创建时的默认权限 0777 - umask 所以创建文件的权限是 0666 - 0022 0644   …

谢谢啦~ 哪个高手帮我用C++解决个小问题啊

0)/*利用辗除法 y)<<endl;} y)<<endl; cout<<"他们的最小公倍数为: "<<Gbs(x y; cout<<"输入要求的两个数&#xff1a;"&#xff1b; cin>>x>>; cout<<"他们的最大公约数为: "<<Gcd(x b)…

设计模式实战,工厂+策略+单例 解决 if else 过多问题

场景 在支付选择中&#xff0c;可以选择京东白条、支付宝、微信、银联支付等。 public void payment(String payType) {if (payType.equals("JD")) {//京东支付} else if (payType.equals("Alibaba")) {//支付宝支付} else if (payType.equals("Tece…

学好C语言要注意哪些啊

C语言学得很浅 如果你是计算机专业的C语言学习 高校中的C语言开课初衷也是为了让学生更快的熟悉计算机软件中最底层的思想 个人感觉大学中的C语言 文件操作当年就被我老师忽略掉 学完指针和结构体之后应该就不会再讲了 我建议你首先学习C语言的编程思想 如果你所在为一所普通高…

设计模式 – 模板设计模式

模板设计模式 模板方法模式&#xff08;Template Method Pattern&#xff09;&#xff0c;又叫模板模式(Template Pattern)&#xff0c;在一个抽象类公开定义了执行它的方法的模板。它的子类可以按需要重写方法实现&#xff0c;但调用将以抽象类中定义的方式进行。 该模式的主…

关于C语言initgraph图象函数

参考资料&#xff1a;http://course.cug.edu.cn/cugFirst/Adv_program/C_ziliao/ctuxin1.htm 答案补充 是的 若没有驱动程序 当程序进行到intitgraph()语句时 在编译和链接时并没有将相应的驱动程序(*.BGI)装入到执行程序 都这个年代了 现在根本就不用 还用它编过几个挺成样的软…

设计模式 – 适配器模式

1 介绍 适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示&#xff0c;主要目的是兼容性&#xff0c;将一个类的接口转换成客户希望的另外一个接口&#xff0c;使得原本由于接口不兼容而不能一起工作的那些类能一起工作 目的&#xff1a;让原本接口…

C语言编辑进

PS&#xff1a;如果代码有问题 最后执行 然后build *.exe 那就在最上边工具条里点build->compile *.c 勾选buildminibar就会有上边那个工具条了如果还没找到 那在上边右键 执行如果没有 停止连接 连接 重新下个 在上边工具栏上有一排6个工具&#xff1a;编译 最后所产生的.e…