当前位置: 首页 > news >正文

网站样式模板下载重庆的网站建设

网站样式模板下载,重庆的网站建设,工地包工接活十大平台,芜湖北京网站建设1. 设计模式原理说明 装饰模式#xff08;Decorator Pattern#xff09; 是一种结构型设计模式#xff0c;它允许在不改变对象接口的前提下#xff0c;动态地给对象增加额外的责任或功能。这种模式创建了一个装饰类#xff0c;用于包装原有的类#xff0c;并在保持类方法…1. 设计模式原理说明 装饰模式Decorator Pattern 是一种结构型设计模式它允许在不改变对象接口的前提下动态地给对象增加额外的责任或功能。这种模式创建了一个装饰类用于包装原有的类并在保持类方法签名完整性的前提下提供了额外的功能。 主要角色 Component组件定义了一个对象接口可以给这些对象动态地添加职责。ConcreteComponent具体组件定义了一个具体的对象也可以给这个对象添加一些责任。Decorator装饰器持有一个 Component 对象的实例并定义了一个与 Component 接口一致的接口。ConcreteDecorator具体装饰器向组件添加新的责任通常通过在其前后添加行为来实现。 2. UML 类图及解释 UML 类图 ----------------- ----------------- | Component | | ConcreteComponent| |-----------------| |-----------------| | operation(): void| | operation(): void| ----------------- -----------------^ ^| || |v v ----------------- | Decorator | |-----------------| | - component: Component | | setComponent(component: Component)| | operation(): void | -----------------^||v ----------------- ----------------- | ConcreteDecoratorA| | ConcreteDecoratorB| |-----------------| |-----------------| | operation(): void| | operation(): void| | addedBehavior(): void| | addedBehavior(): void| ----------------- ----------------- 类图解释 Component定义了一个对象接口可以给这些对象动态地添加职责。ConcreteComponent定义了一个具体的对象也可以给这个对象添加一些责任。Decorator持有一个 Component 对象的实例并定义了一个与 Component 接口一致的接口。装饰器可以在其前后添加行为。ConcreteDecoratorA 和 ConcreteDecoratorB具体装饰器向组件添加新的责任通常通过在其前后添加行为来实现。 3. 代码案例及逻辑详解 Java 代码案例 // 组件接口 interface Component {void operation(); }// 具体组件 class ConcreteComponent implements Component {Overridepublic void operation() {System.out.println(ConcreteComponent operation);} }// 装饰器 abstract class Decorator implements Component {protected Component component;public void setComponent(Component component) {this.component component;}Overridepublic void operation() {if (component ! null) {component.operation();}} }// 具体装饰器 A class ConcreteDecoratorA extends Decorator {Overridepublic void operation() {super.operation();addedBehavior();}private void addedBehavior() {System.out.println(ConcreteDecoratorA added behavior);} }// 具体装饰器 B class ConcreteDecoratorB extends Decorator {Overridepublic void operation() {super.operation();addedBehavior();}private void addedBehavior() {System.out.println(ConcreteDecoratorB added behavior);} }// 客户端 public class Client {public static void main(String[] args) {Component component new ConcreteComponent();ConcreteDecoratorA decoratorA new ConcreteDecoratorA();ConcreteDecoratorB decoratorB new ConcreteDecoratorB();decoratorA.setComponent(component);decoratorB.setComponent(decoratorA);decoratorB.operation();// 输出:// ConcreteComponent operation// ConcreteDecoratorA added behavior// ConcreteDecoratorB added behavior} } C 代码案例 #include iostream// 组件接口 class Component { public:virtual void operation() 0;virtual ~Component() {} };// 具体组件 class ConcreteComponent : public Component { public:void operation() override {std::cout ConcreteComponent operation std::endl;} };// 装饰器 class Decorator : public Component { protected:Component* component;public:void setComponent(Component* component) {this-component component;}void operation() override {if (component ! nullptr) {component-operation();}} };// 具体装饰器 A class ConcreteDecoratorA : public Decorator { public:void operation() override {Decorator::operation();addedBehavior();}void addedBehavior() {std::cout ConcreteDecoratorA added behavior std::endl;} };// 具体装饰器 B class ConcreteDecoratorB : public Decorator { public:void operation() override {Decorator::operation();addedBehavior();}void addedBehavior() {std::cout ConcreteDecoratorB added behavior std::endl;} };// 客户端 int main() {Component* component new ConcreteComponent();Decorator* decoratorA new ConcreteDecoratorA();Decorator* decoratorB new ConcreteDecoratorB();decoratorA-setComponent(component);decoratorB-setComponent(decoratorA);decoratorB-operation();// 输出:// ConcreteComponent operation// ConcreteDecoratorA added behavior// ConcreteDecoratorB added behaviordelete component;delete decoratorA;delete decoratorB;return 0; } Python 代码案例 from abc import ABC, abstractmethod# 组件接口 class Component(ABC):abstractmethoddef operation(self):pass# 具体组件 class ConcreteComponent(Component):def operation(self):print(ConcreteComponent operation)# 装饰器 class Decorator(Component):def __init__(self, component: Component):self._component componentdef operation(self):if self._component:self._component.operation()# 具体装饰器 A class ConcreteDecoratorA(Decorator):def operation(self):super().operation()self.added_behavior()def added_behavior(self):print(ConcreteDecoratorA added behavior)# 具体装饰器 B class ConcreteDecoratorB(Decorator):def operation(self):super().operation()self.added_behavior()def added_behavior(self):print(ConcreteDecoratorB added behavior)# 客户端 if __name__ __main__:component ConcreteComponent()decorator_a ConcreteDecoratorA(component)decorator_b ConcreteDecoratorB(decorator_a)decorator_b.operation()# 输出:# ConcreteComponent operation# ConcreteDecoratorA added behavior# ConcreteDecoratorB added behavior Go 代码案例 package mainimport fmt// 组件接口 type Component interface {Operation() }// 具体组件 type ConcreteComponent struct{}func (c *ConcreteComponent) Operation() {fmt.Println(ConcreteComponent operation) }// 装饰器 type Decorator struct {component Component }func (d *Decorator) SetComponent(component Component) {d.component component }func (d *Decorator) Operation() {if d.component ! nil {d.component.Operation()} }// 具体装饰器 A type ConcreteDecoratorA struct {Decorator }func (c *ConcreteDecoratorA) Operation() {c.Decorator.Operation()c.AddedBehavior() }func (c *ConcreteDecoratorA) AddedBehavior() {fmt.Println(ConcreteDecoratorA added behavior) }// 具体装饰器 B type ConcreteDecoratorB struct {Decorator }func (c *ConcreteDecoratorB) Operation() {c.Decorator.Operation()c.AddedBehavior() }func (c *ConcreteDecoratorB) AddedBehavior() {fmt.Println(ConcreteDecoratorB added behavior) }// 客户端 func main() {component : ConcreteComponent{}decoratorA : ConcreteDecoratorA{Decorator: Decorator{component: component}}decoratorB : ConcreteDecoratorB{Decorator: Decorator{component: decoratorA}}decoratorB.Operation()// 输出:// ConcreteComponent operation// ConcreteDecoratorA added behavior// ConcreteDecoratorB added behavior } 4. 总结 装饰模式 是一种结构型设计模式它允许在不改变对象接口的前提下动态地给对象增加额外的责任或功能。这种模式通过创建装饰类来包装原有类并在保持类方法签名完整性的前提下提供了额外的功能。 主要优点 灵活性可以在运行时动态地添加或删除对象的责任而不需要修改现有的代码。可扩展性可以很容易地通过组合不同的装饰器来实现复杂的功能。符合开闭原则可以在不修改现有代码的情况下通过添加新的装饰器来扩展功能。 主要缺点 增加了系统的复杂性装饰模式会引入许多小类这可能会使系统的设计变得更复杂。调试困难由于装饰器可以层层嵌套调试时可能难以跟踪到最终的行为。 适用场景 当需要在运行时动态地给对象添加功能时。当需要扩展类的功能但又不想使用继承的方式时。当系统需要提供多种可选功能且这些功能可以自由组合时。
http://www.hkea.cn/news/14519992/

