策略模式VS依赖注入 [英] Strategy Pattern vs Dependency Injection

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

问题描述

是怎样的策略模式不同,那么依赖注入

How is strategy pattern is different then dependency injection?

即低于是你可以用策略模式怎么办?

ie below is what you can do with Strategy pattern:

class Foo{
   private readonly ISortAlgo _sortAlgo;

  public Foo(ISortAlgo sortAlgo)
  {
     _sortAlgo = sortAlgo;
  }

  public void Sort()
  {
    _sortAlgo.sort();
  }

}



与DI,你可以做同样的基本上你可以有构造函数,二传手,接口等注射液。并且,将得到为策略模式相同的效果。据我所知,DI也设置的其他原则,如松耦合,可测试性,布线等。

with DI you can do the same, essentially you can have constructor, setter, interface etc. injection. and it would give the same effect as Strategy pattern. I am aware that DI is also set of other principles, such as loose coupling, testability, wiring etc.

在实施方面我没有看到太大的区别。

In terms of implementation i dont see much difference.

持何策略模式和DI?

推荐答案

之间的区别首先,的依赖注入的不仅的构造函数注入的作为方式注入,也的属性方法注入环境背景

First, dependency injection has not only constructor injection as method to inject, but also property, method injection and ambient context.

二, stategy 的定义的行为的,因此客户可以选择特殊的一个这对他的需求相匹配。虽然的依赖注入的工作与外部依赖抽象。

Second, stategy defines behaviour, so client could select special one that matches on his needs. While dependency injection works with abstraction of external dependencies.

这篇关于策略模式VS依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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