solid-principles

What is the One Class, One Responsibility Principle?

白昼怎懂夜的黑 提交于 2019-12-01 03:06:13
问题 I would like to learn about the One Class, One Responsibility principle. I have found some articles about it, but without examples. It would help me if you can give me an example of a class that violates the principle. I'm familiar with the idea that a method should do only one thing, for instance get and set methods. It must not be the same as One Class, One Responsibility , because set and get methods are both implemented inside a class. So does this mean the class is violating the rule

Is the Composite Pattern SOLID?

為{幸葍}努か 提交于 2019-11-30 14:56:06
问题 A Leaf in the Composite Pattern implements the Component interface, including Add , Remove , and GetChild methods that a Leaf is never going to use. This seems to be a violation of the Interface Segregation Principle. So is the usage of Composite Pattern SOLID? link to Composite Pattern: http://www.dofactory.com/Patterns/PatternComposite.aspx 回答1: The real smell in the pattern as depicted in your link and most books is that Component has the methods of a Composite . I think this is probably

Does the traditional use of the controller in MVC lead to a violation of the Single Responsibility Principle?

纵饮孤独 提交于 2019-11-30 13:08:34
Wikipedia describes the Single Responsibility Principle this way: The Single Responsibility Principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility. The traditional use of the controller in MVC seems to lead a programmer towards a violation of this principle. Take a simple guest book controller and view. The controller might have two methods/actions: 1) Index() and 2) Submit(). The Index() displays the form. The Submit() processes it. Do

Aren't Information Expert & Tell Don't Ask at odds with Single Responsibility Principle?

落爺英雄遲暮 提交于 2019-11-30 11:54:02
问题 Information-Expert , Tell-Don't-Ask , and SRP are often mentioned together as best practices. But I think they are at odds. Here is what I'm talking about. Code that favors SRP but violates Tell-Don't-Ask & Info-Expert: Customer bob = ...; // TransferObjectFactory has to use Customer's accessors to do its work, // violates Tell Don't Ask CustomerDTO dto = TransferObjectFactory.createFrom(bob); Code that favors Tell-Don't-Ask & Info-Expert but violates SRP: Customer bob = ...; // Now Customer

SOLID for functional programming

旧时模样 提交于 2019-11-30 10:32:31
问题 Coming from an OOP language, I am familiar with the SOLID principles of object oriented design. It seems like some of these would fit into a functional programming model, while other parts make no sense in a world lacking state. Is there a similar set of principles for refactoring functional code? 回答1: As far as I know (I'm no expert), SOLID principles do not tell anything about state. They should be applicable as well in a functional programming languages. They're more advice about how to

Pattern for Creating a Simple and Efficient Value type

孤街醉人 提交于 2019-11-30 04:38:39
Motivation: In reading Mark Seemann’s blog on Code Smell: Automatic Property he says near the end: The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value type and all conceivable values are allowed. He gives int Temperature as an example of a bad smell and suggests the best fix is unit specific value type like Celsius. So I decided to try writing a custom Celsius value type that encapsulates all the bounds checking and type conversion logic as an exercise in being more SOLID . Basic requirements:

It this an example of the Single Responsibility Principle?

烈酒焚心 提交于 2019-11-30 04:04:55
I made the following code example to learn how to use a generics method signature. In order to get a Display() method for both Customer and Employee, I actually began replacing my IPerson interface with an Person abstract class . But then I stopped, remembering a podcast in which Uncle Bob was telling Scott Hanselman about the Single Responsibility Principle in which you should have lots of little classes each doing one specific thing, i.e. that a Customer class should not have a Print() and Save() and CalculateSalary() method but that you should have a CustomerPrinter class and a

Using the Single Responsibility Principle in the “real world” [closed]

試著忘記壹切 提交于 2019-11-30 01:49:17
I basically want to get an idea of the percentage of people who think it's reasonable to use the Single Responsibility Principle in real-world code and how many actually do. In Podcast #38 Joel talks about how useless this OOP principle is the real world; and further that this demonstrates how people like Uncle Bob have likely not written non-trivial systems. I've personally written or played a big role in a few software projects but have only now come across this pattern in my young career. I love the sound of this principle and would really like to start using it. I found Joel's argument in

SOLID for functional programming

时间秒杀一切 提交于 2019-11-29 22:13:31
Coming from an OOP language, I am familiar with the SOLID principles of object oriented design. It seems like some of these would fit into a functional programming model, while other parts make no sense in a world lacking state. Is there a similar set of principles for refactoring functional code? gasche As far as I know (I'm no expert), SOLID principles do not tell anything about state. They should be applicable as well in a functional programming languages. They're more advice about how to achieve modularity. Some of them are rather obvious or at least well-known. Single-responsibility is

How do you determine how coarse or fine-grained a 'responsibility' should be when using the single responsibility principle?

被刻印的时光 ゝ 提交于 2019-11-29 20:38:15
In the SRP, a 'responsibility' is usually described as 'a reason to change', so that each class (or object?) should have only one reason someone should have to go in there and change it. But if you take this to the extreme fine-grain you could say that an object adding two numbers together is a responsibility and a possible reason to change. Therefore the object should contain no other logic, because it would produce another reason for change. I'm curious if there is anyone out there that has any strategies for 'scoping', the single-responsibility principle that's slightly less objective?