标签: solid-principles

违反单一责任原则的最佳例子是什么?

我正在寻找一些违反单一责任原则的代码示例.不要向我展示鲍勃叔叔的书籍或网站上的任何例子,因为这些都是在互联网上贴满的,就像这样:

interface Modem
{
    public void dial(String pno);
    public void hangup();
    public void send(char c);
    public char recv();
}
Run Code Online (Sandbox Code Playgroud)

single-responsibility-principle solid-principles

3
推荐指数
1
解决办法
3503
查看次数

这违反了SOLID原则吗?

我的项目中有这样的东西,项目已经完成了(它正在工作)我只想知道它是否适用于SOLID原则

static public class Tools
{
    static public GetProduct(this id){...}

    static public GetProductCategory(this id){...}

    static public GetUser(this id){...}

    // I also have here methods like IsStringNull ...
    // IsNull IsFalse, lots of stuff, everything static
}
Run Code Online (Sandbox Code Playgroud)

用法是这样的

var UserThatCreatedCategoryForThisProduct = 
      prodId.GetProduct().CategoryId.GetProductCategory().Creator.GetUser();
Run Code Online (Sandbox Code Playgroud)

我知道这是显而易见的,它是违反了SRP,但这个类是静态的,它包含了彼此独立的一个静态方法,它是一种过相同的,如果我会为每个方法创建一个静态类

c# oop solid-principles

3
推荐指数
1
解决办法
1655
查看次数

使用C#接口,同时最大化代码重用

所以说我有一个C#类,

class Foo : Bar, IBar, IBar2
{

}
Run Code Online (Sandbox Code Playgroud)

...其中Bar是一个类,IWhatever和IFine是接口.我打算在多个类中使用类似的IWhatever和IFine实现 - 我可以看到封装这些代码并在所有类中重用它的唯一理智方法是创建一个类,无论继承自IW和I的继承自IFine和使它们成为实现这些接口的类的成员,然后在从接口实现的成员中调用它们的成员,如下所示:

class Foo : Bar, IWhatever, IFine
{
   IWhatever mWhatever;
   IFine mFine;
   Foo()
   {
      mWhatever = new Whatever();
      mFine = new Fine();
   }
   // from IWhatever
   void Kick()
   {
      mWhatever.Kick();
   }
   // from IFine
   void Punch()
   {
      mFine.Punch();
   }
}
Run Code Online (Sandbox Code Playgroud)

我做得对吗?有没有更好的办法?

c# inheritance interface solid-principles

3
推荐指数
1
解决办法
1864
查看次数

依赖倒置原则以及放置接口的位置

我正在asp.net中构建一个简单的MVC应用程序.我想遵循依赖倒置原则,我不知道我是否做得对.

我目前正在研究身份验证系统.我有一个AccountController,里面使用Authenticator服务.Authenticator服务通过构造函数注入注入控制器.

public class AccountController : Controller
{
    private IAuthenticator _authenticator;

    public AccountController(IAuthenticator authenticator)
    {
        _authenticator = authenticator;
    }

    //
    // POST: /Account/Login

