open-closed-principle

How to avoid type checking in C# to load correct UI?

拜拜、爱过 提交于 2020-01-23 10:39:43
问题 I am building a simple quiz program with WinForms that has 2 modes: 1) Edit mode: Where the user can create its own questions 2) Quiz mode: Where the user needs to answer the questions Currently there are 2 type of questions: Open (question and free text box) and multiple choice (question and 4 possible answers). I have created an abstract class for a Question: public abstract class Question { public string QuestionString { get; private set; } public Question(string q) { QuestionString = q; }

Visitor Pattern + Open/Closed Principle

馋奶兔 提交于 2019-12-31 10:36:16
问题 Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle, but still be able to add new visitable classes? The Open/Closed Principle states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification". struct ConcreteVisitable1; struct ConcreteVisitable2; struct AbstractVisitor { virtual void visit(ConcreteVisitable1& concrete1) = 0; virtual void visit(ConcreteVisitable2& concrete2) = 0; }; struct

Configuring Automapper in Bootstrapper violates Open-Closed Principle?

我们两清 提交于 2019-12-29 02:23:05
问题 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();

Making Validation Generic

为君一笑 提交于 2019-12-23 04:33:57
问题 I have the following C# code. Here the validations are kept outside the class to satisfy Open – Closed Principle. This is working fine. But the challenge is – the validations are not generic. It is specific to employee class (E.g DateOfBirthRuleForEmployee). How do I make the validations generic for all objects (DateOfBirthRuleForAnyObject). Note: Make Generic <==> Make Type-Independent Note: I have NameLengthRuleForEmployee validation also. New validation may come in future. EDIT Generic

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

我怕爱的太早我们不能终老 提交于 2019-12-21 16:52:11
问题 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)

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

独自空忆成欢 提交于 2019-12-17 10:25: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? 回答1: 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

Does Façade leverage the Open-Closed Principle?

寵の児 提交于 2019-12-13 15:36:10
问题 The Wikipedia page (as of today 2013-02-27) for the Open-Closed Principle says that it's realized via inheritance. The name Open/Closed Principle has been used in two ways. Both ways use inheritance to resolve the apparent dilemma, but the goals, techniques, and results are different. The "two ways" refers to Meyer's implementation inheritance and the more common Polymorphic extensions. Anyway, my question is about the Façade pattern, which does not use inheritance. Since it defines an

Open / Closed Principle - How to deal with this Switch?

别来无恙 提交于 2019-12-12 11:05:33
问题 I have been looking into the open closed principle and it sounds good and so I wanted to exercise it's teachings. I looked at applying my new found knowledge to an existing project and have become a little stuck right away. If a new UserType comes along (And this is Very likely), this will need to be changed, it is not yet closed to modification. How could one get round this? From what I have read, it sounds like I should be implementing a factory here instead of applying the OCP? Factory

Template specialization or conditional expressions?

北城以北 提交于 2019-12-10 18:06:46
问题 I am deep into a new project which I address with a bunch of templates and specializations of them. Now, after a day without programming, I find myself asking whether it is really worth the extra lines of code. The question is: What are the advantages of specialization? Is this: template <int i> class A {}; template <> class A <1> { void foo() {/* something */} }; template <> class A <2> { void foo() {/* something else*/} }; template <> class A <3> { void foo() {/* even different*/} }; In any

Why do static and instance init blocks in Enums behave differently from those in Classes

可紊 提交于 2019-12-10 14:21:29
问题 In studying for the Java certification test I learned that static initialization blocks run once when the class is loaded, in order of appearance within the source code, that instance initialization blocks run each time an instance is created, and that code in constructors runs each time an instance is created after that. To test that I created a class with some static and instance init blocks and a contructor with print stuff. Everything worked as expected -- except I thought "loaded" meant