1

is it a good pattern to add a parameter object instead of primitive for respecting the Open Close Principle.

Lets say I have this interface

public interface IBar{
  void DoSomething(int id);
}

If one day I need to add a parameter, I'll have to change my interface. With this solution

public interface IBar{
  void DoSomething(DoSomethingParameter parameters);
}

I can add as many parameters as I need without touching the interface, and I'll be able to create new implementaiton of IBar using the new parameters.

Is it an anti pattern ?

remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
  • 1
    Don't really understand what's your question, but since a good design is open for extension but closed for modification and you obviously modify the existing interface (not to mention you need to change all the classes which implement IBar as well), it's anti-pattern then, we should avoid change the interface as best as we can. – Paul Lo Nov 05 '13 at 13:00

1 Answers1

1

It is a good idea to use objects as parameters when you want to combine several related parameters into a meaningful representation.

In my opinion doing so for primitive types such as integers, strings ... etc i redundant and won't give you any benefits, if it didn't actually cause overhead for wrapping/unwrapping on each call.

Also you can see many interfaces representing the a Repository where there are always methods like GetAuthor(int id) that are used to get elements from the database by their respective ids.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95