• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Interfaces while working with Value Objects  RSS feed

 
Hanish Wassan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone. I am new to this forum and this is my first post. I have the following request.

I have some value objects in my project that are used to store and pass data from and to the database. Some of them are inherited from the other. I have some methods that take these value objects as parameters and return the requred result. I have been suggested to use interfaces as parameters instead of actual class (i.e. value object e.g. A). I understand that it enhances flexibility. However...

My question is that wheather it is useful and/or adds value to code if interfaces are used as parameters instead of actual concrete classes, for the value objects?
Thanks.
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aye.

it is better to use the interface, but only if you code well to begin with.
you said yourself that you have value objects that inherit other value objects. this case is easy, since you can just put the base class as the argument type, and code ahead.
however, suppose you have two value objects that do not have the inheritance relationship. however, they are still similar in a way - so in your design they both implement the same interface. lets say, for example, that we are talking about some kind of merchandise, that implements an interface Product that has two methods: getPrice() and getQuantity().
if all your value objects implement this interface, it will be very easy for other parts of the application to receive an argument of type Product and not worry about anything else except getPrice() and getQuantity().

Nimo.
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hanish Wassan:

My question is that wheather it is useful and/or adds value to code if interfaces are used as parameters instead of actual concrete classes, for the value objects?
Thanks.


Hi Hanish. What you are talking about is polymorphism. One of the aims of OOP is that you should be able to re-use more of your code than was possible before. It is not absolutely necessary that your parameters should be passed as interfaces. The aim is that you should pass (as a parameter) some type to which the broadest range of objects conform while still making sense. As it is good practice to use an interface to define the behaviour that a class inheriting from it should display this tends to mean that the type that you pass will be an interface.

That's all a bit dry so here's an example:

Suppose you are working for a food company and you wish to write some software to model pies. The company makes different kinds of pies; apple pie, cherry pie, blackberry pie... Each pie is different but at the same time it shares certain characteristics. They all have a crust, they all contain fruit ( I'm ignoring meat pies here for simplicity ), they have sugar in them, they all have a name.

Now instead of writing a different class for each type of pie, it would simplify my program and make it more robust if there was a basic contract defining what a pie should be and do. This is the purpose of an interface.


If I now write a class for each type of pie, by conforming to the FruitPie interface I ensure that all the pies are consistent in their common methods. All the pies will conform to this type and as a result I can write code which will accept any kind of pie as an argument without worrying about whether I am dealing with an ApplePie or a GooseberryPie. If I don't do that then I may be forced to write:


rather than just


This is obviously more desirable. It makes your code less liable to errors, simpler and makes it simple for the company to add new kinds of fruit pie to its range without having to rewrite existing code. All the new pie has to do is conform to the agreed FruitPie type.

Now, it is up to you whether your code justifies this approach. You may feel that your code is one off, will never be updated and will never be used by anyone else. In which case you might feel that this approach is unnecessary. However, it is a good idea to get used to writing code that is robust and flexible. This is what people are recommending to you.

Regards,
Ken

E&OE
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!