策略模式V / S装饰模式 [英] Strategy Pattern V/S Decorator Pattern

查看:105
本文介绍了策略模式V / S装饰模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一些设计模式。

I am learning some design patterns.

我刚刚遇到两种模式。


  1. 策略模式

  1. Strategy Pattern

装饰模式

根据实施,我发现他们都有些混乱。

According to implementation i found both of them some what confusing.

我的理解是: -


策略模式: -

Strategy Pattern :-

策略模式提供了可用于执行
特定操作或任务的几种算法。

Strategy pattern gives several algorithms that can be used to perform particular operation or task.

装饰模式: -

装饰模式向组件添加了一些功能。

Decorator pattern adds some functionality to component.

事实上,我发现策略模式和装饰器模式也可以互换使用。

In fact I have found that Strategy Pattern and Decorator Pattern can also be used interchangeably.

这里是链接:
何时和如何策略模式可以应用而不是装饰图案吗?

所以我有混淆:战略模式和装饰模式之间有什么区别? >

So i am having confusion :What is the difference between Strategy Pattern and Decorator Pattern?

应该使用策略模式和何时使用装饰模式?

解释两者之间的区别同样的例子。

Explain the difference between both with the same example.

推荐答案

策略模式允许您更改运行时使用的东西的实现

The strategy pattern allows you to change the implementation of something used at runtime.

装饰器模式允许您在运行时增加(或添加)现有功能与附加功能。

The decorator pattern allows you augment (or add to) existing functionality with additional functionality at run time.

主要区别在于更改 vs

The key difference is in the change vs augment

您链接到的问题还指出,消费者通过策略模式意识到存在不同的选项,而使用装饰器模式,消费者不会意识到附加功能nality。

In one of the questions you linked to it also points out that with the strategy pattern the consumer is aware that the different options exist, whereas with the decorator pattern the consumer would not be aware of the additional functionality.

作为一个例子,想象你正在写一些东西来排序元素集合。所以你编写一个界面 ISortingStrategy ,然后可以实现几种不同的排序策略 BubbleSortStrategy QuickSortStrategy RadixSortStrategy ,那么您的应用程序根据现有列表的某些标准选择最适合的策略来排序列表。因此,例如,如果列表中少于10个项目,我们将使用 RadixSortStrategy ,如果自上次排序以来,将少于10个项目添加到列表中,我们将使用 BubbleSortStrategy 否则我们将使用 QuickSortStrategy

As an example, imagine you are writing something to sort a collection of elements. So you write an interface ISortingStrategy you can then implement several different sorting strategies BubbleSortStrategy, QuickSortStrategy, RadixSortStrategy, then your application, based on some criteria of the existing list chooses the most appropriate strategy to use to sort the list. So for example if the list has fewer then 10 items we will use RadixSortStrategy, if fewer than 10 items have been added to the list since the last sort we will use BubbleSortStrategy otherwise we will use QuickSortStrategy.

我们正在更改类型在运行时(基于一些额外的信息更有效)。这是策略模式。

We are changing the type of sort at runtime (to be more efficient based on some extra information.) this is the strategy pattern.

现在想象有人要求我们提供一个日志,每个日志排序算法用于进行实际排序,并将排序限制为管理员用户。我们可以通过创建一个装饰器来添加这两个功能,这些装饰器可以增强任何 ISortingStrategy 。我们可以创建一个装饰器,它记录它被用来排序某些东西和装饰排序策略的类型。我们可以添加另一个装饰器,检查当前用户是否是管理员,然后再调用装饰排序策略。

Now imagine someone asks us to provide a log of how often each sorting algorithm is used to do an actual sort and to restrict sorting to admin users. We can add both of these pieces of functionality by creating a decorator which enhancers any ISortingStrategy. We could create a decorator which logs that it was used to sort something and the type of the decorated sorting strategy. And we could add another decorator that checked if the current user was an administrator before it called the decorated sorting strategy.

这里我们添加了新功能,策略使用装饰器,但不会排除核心排序功能(我们使用不同的策略来改变)

Here we are adding new functionality of any sorting strategy using the decorator, but are not swapping out the core sorting functionality (we used the different strategies to change that)

这里是一个例子,装饰器可能会看起来:

Here is an example of how the decorators might look:

public interface ISortingStrategy
{
    void Sort(IList<int> listToSort);
}

public class LoggingDecorator : ISortingStrategy
{
    private ISortingStrategy decorated;
    public LoggingDecorator(ISortingStrategy decorated)
    {
         this.decorated=decorated;
    }

    void Sort(IList<int> listToSort)
    { 
         Log("sorting using the strategy: " + decorated.ToString();
         decorated.Sort(listToSort);
    }
}

public class AuthorisingDecorator : ISortingStrategy
{
    private ISortingStrategy decorated;
    public AuthorisingDecorator(ISortingStrategy decorated)
    {
         this.decorated=decorated;
    }

    void Sort(IList<int> listToSort)
    { 
         if (CurrentUserIsAdministrator()
         {
             decorated.Sort(listToSort);
         }
         else
         {
             throw new UserNotAuthorizedException("Only administrators are allowed to sort");
         }
    }
}

这篇关于策略模式V / S装饰模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