相关文章:

  • 苏州企业网站建站wordpress插件 2017
  • 动漫网站建站目的视频网站建设要多少钱
  • 诸暨市住房和建设局网站个人网站设计模板下载
  • 设计师个人网站欣赏 中国旅游网站建设的背景
  • 做网站用lunx建设网站网页打不开
  • 学院网站建设时间控制变更申请表做网站常用的英文字体
  • 有个网站301什么网页游戏折扣充值平台
  • 了解网站建设海口模板建站公司
  • 从事网站类网站建设的个人简历在线填写电子版
  • html5做音乐网站广州做网页的公司
  • 网站开发语言有哪些盐城网站设计公司
  • 上海网站建设上海迈歌行情软件排行榜前十名
  • 美容加盟的网站建设近期网络营销的热点事件
  • wap网页开发天津放心站内优化seo
  • 网站建设步骤详解视频教程怎么创建网站教程
  • 企业网站模板带后台甘州区建设局网站
  • 深圳网站的优化wordpress小工具添加底部
  • 网站服务器如何做热备价wordpress ctf
  • 建设网站的网站空间生态网站模板
  • 注册公司多少钱收费网站设计seo
  • 域名抢注网站是怎么京东购物商城官网
  • 贵阳网站制作免费东莞网站建设必要性
  • 物流网个人网站建设anaconda可以做网站吗
  • 小型电商网站模板嘉兴网站制作公司
  • 站长工具pinghtml5移动端网站开发
  • 想建网站怎么做阿土伯网站做产品推广咋样
  • seo更新网站内容的注意事项莱州市网站
  • 国外网站引流如何做app界面生成器
  • 手机网站用什么空间宁波怎么做网站排名优化
  • 网站建设全包 广州奉节做网站