    [HttpPost]
    public ActionResult Login(LoginModel model, string redirectToUrl = null)
    {

     ...

    }
Run Code Online (Sandbox Code Playgroud)

文件的结构是这样的:

在此输入图像描述

但我想如果我想要完全颠倒控制器及其依赖关系之间的重要性,我将不得不将身份验证服务的接口移到控制器旁边.像这样的东西:

在此输入图像描述

这样,客户端 - 控制器 - 以及服务的抽象将位于同一名称空间中.因此,服务接口的更改将来自客户端,并将传播到服务实现.而不是以前的方式,服务中发生的变化传播到客户端.依赖项是Inverted - 服务依赖于客户端.

当客户端和服务在不同的程序集中时,我可以看到这样做的好处,但是我不确定如果在同一个程序集中我应该这样做.

如果我这样做是正确的,我是否应该使用第一个文件结构或第二个文件结构,请告诉我.

谢谢,阿西尔

c# asp.net-mvc inversion-of-control solid-principles dependency-inversion

3
推荐指数
1
解决办法
713
查看次数

这是否解决了Liskov Substitution方形矩形违规问题?

我对SOLID设计原则很陌生.我理解的一个问题是Liskov Substition Principle违规的"方形矩形"示例.为什么Square的高度/宽度设置器会覆盖矩形的高度?当存在多态性时,这不正是导致问题的原因吗?

不删除这个解决问题?

class Rectangle
{
    public /*virtual*/ double Height { get; set; }
    public /*virtual*/ double Width { get; set; }
    public double Area() { return Height * Width; }
}

class Square : Rectangle
{
    double _width; 
    double _height;
    public /*override*/ double Height
    {
        get
        {
            return _height;
        }
        set
        {
            _height = _width = value;
        }
    }
    public /*override*/ double Width
    {
        get
        {
            return _width;
        }
        set
        {
            _width = _height = value;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

oop liskov-substitution-principle solid-principles

3
推荐指数
1
解决办法
2130
查看次数

如何在完整的OO应用程序中处理依赖注入

我在课堂设计上陷入两难境地.我正在尽力尊重SOLID原则,但我不知道如何处理依赖注入.

这是我的困境:

  • 我读到在类中实例化对象以避免引入依赖是一种不好的做法.那么在完整对象应用程序中应该在哪里创建依赖项呢?在一个只负责依赖实例化的特殊对象中?如果是,该对象的名称是什么以及如何定义它?这就是我们所说的"控制器"吗?
  • 这个"控制器",什么是单元测试的正确方法呢?我们应该进行单元测试吗?
  • 在完整的POO应用程序中,如何避免在类之间传递我们的对象(通常是相同的)?例如,一个DB对象,Log,......这样,我们冒险让构造函数有很多参数,不是吗?

为了说明我的困境,我试图创建一个用例.

我想创建一个脚本(我在下面部分实现),生成一个文件并打印另一个文件:

<?php

/**
 * Generate a file, add it to the queue and print the next one
 */
class Script
    public function run() {
        #Generate file
        $fileComputor = new FileComputer(...);
        $file = $fileComputor->compute();

        #Instantiate dependencies for printing
        $db = new Db(new PdoAdapter());
        $printerDriver = new Driver(new HttpRequest(new CurlAdapter()));
        $log = new Log($db);
        $queue = new Queue($db);
        $statsUpdater = new StatsUpdater($db);
        $monitor = new Monitor(new StatsdDriver(new SocketAdapter()));

        #Add generated file to the queue
        $queueModel->push($file);

        #Print …
Run Code Online (Sandbox Code Playgroud)

php oop dependency-injection solid-principles

3
推荐指数
1
解决办法
109
查看次数

单一责任与封装

我想更多地了解单一责任。如果我尝试代表可以添加,删除,更新,检索的客户,那么过去我的客户类将具有所有的add,remove,update和get方法。这有助于将现实世界中客户的行为归为一类。我只负责将客户类别分为AddCustomer类,删除客户类别,更新客户类别和获取客户类别,甚至获取所有客户类别?

c# encapsulation single-responsibility-principle solid-principles

3
推荐指数
1
解决办法
690
查看次数

带接口的架构/设计(重构帮助)

我需要你帮我做一个我不满意的设计.该应用程序正在消费RSS新闻(文章的RSS,以及每篇文章的评论RSS).

我创建了一个名为IDataService的接口,它提供了数据提供程序的基本行为.

public interface IDataService
{
    Task<List<Item>> GetItemsAsync(string url, IItemsParser parser);
    Task<List<Comment>> GetItemCommentsAsync(string url, ICommentsParser parser);
}
Run Code Online (Sandbox Code Playgroud)

如您所见,每个函数都将web服务url(在我的情况下为RSS feed url)和一些知道如何处理数据的解析器的接口作为参数.

这些是两个解析的接口:

public interface IItemsParser
{
    List<Item> ParseRawData(string rawData);
}

public interface ICommentsParser
{
    List<Comment> ParseRawData(string rawData);
}
Run Code Online (Sandbox Code Playgroud)

现在让我们具体一点,这是实现类:

public class MyRSSDataService : IDataService
{
    public async Task<List<Item>> GetItemsAsync(string url, IItemsParser parser)
    {
        using (var httpClient = new HttpClient())
        {
            var response = await httpClient.GetAsync(new Uri(url));

            if (response.IsSuccessStatusCode)
            {
                var jsonResponse = await response.Content.ReadAsStringAsync();
                List<Item> items = parser.ParseRawData(jsonResponse);

                return items;
            } …
Run Code Online (Sandbox Code Playgroud)

c# architecture rss solid-principles

3
推荐指数
1
解决办法
98
查看次数

服务层类是否违反了SRP原则?

好吧,当我在观看SOLID视频时,我想到了这一点.该单Responsabily原理说,"一类的应该只有一个单一的责任".

非常好.但与此同时,我正在使用N-Layer模型构建的ASP.NET MVC 5项目.我们有UI层,存储库层,域层和要公开的服务层.在服务层,我们有每一个基本类类(UserService,CompanyService,等).该UserService班有一个责任就是采取的护理User操作,但另一方面,它有很多型动物responsabilities如身份验证和处理用户/公司的关系.这违反了SRP原则吗?

architecture design-patterns solid-principles

3
推荐指数
2
解决办法
652
查看次数

使用关系中的属性为子类建模的正确方法

我有一个LawsuitEmployer列表的类.要求列表中的一个雇主必须被设定为主要雇主.我想到了两种方法来满足这个业务规则.

解决方案1

这是我当前的实现,我有一个MainEmployer和我在同一个属性和Employers列表上存储相同的实体:

public class Lawsuit()
{
    public int Id { get; set; }

    public virtual Employer MainEmployer { get; set; }
    public virtual ICollection<Employer> Employers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

解决方案2

我还可以EmployerLawsuit使用bool属性创建一个名为的中间类Main:

public class LawsuitEmployer()
{
    public int Id { get; set; }
    public bool Main { get; set; }

    public virtual Employer Employer { get; set; }
    public virtual Lawsuit Lawsuit { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# oop entity-framework solid-principles

3
推荐指数
1
解决办法
77
查看次数