mixin 和装饰器模式有什么区别?

发布于 2024-08-27 23:36:42 字数 226 浏览 8 评论 0原文

装饰器模式是类的运行时动态扩展。它动态地形成一种 is-a 关系。

在我得到 这个答案关于mixin和抽象类之间的区别。

The Decorator Pattern is dynamic extension-at-runtime of classes. It dynamically forms a is-a relationship.

I started to wonder if I was over-complicating my API by using the Decorator Pattern after I got this answer about the difference between a mixin and an abstract class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-09-03 23:36:42

当你向类中添加一些行为时,mixin 是合适的。例如,在集合类型的情况下进行枚举的能力。您可以根据需要将任意多组行为混合到您的类中。这是重用公共代码的好方法;你基本上可以免费获得很多方法。

另一方面,装饰器更像是一个偷偷摸摸的拦截器。它公开与目标对象相同的公共接口,包含一个将所有客户端调用委托给的目标对象;然而,它通过一些预处理和/或后处理来装饰调用。例如,如果我正在针对 MyCollection 编写代码,并且我希望记录对此类型的所有调用。我可以派生一个新的装饰器 MyCollectionWithTimeStampedLogging ,它们都派生自 ICollection 基础,以便它们对于客户端来说看起来相同。装饰器会将 ICollection 的实例作为构造函数参数并将调用委托给它。例如添加看起来像这样

public void Add( int item)
{
  _logger.log(String.Format( "{0} Add called with param {1}", DateTime.Now, item.ToString());
  _collection.Add(item);
  _logger.log(String.Format( "{0} Add completed with param {1}", DateTime.Now, item.ToString());
}

A mixin is apt when you're adding some behavior to your class. e.g. the ability to enumerate in case of a collection type. You can mixin as many sets of behavior into your class as you want. Its a nice way to reuse common code ; you basically get a bunch of methods for free.

A decorator on the other hand is more of a sneaky interceptor. It exposes the same public interface as the target object, contains a target object to which it delegates all client calls ; however it decorates the call with some pre and/or post processing. e.g. if I'm writing code against a MyCollection and I want all calls to this type to be logged. I could derive a new decorator MyCollectionWithTimeStampedLogging both deriving from an ICollection base so that they look identical to the client. The decorator would take an instance of ICollection as a ctor param and delegate calls to it. e.g. Add would look like this

public void Add( int item)
{
  _logger.log(String.Format( "{0} Add called with param {1}", DateTime.Now, item.ToString());
  _collection.Add(item);
  _logger.log(String.Format( "{0} Add completed with param {1}", DateTime.Now, item.ToString());
}
娜些时光,永不杰束 2024-09-03 23:36:42

使用装饰器模式时,通常是封装,而不是扩展(或混合)基类。通常,您这样做是因为您想要使用该类的功能,但想要包装对该类的调用,以便您可以在调用之前或之后执行一些额外的步骤。考虑一个 LoggerDecorator

public class LoggerDecorator implements SomeInterface {
      private SomeInterface delegate;
      public void someMethod() {
          LOGGER.debug("someMethod called");
          delegate.someMethod();
      }
}

您不想实际公开委托,但您想使用它的功能。因此,是否使用 mixins、扩展类或装饰实际上取决于您想要做什么。您是否正在扩展一个类并向其添加新功能?或者你正在包装/装饰班级?

When using the decorator pattern, you generally are encapsulating, rather than extending (or mixing in) the base class. Often you do this because you want to use the functionality of the class but you want to wrap the calls to it so you can do some extra steps before or after the call. Consider a LoggerDecorator

public class LoggerDecorator implements SomeInterface {
      private SomeInterface delegate;
      public void someMethod() {
          LOGGER.debug("someMethod called");
          delegate.someMethod();
      }
}

You don't want to actually expose the delegate but you want to use its functionality. So whether or not you use mixins, extends a class, or decorate really depends on what you're trying to do. Are you extending a class and adding new functionality to it? Or are you wrapping/decorating the class?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文