当前位置: 首页> 房产> 家装 > 建设工程合同主体有哪些_网站建设与管理心得_培训机构网站_舆情系统

建设工程合同主体有哪些_网站建设与管理心得_培训机构网站_舆情系统

时间:2025/9/9 23:53:27来源:https://blog.csdn.net/qq_37398465/article/details/142639928 浏览次数:0次
建设工程合同主体有哪些_网站建设与管理心得_培训机构网站_舆情系统

概述

装饰器模式(Decorator Pattern)是一种结构型设计模式,允许你在不改变对象接口的前提下,动态地给对象添加新功能。这个模式通常用于扩展类的功能。

基本结构

  1. 组件接口(Component):定义一个接口,包含基本的方法。
  2. 具体组件(ConcreteComponent):实现组件接口的类,是被装饰的对象。
  3. 装饰器(Decorator):实现组件接口,并持有一个组件的引用,通常会重写接口中的方法来添加新功能。
  4. 具体装饰器(ConcreteDecorator):继承装饰器类,增加具体的功能。

示例代码

假设我们有一个简单的文本组件,我们希望添加一些装饰,比如加粗和斜体。

// 组件接口
interface Text {String getContent();
}// 具体组件
class SimpleText implements Text {private String content;public SimpleText(String content) {this.content = content;}@Overridepublic String getContent() {return content;}
}// 装饰器
abstract class TextDecorator implements Text {protected Text decoratedText;public TextDecorator(Text decoratedText) {this.decoratedText = decoratedText;}@Overridepublic String getContent() {return decoratedText.getContent();}
}// 具体装饰器:加粗
class BoldDecorator extends TextDecorator {public BoldDecorator(Text decoratedText) {super(decoratedText);}@Overridepublic String getContent() {return "<b>" + decoratedText.getContent() + "</b>";}
}// 具体装饰器:斜体
class ItalicDecorator extends TextDecorator {public ItalicDecorator(Text decoratedText) {super(decoratedText);}@Overridepublic String getContent() {return "<i>" + decoratedText.getContent() + "</i>";}
}// 使用示例
public class Main {public static void main(String[] args) {Text simpleText = new SimpleText("Hello, World!");Text boldText = new BoldDecorator(simpleText);Text italicText = new ItalicDecorator(boldText);System.out.println(italicText.getContent());  // 输出:<i><b>Hello, World!</b></i>}
}

优点

  • 灵活性:可以按需组合装饰,增加或减少功能。
  • 遵循单一职责原则:每个装饰器负责一项功能。

缺点

  • 复杂性:增加了系统的复杂性,使用过多装饰器可能导致代码难以理解。
  • 性能问题:由于多层装饰,可能会引入一些性能开销。

希望这个介绍和示例对你理解装饰器模式有所帮助!如果你有任何问题,欢迎随时问我。

示例2 在线购物系统中的订单处理示例

一个在线购物系统中的订单处理示例。在这个例子中,我们将通过装饰器模式动态添加不同的费用,比如运费和税费。

结构

  1. 订单接口(Order):定义一个接口,表示订单的基本功能。
  2. 具体订单(BasicOrder):实现订单接口的基本订单。
  3. 装饰器(OrderDecorator):实现订单接口,持有一个订单的引用。
  4. 具体装饰器(ShippingDecorator, TaxDecorator):分别添加运费和税费。

示例代码

// 订单接口
interface Order {double getCost();
}// 具体订单
class BasicOrder implements Order {private double cost;public BasicOrder(double cost) {this.cost = cost;}@Overridepublic double getCost() {return cost;}
}// 装饰器
abstract class OrderDecorator implements Order {protected Order decoratedOrder;public OrderDecorator(Order decoratedOrder) {this.decoratedOrder = decoratedOrder;}@Overridepublic double getCost() {return decoratedOrder.getCost();}
}// 具体装饰器:运费
class ShippingDecorator extends OrderDecorator {private double shippingCost;public ShippingDecorator(Order decoratedOrder, double shippingCost) {super(decoratedOrder);this.shippingCost = shippingCost;}@Overridepublic double getCost() {return decoratedOrder.getCost() + shippingCost;}
}// 具体装饰器:税费
class TaxDecorator extends OrderDecorator {private double taxRate;public TaxDecorator(Order decoratedOrder, double taxRate) {super(decoratedOrder);this.taxRate = taxRate;}@Overridepublic double getCost() {return decoratedOrder.getCost() + (decoratedOrder.getCost() * taxRate);}
}// 使用示例
public class Main {public static void main(String[] args) {Order order = new BasicOrder(100.0);  // 基本订单// 添加运费order = new ShippingDecorator(order, 10.0);// 添加税费order = new TaxDecorator(order, 0.15);  // 15% 税率System.out.println("总费用: " + order.getCost());  // 输出:总费用: 126.5}
}

总结

在这个例子中,装饰器模式允许我们根据需要动态地为基本订单添加运费和税费,而无需修改基本订单的结构。这种灵活性在处理不同类型的订单时非常有用。如果你有其他问题或需要更深入的讨论,请告诉我!

关键字:建设工程合同主体有哪些_网站建设与管理心得_培训机构网站_舆情系统

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: