简体   繁体   English

它是装饰图案吗?

[英]Is it a decorator pattern?

I read this post, but I need an expert opinion on Employ example from this post: 我阅读了这篇文章,但是我需要这篇文章中有关Employ示例的专家意见:

Decorator Pattern by Examples 装饰图案实例

在此处输入图片说明

Can we decorate classes with data members as well? 我们也可以用数据成员修饰类吗?

Please also share your feedback on this last questions as well. 也请分享您对最后一个问题的反馈。

Whenever you implement and aggregate an interface, using the aggregated object to provide part of the implemented functionality, you will have a decorator pattern. 每当您实现并聚合接口时,使用聚合对象提供部分已实现的功能,便会获得装饰器模式。 The post you linked matches that description and therefore it is a decorator pattern. 您链接的帖子与该描述匹配,因此它是装饰器模式。

From Head First, Design Patterns : 从头开始,设计模式

The Decorator Pattern attaches additional responsibilities to an object dynamically. 装饰器模式动态地将附加职责附加到对象。 Decorators provide a flexible alternative to subclassing for extending functionality 装饰器为子类提供了灵活的替代方案,以扩展功能

NOTE : This definition is exactly the same that the one you can find in the GoF book: Design Patterns: Elements of Reusable Object-Oriented Software and the same that you find at the link you posted. 注意 :此定义与您在GoF书中可以找到的定义完全相同:“ 设计模式:可重用的面向对象软件的元素” ,也可以在发布的链接中找到。

With your EmployeeDecorator subclasses you are adding services to Employee being this "intermediate" class ( EmployeeDecorator ) a way to encapsulate delegation at a parent class so concrete subclases such as TeamMember and TeamLead do not replicate the delegation code. 使用EmployeeDecorator子类,您可以向Employee添加服务,使之成为此“中间”类( EmployeeDecorator ),这是一种在父类上封装委托的方法,因此,诸如TeamMemberTeamLead类的具体子类不会复制委托代码。

The usual purpose of the Decorator pattern is to wrap the implementations of individual methods of the decorated object with another implementation. Decorator模式的通常目的是将装饰对象的各个方法的实现与另一个实现包装在一起。

Both a decorated and an undecorated object are usually referred to through their common interface, so that the code which uses them doesn't need to know if the object is wrapped in a decorator or not and when it is wrapped which decorator(s) it uses. 装饰对象和未装饰对象通常都通过它们的公共接口来引用,因此使用它们的代码不需要知道对象是否包装在装饰器中以及何时包装在哪个装饰器中使用。 For that reason it is uncommon for a Decorator to add new public methods to the object which do not override any of the methods defined by the common interface. 因此,Decorator向对象添加不覆盖公共接口定义的任何方法的新公共方法并不常见。

Decorator is a behavioral pattern, ie it deals with the class methods that are defined by its interface and the last do not define any members, the pattern is irrelevant to them. 装饰器是一种行为模式,即它处理由其接口定义的类方法,而最后一个不定义任何成员,该模式与它们无关。

As per your question: 根据您的问题:

Can we decorate classes with data members as well? 我们也可以用数据成员修饰类吗?

Yes, we can decorate a class either with or without members. 是的,我们可以装饰一个带有或不带有成员的类。

However, if you plan using decoration as a substitution for inheritance and as a such have plans to access the members of the decorated class (in your example EmployeeImpl), that won't work for you, as a decorator actually decorates the interface rather than its implementation class. 但是,如果您计划使用装饰来代替继承,并且因此计划访问装饰的类的成员(在您的示例EmployeeImpl中),那么这对您不起作用,因为装饰器实际上是在装饰接口而不是它的实现类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM