open-closed-principle

Breaking SOLID Principles in multiple implementation of an Interface

一世执手 提交于 2019-11-28 06:46:36
问题 I am facing a problem with dependency inversion in a factory method and it is also breaking Open Closed principle. My code looks like below codes public interface IWriter { void WriteToStorage(string data); } public class FileWriter : IWriter { public void WriteToStorage(string data) { //write to file } } public class DBWriter : IWriter { public void WriteToStorage(string data) { //write to DB } } Now I an using a factory class to solve the object creation. It look like below code public

Understanding the Open Closed Principle

岁酱吖の 提交于 2019-11-27 14:11:53
问题 I was refactoring some old code of a simple script file parser when I came across the following code: StringReader reader = new StringReader(scriptTextToProcess); StringBuilder scope = new StringBuilder(); string line = reader.ReadLine(); while (line != null) { switch (line[0]) { case '$': // Process the entire "line" as a variable, // i.e. add it to a collection of KeyValuePair. AddToVariables(line); break; case '!': // Depending of what comes after the '!' character, // process the entire

What is the meaning and reasoning behind the Open/Closed Principle?

无人久伴 提交于 2019-11-27 11:07:11
The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is it an important principle of good object-oriented design? Specifically, it is about a "Holy Grail" of design in OOP of making an entity extensible enough (through its individual design or through its participation in the architecture) to support future unforseen changes without rewriting its code (and sometimes even without re-compiling **). Some ways to do this include Polymorphism/Inheritance, Composition, Inversion of

Is the Open/Closed Principle a good idea? [closed]

回眸只為那壹抹淺笑 提交于 2019-11-27 02:17:54
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago . This question is not about what OCP is. And I am not looking for simplistic answers, either. So, here is why I ask this. OCP was first described in the late 80s. It reflects the thinking and context of that time