New answers tagged

1

I'm a bit puzzled about following example: Are you not the creator of the example? The posted code, together with your question indicates that you are worrying about principles far before you have gotten your problem down, analyzed it and created a fair design to solve it, no matter how small or large it is. You say: A sort of "pseudo Finite State ...


1

Most of the interaction between a car and driver shouldn't take place using public methods of either. Instead, a driver wishing to board a car should create a private object that is designed to receive notifications from a car, and pass that to a function which will "board" the car. The car should then give the driver a reference to a private "car control" ...


9

It's not appropriate to have the Car deciding what the owner should do when it's out of gas, and certainly not acceptable for the car to choose what other kind of car the driver should drive if it runs out of gas. The ICar interface should look more like this: public class ICar { // return false if the car is undriveable boolean Drive(); } Then: ...


3

For artificial examples out of any context, it is often undecidable if they violate SOLID or not, and it is impossible to evaluate the code as "good" or "bad" in a sensible manner . For example, the DIP says "High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g. interfaces)." but what in this example is "a ...


0

As written you are breaking the Liskov substitution principle and Interface segregation principle. Anything that implements i_pet needs to be able to be substituted without breaking anything. The fish class is causing this to fail with get_fur_color() not being implemented. Furthermore, the pet_printer is breaking the open closed principle, the problem here ...


1

You are violating ISP but not so much because of an out-of-place method. The idea behind ISP is to keep interfaces small, which really means they should define just one behavioral aspect. This implies that both ICat and IFish are sub-optimal and pointless from an ISP perspective. Instead of ICat you should have interfaces like IScratcher, IJumper, ...


0

There are a fair number of principles being violated here that have nothing to do with SOLID. Understand that SOLID isn't a comprehensive list of every useful principle. Even Uncle Bob himself has published principles beyond SOLID that are worth considering. You have indicated that you only want to focus on SOLID violations but that narrow view is causing ...


1

Whether the ISP is broken or not depends on what parts of the interface the clients require. If there is just one client as in your example, and it requires all of the interface methods, then this is no ISP violation. Moreover, it could perfectly make sense to have a color "indefinite", which can be the return value for pets like fish. This can be a ...


1

Yes you are right i would do it some thing like create another interface for bird which will implement i_Pet but with "color get_fur_color()" so: interface i_pet string get_name() string get_species() end interface interface i_bird implements i_pet color get_fur_color() end interface Other then that i don't think if your breaking any solid principle....


1

Why does class cat have an instance field for species? All cats are the same species by definition. As you say, fur_color has no place in i_pet because not all pets have fur. But that's not a question of the ISP, it's worse - the method isn't just more than most clients need, it's actively wrong because it may or may not be appropriate for a subclass et all.


2

There is no general solution. Ask yourself about performance, usability, security and lifecycle of a constant. The closer they are defined to their scope the higher the performance. The more they are grouped logically and outside their scope, the higher the reusability. The less accessible the costants are, the higher the security. The higher the ...


Top 50 recent answers are included