首页 > 其他分享 >[设计模式]设计模式之装饰器模式/包装模式【8】【待完善】

[设计模式]设计模式之装饰器模式/包装模式【8】【待完善】

时间:2023-03-09 17:13:29浏览次数:47  
标签:包装 Component 模式 public operation 设计模式 装饰

1 概述

当你在编写代码时,需要扩展一个类的功能,或者是当前类的接口不能满足需求时,你会选择怎么做?

  • 重新编写子类,通过继承加入功能?
  • 修改原有类的接口使其符合现有环境?

但你会发现这些改动是不完美的,它们并不符合面向对象的「开放-关闭原则」。

软件设计模式中有一个更好的答案——包装

今天介绍的四种设计模式都围绕着“包装”展开,那么首先先简单了解一下这些设计模式:

  • 装饰者模式Decorator Pattern):包装另一个对象,并提供额外的行为
  • 适配器模式Adapter Pattern):包装另一个对象,并提供不同的接口
  • 外观模式Facade Pattern):包装许多对象以简化它们的接口
  • 代理模式Proxy Pattern):包装另一个对象,并【控制】对它的访问

2 模式定义

  • 装饰器模式(Decorator Pattern):动态地对一个对象添加额外的职责/行为。就增加功能来说,装饰器模式比生成子类更为灵活。

3 主要角色

4 样例代码/通用代码

案例1

Component

//Component定义了一个对象接口,通过装饰类可以给这些对象动态地添加职责
public abstract class Component {
    public abstract void operation();
}

Decorator

/** Decorator,装饰抽象类,继承了Component
* 从外类来扩展Component类的功能,但对于Component来说,
* 是无需知道Decorator的存在的
*/
public abstract class Decorator extends Component {
		protected Component component;
//获取被装饰的对象
    public Component getComponent() {
	return component;
    }
//设置被装饰的对象
    public void setComponent(Component component) {
	this.component = component;
    }
    @Override
    public void operation() {
	if (component != null) {
    component.operation();
	}
    }
}

ConcreteDecoratorX(A/B/C)

//具体装饰类,可以为类加入新的行为
class ConcreteDecoratorA extends Decorator {
    private String addedState;

    @Override
    public void operation() {
	// 首先运行原Component的operation(),再执行本类的功能,如addedState,相当于对原Component进行了装饰
	super.operation();
	addedState = "A中的new state ";
	System.out.println(addedState + "具体装饰对象A的操作");
    }
}

class ConcreteDecoratorB extends Decorator {
    @Override
    public void operation() {
	super.operation();
	addedBehavior();
	System.out.println("具体装饰对象B的操作");
    }
    public void addedBehavior() {
	System.out.print("B中的新增行为 ");
    }
}

class ConcreteDecoratorC extends Decorator {
    @Override
    public void operation() {
	super.operation();
	System.out.println("C没有特殊行为 " + "具体装饰对象C的操作");
    }

}

ConcreteComponent

//ConcreteComponent是定义一个具体的对象,也可以给这个对象添加一些职责
public class ConcreteComponent extends Component {
    @Override
    public void operation() {
	System.out.println("具体对象的操作");
    }

}

DecoratorClient

//装饰模式客户端调用代码
public class DecoratorClient {
    public static void main(String[] args) {
	ConcreteComponent concreteComponent = new ConcreteComponent();
    //声明装饰类A、B、C
	ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
	ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB();
	ConcreteDecoratorC concreteDecoratorC = new ConcreteDecoratorC();
    //装饰的过程就像是层层包装,不断地装饰类包装对象,达到添加功能的目的
	concreteDecoratorA.setComponent(concreteComponent); //装饰类A包装对象
	concreteDecoratorB.setComponent(concreteDecoratorA); //装饰类B包装装饰类A(对象已经被包装在装饰类A中)
	concreteDecoratorC.setComponent(concreteDecoratorB); //装饰类C包装装饰类B
	concreteDecoratorC.operation();
    }
}

5 模式特点

优点

缺点

装饰器模式与代理模式的区别

装饰器模式与建造者模式的区别

比起建造者模式,建造者模式必须过程稳定;而装饰器模式过程动态的。

X 参考文献

标签:包装,Component,模式,public,operation,设计模式,装饰
From: https://www.cnblogs.com/johnnyzen/p/17199181.html

相关文章

  • IdentityServer4:简化(隐藏)模式
    IdentityServer4:简化(隐藏)模式这种模式使用于纯前端项目、微信开发等,比如前后端分离的项目得到纯前端。Api资源项目创建项目打开VS,创建一个“AspNetCoreWebApi”......
  • docker搭建Redis服务、主从复制、哨兵模式、Cluster模式
    本篇介绍使用docker搭建简单的Redis服务以及搭建Redis服务的三种模式主从复制、哨兵模式、Cluster模式1.搭建简单的Redis服务port7006#端口号cluster-enabledyes......
  • IdentityServer4:客户端模式
    IdentityServer4:客户端模式Api资源项目创建项目打开VS,创建一个“AspNetCoreWebApi”项目,名为:Dotnet.WebApi.Ids4.CustomerApi依赖包添加依赖包<PackageRe......
  • 创建型-原型模式
    定义 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。原型模式是一种对象创建型模式---百科。 通俗的说就是原型模式是一种创建型设计模式,指定某......
  • Spring设计模式——代理模式[手写实现JDK动态代理]
    代理模式代理模式(ProxyPattern):是指为其他对象提供一种代理,以控制对这个对象的访问。代理对象在客户端和目标对象之间起到中介作用,代理模式属于结构型设计模式。使用代......
  • 一Spring框架基础--2设计模式
    一Spring框架基础--2设计模式1.3spring用到的设计模式1.3.1责任链模式有多个对象,每个对象持有对下一个对象的引用,这样就会形成一条链,请求在这条链上传递,直到某一对象......
  • CSS 混合模式:mix-blend-mode: difference
    mix-blend-mode值可以是以下几个:mix-blend-mode:normal;mix-blend-mode:multiply;mix-blend-mode:screen;mix-blend-mode:overlay;mix-blend-mode:darken;mix......
  • Python单例模式
    单例模式(SingletonPattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上......
  • Spring设计模式——原型模式
    原型模式原型模式(PrototypePattern),是指原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。原型模式主要适用于以下场景:类初始化消耗资源较多使用new生......
  • 前端设计模式——中介者模式
    前端中介者模式(MediatorPattern),用于将对象之间的通信解耦并集中管理。它通过引入一个中介者对象,将对象之间的交互转移到中介者对象中,从而避免对象之间直接相互通信。在前......