Java 策略模式 和工厂模式的结合代替if

本文介绍了如何利用策略模式来替代传统的if-else或switch语句,以提高代码的可读性和可维护性。通过创建不同的数据库策略实现类,如MysqlStrategy、ClickHouseIStrategy和HiveIStrategy,实现了特定数据库的初始化方法。DbFactory类作为工厂模式的一部分,根据输入参数返回相应的策略实例,简化了客户端代码,并遵循开闭原则。然而,策略模式也存在缺点,如需要客户端了解所有策略并选择合适的策略,且策略过多会增加维护难度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 策略模式代替if else if

2.优点:

1.策略模式符合开闭原则
2.避免使用多重条件转义语句   比如: if  else if      switch  语句
3. 使用策略模式可以提高算法的保密性和安全性

3. 缺点

1.客户端需要知道所有的策略 并且知道使用那一个策略
2.策略过多会增加 维护难度。

4. 类图

在这里插入图片描述

5.实现代码:

5.1 DbIStrategy

package com.wudl.design.trategy.upgrade;
/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:29
 * @description:
 * @modified By:
 * @version: 1.0
 */
public interface DbIStrategy {
    /**
     * 初始化数据库
     */
    void initDb();
}

MysqlStrategy

package com.wudl.design.trategy.upgrade;


/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:23
 * @description: MYSQL  的策略实现
 * @modified By:
 * @version: 1.0
 */

public class MysqlStrategy implements DbIStrategy {
    public void initDb() {
        System.out.println("mysql 的策略类");
    }
}

package com.wudl.design.trategy.upgrade;

/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:42
 * @description:
 * @modified By:
 * @version: 1.0
 */

public class ClickHouseIStrategy implements DbIStrategy{
    public void initDb() {
        System.out.println("Click House 的实现策略");
    }
}

package com.wudl.design.trategy.upgrade;


/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:24
 * @description:
 * @modified By:
 * @version: 1.0
 */

public class HiveIStrategy implements DbIStrategy {
    public void initDb() {
        System.out.println("hive 的策略实现----------------------->");
    }
}

package com.wudl.design.trategy.upgrade;

/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:31
 * @description:
 * @modified By:
 * @version: 1.0
 */

public class OpertorActivity {
    private DbIStrategy iStrategy;

    public OpertorActivity(DbIStrategy iStrategy) {
        this.iStrategy = iStrategy;
    }

    public void execute() {
        iStrategy.initDb();
    }
}

package com.wudl.design.trategy.upgrade;

import java.util.HashMap;
import java.util.Map;

/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:32
 * @description:
 * @modified By:
 * @version: 1.0
 */

public class DbFactory extends Test {
    private static Map<String, DbIStrategy> map = new HashMap<String, DbIStrategy>();

    private static final DbIStrategy EMPTY =  new MysqlStrategy();

    static {
        map.put("mysql", new MysqlStrategy());
        map.put("hive",new HiveIStrategy());
        map.put("clickhouse",new HiveIStrategy());
    }

    public static DbIStrategy getPromotionStrategy(String promotionKey){
        DbIStrategy strategy = map.get(promotionKey);
        return strategy == null ? EMPTY : strategy;
    }
}

package com.wudl.design.trategy.upgrade;

/**
 * @author :wudl
 * @date :Created in 2022-04-13 0:41
 * @description:
 * @modified By:
 * @version: 1.0
 */

public class Test  {
    public static void main(String[] args) {

            String str = "Hvie";
        DbIStrategy promotionStrategy = DbFactory.getPromotionStrategy(str);
        promotionStrategy.initDb();

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值