solid-principles

Does using enum with associated value in Swift violate Liskov Substitution Principle?

痴心易碎 提交于 2019-12-08 05:14:13
问题 enum WeatherType { case cloudy(coverage: Int) case sunny case windy } I just saw this in a Swift tutorial and I can't believe they allow you to do that. Now, whenever I switch on that enum, I gotta create a special case for cloudy ! 回答1: You don't "gotta" do anything. If you don't care what the coverage is, don't ask what the coverage is. If you don't care if it's cloudy, don't ask if it's cloudy. There is nothing special about the way you write a switch for a case that has an associated

Does using enum with associated value in Swift violate Liskov Substitution Principle?

别说谁变了你拦得住时间么 提交于 2019-12-07 03:54:26
enum WeatherType { case cloudy(coverage: Int) case sunny case windy } I just saw this in a Swift tutorial and I can't believe they allow you to do that. Now, whenever I switch on that enum, I gotta create a special case for cloudy ! You don't "gotta" do anything. If you don't care what the coverage is, don't ask what the coverage is. If you don't care if it's cloudy, don't ask if it's cloudy. There is nothing special about the way you write a switch for a case that has an associated value. Suppose we have this: let weather = WeatherType.cloudy(coverage:1) Then this is perfectly legal: switch

How to resolve type at run time to avoid multipe if else

依然范特西╮ 提交于 2019-12-07 02:59:10
问题 I have my code which makes a webservice Call based on type of request. To do that , I have following code; public class Client { IRequest request; public Client(string requestType) { request = new EnrolmentRequest(); if (requestType == "Enrol") { request.DoEnrolment(); } else if (requestType == "ReEnrol") { request.DoReEnrolment(); } else if (requestType == "DeleteEnrolment") { request.DeleteEnrolment(); } else if (requestType == "UpdateEnrolment") { request.UpdateEnrolment(); } } } So as per

Issue in using Composition for “is – a “ relationship

此生再无相见时 提交于 2019-12-07 01:06:14
问题 I have system being developed for an HR system. There are Accountant employees and Programmer employees. For the first month of joining the company, the employee is not given any role. One employee can be an Accountant and a programmer at the same time. I have a design shown by the following code. Now, I need to enhance the system by implementing a new functionality: Terminate all Accountants. (Terminate means set status of employee as IsActive = false). The issue is I cannot set all

How to avoid switch-case statements in Java [duplicate]

江枫思渺然 提交于 2019-12-07 00:42:03
问题 This question already has answers here : Converting many 'if else' statements to a cleaner approach [duplicate] (7 answers) Closed 3 years ago . I have an enum of TriggerType, where different triggers can be added public enum TriggerType { meta,data,list,toggle } These trigger types are used inside different handlers (eg Component, Dashboard etc) to identify which trigger is triggered inside the handler through a switch-case, for eg Code snippet of ComponentHandler using trigger through

Understanding the practical benefits of using the Single Responsibility Principle

可紊 提交于 2019-12-06 17:52:37
问题 I'm trying to understand the SRP but, whilst I understand the reasoning behind how to apply it, I'm not really seeing the benefit of doing so. Consider this example, taken from Robert Martin's SRP PDF: interface IModem { void Dial(string number); void Hangup(); void Send(char c); char Recv(); } He proposes separating this into two interfaces: interface IModemConnection { void Dial(string number); void Hangup(); } interface IModemDataExchange { void Send(char c); char Recv(); } I've also been

Dependency Inversion Principle and where to put the interfaces

半世苍凉 提交于 2019-12-06 07:10:44
问题 I am building a simple MVC application in asp.net. I want to follow the dependency inversion principle and I don't know if I am doing it right. I am currently working on the Authentication system. I have an AccountController which uses the Authenticator service inside. The Authenticator service is injected into the controller by constructor injection. public class AccountController : Controller { private IAuthenticator _authenticator; public AccountController(IAuthenticator authenticator) {

SQLAlchemy: Knowing the field names and values of a model object?

淺唱寂寞╮ 提交于 2019-12-06 06:01:59
问题 I'm trying to keep to SOLID object oriented programming principles, stay DRY, etc, but my newness to Python/SQLAlchemy/Pyramid is making it very hard. I'm trying to take what I now know to be a SQLAlchemy model used to create a simple Pyramid Framework object and use what I know to be "reflection" in C#, it may be called something different in Python (Introspection? Not sure as this is only my second week with python but I have lots of experience in other languages (C/C++/C#,Java, etc) so the

SOLID principles and web page class

和自甴很熟 提交于 2019-12-06 05:44:38
I am trying to follow SOLID principles when writing new classes and code. My question is about ASP.NET classes that extend the Page class i.e. ASPX files. If I had a page class that has a page_load event that creates instances of multiple objects e.g. Person class, Sport class etc then I believe this page class is tightly coupled with these classes. Is this the case or am I missing something obvious? Could it be that all classes should expose interfaces and the client (aspx pages) should use the interfaces rather than instantiating the classes directly. I find interfaces useful if Polymorphism

Does dependency inversion principle mean that I have to create an interface for every module?

流过昼夜 提交于 2019-12-06 02:56:35
问题 If I want my code to follow SOLID principles, specifically the dependency inversion principle, does that mean that I have to create an interface (abstraction) for every module, even if it has only one implementation ? In my opinion, and according to these posts: http://josdejong.com/blog/2015/01/06/code-reuse/ http://blog.ploeh.dk/2010/12/02/Interfacesarenotabstractions/ creating an "abstraction" for every module is a code clutter and violates the YAGNI principle. My rule of thumb is: do not