结构型模式之装饰模式(Decorator)

本文介绍装饰模式的概念、结构及优势,并通过一个员工考核的例子详细说明如何使用装饰模式来动态增加对象的功能。

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

装饰模式(Decorator):也叫包装器,GOF把装饰模式定义为:动态的给对象增加一些额外的职责。可以理解为保持原有的功能不变并进行拓展。

装饰器结构:


Component:定义一个对象接口,可以动态的给这些对象增加职责。
ConcreteComponent:定义一个对象,可以给这个对象增加一些职责;
Decorator:维持一个指向Component对象的指针,并定义一个与Component接口一致的接口;

ConcreteDecorator:向组件添加职责。

装饰模式是为了已有功能动态的增加更多功能的一种方式。

实例:每到年底,公司都会进行员工考核,以市场部某员工为例,考察其上一年的考核排名即分数情况。
考核抽象类

<span style="font-size:18px;">public abstract class ExamineeRecord
{
	public abstract String record();
	public abstract String signature(String name);
}</span>

员工考核类

<span style="font-size:18px;">package com.test.decorator;

public class YearExamineeRecord extends ExamineeRecord
{

	public  String record()
	{
		String a1 ="员工:jerry,部门:市场部";
		String a2="考核情况:平均分:3.5,年度考核分:3.6";
		String a3=".......";
		System.out.println("员工:jerry,部门:市场部");
		System.out.println("考核情况:平均分:3.5,年度考核分:3.6");
		System.out.println(".......");
		return a1+a2+a3;
	}
	public String signature(String name)
	{
		String a="评价人:";
		System.out.println(a+name);
		return a+name;
	}
}</span>

装饰考核报表

<span style="font-size:18px;">package com.test.decorator;

public class Decorator extends ExamineeRecord
{
	private ExamineeRecord er;
	public Decorator(ExamineeRecord er)
	{
		this.er = er; 
	}
	public String record()
	{
		return this.er.record();
	}
	public String signature(String name)
	{
		return  this.er.signature(name);
	}
}</span>

最高考核分数装饰

<span style="font-size:18px;">package com.test.decorator;

public class TipTopPointDecorator extends Decorator
{
	public TipTopPointDecorator(ExamineeRecord er)
	{
		super(er);
	}
	private String reportHighRecord()
	{
		String a="此次考核最高平均分:4.2,年度考核平均分:4.1";
		System.out.println("此次考核最高平均分:4.2,年度考核平均分:4.1");
		return a;
	}
	public String record()
	{
		String a=this.reportHighRecord();
		String b =super.record();
		return a+b;
	}
}</span>

排名装饰类

<span style="font-size:18px;">package com.test.decorator;

public class OrderDecorator extends Decorator
{
	public OrderDecorator(ExamineeRecord er)
	{
		super(er);
	}
	public String recordOrder()
	{
		String a="部门排名:第7";
		System.out.println("部门排名:第7");
		return a;
	}
	public String record()
	{
		String a =this.recordOrder();
		String b =super.record();
		return a+b;
	}
}</span>

测试类

<span style="font-size:18px;">package com.test.decorator;

public class TestDecorator
{
	public static void main(String[] args)
	{
		ExamineeRecord er;
		er=new YearExamineeRecord();
		er=new OrderDecorator(er);
		er=new TipTopPointDecorator(er);
		er.record();
		er.signature("tom");
	}
}
</span>
测试结果:

此次考核最高平均分:4.2,年度考核平均分:4.1
部门排名:第7
员工:jerry,部门:市场部
考核情况:平均分:3.5,年度考核分:3.6
.......
评价人:tom

优势:
通过在类中移除装饰功能,从而使其更加精简;
提供比单独的继承、组合更强大、更灵活的功能;
区别类的核心职责和装饰职责,降低了类的耦合度;
无需修改组件,即可添加面向该具体组件的具体的装饰功能。
应用场合:
当程序需要扩展现有类的功能或职责时;
当类需要添加灵活性较高的功能时;
单独使用继承或组合存在维护复杂、代码过多等问题时;

Struts与装饰模式
在Struts框架中,继承于ActionConfig的ActionMapping使业务逻辑分为几种类型,当某一个Http请求过来时, ActionServlet将于ActionMapping的相关目录中通过path属性方便快捷的实现所需要内容的目标。
并且,“全部的ActionMapping对象都存放在一个集合中,为了更好的使用Action,ActionMapping对象被用来作为Action对象的修饰符,它将一个或者多个URL交给Action,同时根据哪个URL被调用来传递不同配置信息给Action。即ActionMapping可以动态的赋予一个特定的Action对象一些附加的要求和新功能,这些就是装饰模式的要求。该设计模式相对于子类话方法扩展功能而言,具有更高的灵活性。”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值