设计模式之建造者+策略模式

// Builder interface
interface Builder {
    //定义了三个抽象方法,用于设置属性
    void setProperty1(String property1);
    void setProperty2(int property2);
    void setProperty3(boolean property3);
    //用于构建Product对象
    Product build();
}

// Concrete builder implementing the Builder interface
class ConcreteBuilder implements Builder {
    //定义了三个属性,用于存储Product对象的属性
    private String property1;
    private int property2;
    private boolean property3;

    @Override
    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    @Override
    public void setProperty2(int property2) {
        this.property2 = property2;
    }

    @Override
    public void setProperty3(boolean property3) {
        this.property3 = property3;
    }

    @Override
    public Product build() {
        //创建Product对象并返回
        return new Product(property1, property2, property3);
    }
}

// Product class
class Product {
    //定义了三个属性,用于存储Product对象的属性
    private final String property1;
    private final int property2;
    private final boolean property3;

    public Product(String property1, int property2, boolean property3) {
        this.property1 = property1;
        this.property2 = property2;
        this.property3 = property3;
    }

    // Getters
    public String getProperty1() {
        return property1;
    }

    public int getProperty2() {
        return property2;
    }

    public boolean isProperty3() {
        return property3;
    }
}

// Strategy interface
interface Strategy {
    //定义了一个抽象方法,用于执行策略
    void execute(Product product);
}

// Concrete strategies implementing the Strategy interface
class ConcreteStrategy1 implements Strategy {
    @Override
    public void execute(Product product) {
        // Implementation of strategy 1
        //执行策略1
		System.out.println(product.getProperty1());
    }
}

class ConcreteStrategy2 implements Strategy {
    @Override
    public void execute(Product product) {
        // Implementation of strategy 2
        //执行策略2
    }
}

// Context class
class Context {
    //定义了一个Strategy类型的属性,用于存储策略
    private final Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void executeStrategy(Product product) {
        //执行策略
        strategy.execute(product);
    }
}

// Usage example
public class Main {
    public static void main(String[] args) {
        // Creating a product using the builder
        //使用ConcreteBuilder创建Product对象
        Builder builder = new ConcreteBuilder();
        builder.setProperty1("property1");
        builder.setProperty2(42);
        builder.setProperty3(true);
        Product product = builder.build();

        // Creating a context with a strategy
        //创建Context对象,并将ConcreteStrategy1作为参数传入
        Strategy strategy = new ConcreteStrategy1();
        Context context = new Context(strategy);

        // Executing the strategy on the product
        //执行策略
        context.executeStrategy(product);
    }
}

// Output:
// Implementation of strategy 1
//输出:执行策略1

/**

* 建造者模式的优点:

* 1. 建造者模式将复杂产品的创建过程封装起来,使得用户只需要知道所需建造者的类型即可,无须关心产品对象的具体创建过程,大大降低了系统的耦合度。

* 2. 建造者模式将对象的创建过程与对象本身分离开来,可以使用相同的创建过程来得到不同的产品对象,也可以通过相同的产品建造过程得到不同的产品,方便扩展。

* 3. 建造者模式通过组合不同的建造者和不同的零件,可以创建出更加复杂功能的产品对象。

*

* 策略模式的优点:

* 1. 策略模式将不同的算法封装成不同的策略类,使得算法可以互相替换,易于扩展和维护。

* 2. 策略模式将算法的使用与算法的实现分离开来,使得算法的使用方便修改和替换,也方便单元测试。

* 3. 策略模式避免了使用多重条件转移语句的情况,使得代码更加清晰,易于理解和维护。

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值