open-closed-principle

Extending List<T> and Violating The Open/Closed Principle

梦想的初衷 提交于 2019-12-10 13:13:16
问题 I just created the following method in one of my classes public static bool Assimilate(this List<Card> first, List<Card> second) { // Trivial if (first.Count == 0 || second.Count == 0) { return false; } // Sort the lists, so I can do a binarySearch first.Sort(); second.Sort(); // Copia only the new elements int index; for (int i = 0; i < second.Count; i++) { index = first.BinarySearch(second[i]); if (index < 0) { first.Insert(~index, second[i]); } } // Edit second = null; return true; } And a

Getting past Open-Closed Principle

百般思念 提交于 2019-12-07 00:31:35
问题 I have a simple program which draws geometrical figures based on mouse data provided by user. I've got one class which handles the mouse tracking (it gets the List with mouse movement history) and one abstract class called Shape. From this class I derieve some extra Shapes, like Circle, Rectangle, etc. - and every one of them overrides the abstract Draw() function. It all works nicely, but the problem comes when I want the user to be able to switch desired Shape manually. I get the mouse data

Getting past Open-Closed Principle

大憨熊 提交于 2019-12-05 04:49:50
I have a simple program which draws geometrical figures based on mouse data provided by user. I've got one class which handles the mouse tracking (it gets the List with mouse movement history) and one abstract class called Shape. From this class I derieve some extra Shapes, like Circle, Rectangle, etc. - and every one of them overrides the abstract Draw() function. It all works nicely, but the problem comes when I want the user to be able to switch desired Shape manually. I get the mouse data and i know what shape should I draw. The problem is how to make the code to "know" which object should

Simple Factory vs Factory Method: Switch statement in factory vs. client

好久不见. 提交于 2019-12-04 08:16:35
I understand that one of the main advantages of the Factory Method over Simple Factory is that it doesn't violate the Open-Closed SOLID Principle. That is, the former doesn't require modifying the switch statement when new types are added. There is one piece on which I am hoping to get clarification. If I were to use a simple factory, I would have a factory like this (simplified): public class ObjectFactory { public static IObject CreateObject(ObjectTypeEnum objectType) { switch (objectType) { case TypeA: return ObjectA; break; case TypeB: return ObjectB; break; case TypeC: return ObjectC;

Open-closed principle and Java “final” modifier

纵饮孤独 提交于 2019-11-30 04:42:55
The open-closed principle states that "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". However, Joshua Bloch in his famous book "Effective Java" gives the following advice: "Design and document for inheritance, or else prohibit it", and encourages programmers to use the "final" modifier to prohibit subclassing. I think these two principles clearly contradict each other (am I wrong?). Which principle do you follow when writing your code, and why? Do you leave your classes open, disallow inheritance on some of them (which ones?),

Breaking SOLID Principles in multiple implementation of an Interface

好久不见. 提交于 2019-11-29 12:58:46
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 interface IFactory { IWriter GetType(string outputType); } public class Factory : IFactory { public IWriter

Open-closed principle and Java “final” modifier

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 02:04:32
问题 The open-closed principle states that "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". However, Joshua Bloch in his famous book "Effective Java" gives the following advice: "Design and document for inheritance, or else prohibit it", and encourages programmers to use the "final" modifier to prohibit subclassing. I think these two principles clearly contradict each other (am I wrong?). Which principle do you follow when writing

Understanding the Open Closed Principle

☆樱花仙子☆ 提交于 2019-11-28 21:35:00
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 "scope" and/or the command in "line". if (line == "!execute") ExecuteScope(scope); else if (line

Configuring Automapper in Bootstrapper violates Open-Closed Principle?

喜欢而已 提交于 2019-11-28 15:19:45
I am configuring Automapper in the Bootstrapper and I call the Bootstrap() in the Application_Start() , and I've been told that this is wrong because I have to modify my Bootstrapper class each time I have to add a new mapping, so I am violating the Open-Closed Principle. How do you think, do I really violate this principle? public static class Bootstrapper { public static void BootStrap() { ModelBinders.Binders.DefaultBinder = new MyModelBinder(); InputBuilder.BootStrap(); ConfigureAutoMapper(); } public static void ConfigureAutoMapper() { Mapper.CreateMap<User, UserDisplay>() .ForMember(o =>

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

随声附和 提交于 2019-11-28 08:34:05
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. The concern was that changing source code to add or modify functionality, after the code had already been tested and put into production, would end up being too risky and costly. So the idea was to avoid changing existing source files as much as possible, and only add to the codebase in the form of subclasses (extensions). I may be wrong, but my impression is that network-based