14 Replies - 870 Views - Last Post: 14 May 2013 - 07:01 PM

#1 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Use inheritance or interface

Posted 12 May 2013 - 04:05 PM

Hi guys, I've been learning a bit about design principles and I've come across one thing that has been bugging me.

Say I had a Pizza class. Would it be better to subclass Pizza and make a CheesePizza class or a MeatLoversPizza class OR should I create a Topping interface and add an array/collection inside of Pizza to hold objects of classes that derive from Topping?

Or a third option...I could do both. I could create set types of Pizza, like MeatLoversPizza, and have it contain the right Topping classes. That way a person can order a premade type, but can also customize.

I think the third option is the best. But when I read examples from textbooks or even when I hear examples from my teachers, they never talk about using interfaces to "encapsulate what varies".

For example, my professor was talking about Employee and creating subclasses of Employees called SalariedEmployee, ComissionEmployee, and HourlyEmployee.

I was like...wait...wouldn't it be better if we just added a field that would hold an interface of WageType. That way, if we had a SalaryEmployee switch to Hourly, we can just change that field, instead of deleting that SalarayEmployee and creating HourlyEmployee.

When I gave that opinion to my professor, he didn't really seem to grasp where I was going with it...Which was kind of disappointing...

Is This A Good Question/Topic? 0
  • +

Replies To: Use inheritance or interface

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Use inheritance or interface

Posted 12 May 2013 - 04:16 PM

There is a tutorial on classes linked in my signature block. After working it, I think you should be able to make this design decision on your own.
Was This Post Helpful? 1
  • +
  • -

#3 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Use inheritance or interface

Posted 12 May 2013 - 04:34 PM

View PosttlhIn`toq, on 12 May 2013 - 04:16 PM, said:

There is a tutorial on classes linked in my signature block. After working it, I think you should be able to make this design decision on your own.


Nice tutorial. Using an interface for Ingredient seems like the better way to go rather than creating individual types of pizza.

We could also set the Ingredient interface to derive from Dish. That way we can "wrap" the dishes and ingredients up to get the final total right?!

This post has been edited by toad87: 12 May 2013 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Use inheritance or interface

Posted 12 May 2013 - 04:38 PM

You could use Inheritance with Interfaces.
IPizza
 .Name     : String
 .Toppings : IE<ITopping>
 .Type     : IPizzaType
 .Size     : IPizzaSize

ITopping
 .Name     : String

IPrice
  .Price    : Decimal

IPizzaType
  .Name : String

IPizzaSize
  .Name : String

IStuffedCrustPizza <: IPizza
 .CrustStuffing  : ICrustStuffing

IPizzaCalculator
 .Calculate : IPizza -> Decimal

IPizzasCalculator
 .Calculate : IE<IPizza> -> Decimal



Was This Post Helpful? 0
  • +
  • -

#5 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Use inheritance or interface

Posted 12 May 2013 - 04:47 PM

View PostAdamSpeight2008, on 12 May 2013 - 04:38 PM, said:

You could use Inheritance with Interfaces.
IPizza
 .Name     : String
 .Toppings : IE<ITopping>
 .Type     : IPizzaType
 .Size     : IPizzaSize

ITopping
 .Name     : String

IPrice
  .Price    : Decimal

IPizzaType
  .Name : String

IPizzaSize
  .Name : String

IStuffedCrustPizza <: IPizza
 .CrustStuffing  : ICrustStuffing

IPizzaCalculator
 .Calculate : IPizza -> Decimal

IPizzasCalculator
 .Calculate : IE<IPizza> -> Decimal




I thought interfaces cannot have fields in c#?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Use inheritance or interface

Posted 12 May 2013 - 04:59 PM

What makes you think it's C#? I see and read of no mention C# in your question.
Was This Post Helpful? 0
  • +
  • -

#7 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Use inheritance or interface

Posted 12 May 2013 - 05:44 PM

View PostAdamSpeight2008, on 12 May 2013 - 04:59 PM, said:

What makes you think it's C#? I see and read of no mention C# in your question.


I'm sorry. I assumed it was c# because you used the "I" prefix. But I guess that's a c language thing. I came from java and have just recently started to learn c#.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,115
  • Joined: 05-May 12

Re: Use inheritance or interface

Posted 13 May 2013 - 06:05 AM

View Posttoad87, on 12 May 2013 - 07:47 PM, said:

I thought interfaces cannot have fields in c#?


And specifically for C#, interfaces can have properties. See IList's properties: http://msdn.microsof...y/hc0h48wd.aspx
Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Use inheritance or interface

Posted 14 May 2013 - 06:13 AM

toad87 your Pizza example is a perfect fit for Decorator design pattern (take a look at coffee making scenario example)

This post has been edited by lucky3: 14 May 2013 - 06:14 AM

Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Use inheritance or interface

Posted 14 May 2013 - 06:46 AM

I'm not convinced about the fancier solutions, especially the Decorator pattern. What if you want to remove one of the ingredients?

We can assume that the available ingredients and their prices are going to change over time. The program will probably load them from a file or database. This means we don't know ahead of time what they are going to be. The best we can do is:

class Topping {

	private String name;
	private int pennyPrice;

	public Topping(String name, int pennyPrice) {
		this.name = name;
		this.pennyPrice = pennyPrice;
	}

	public String getName() {
		return name;
	}

	public int getPennyPrice() {
		return pennyPrice;
	}
}


Toppings would be stored in a List: Easy to add and remove items; Easy to print out and sum up.

Predefined pizzas would also be loaded from a file or database. It might be sensible to clone these as a starting point for customisation.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,115
  • Joined: 05-May 12

Re: Use inheritance or interface

Posted 14 May 2013 - 08:39 AM

With a Decorator pattern, the relationships of the decorators do not need to be hard coded even though that is the most common example. One can use the Builder pattern where decorations are built up on the fly.

And just to tease cfoley, isn't have the predefined pizzas available from a file or database the Template pattern?
Was This Post Helpful? 0
  • +
  • -

#12 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Use inheritance or interface

Posted 14 May 2013 - 08:52 AM

I've been trying to think up reasons why the decorator pattern would be better than what cfoley suggested but can't really come up with anything.

I've ever only really thought of the decorator pattern use in terms food/drinks and to calculate prices.

Can someone give me an example of the decorator pattern used in application not relating to food or to calculate price?



Sent from my Nexus 10 using Tapatalk HD
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Use inheritance or interface

Posted 14 May 2013 - 09:58 AM

Quote

One can use the Builder pattern where decorations are built up on the fly.


Yes but reguardless of if it is done on the fly, you have difficulty removing toppings. Say you have:

Chicken(Anchovies(Peppers(Onions(BasePizza))))


How do you remove the Anchovies? It's not impossible but I'd rather just call toppings.remove(anchovies).

Quote

And just to tease cfoley, isn't have the predefined pizzas available from a file or database the Template pattern?


Is it? Cool. I'll have to read up on it. :)
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Use inheritance or interface

Posted 14 May 2013 - 03:42 PM

Quote

Can someone give me an example of the decorator pattern used in application not relating to food or to calculate price?


IO in Java and .NET uses the decorator pattern. Here are some examples:

		BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
		BufferedReader b = new BufferedReader(new FileReader("c:\\some-file.txt"));
		BufferedReader c = new BufferedReader(new StringReader("Hello World!"));



A really cool use of this is that you can design your programs to take any kind of reader. When you are testing it, use a String reader. When you are running it for real, iuse a FileReader or InputStreamReader:

public class IODemo {

	public static void main(String[] args) throws IOException {
		new IODemo(new StringReader("Hello.\nThis is a test string\nwith multiple lines."));
		new IODemo(new FileReader("C:\\using-it-for-real.txt"));
	}

	public IODemo(Reader in) throws IOException {
		BufferedReader lineReader = new BufferedReader(in);
		String line = lineReader.readLine();
		do {
			System.out.println(line);
			line = lineReader.readLine();
		} while (line != null);
	}

}


Was This Post Helpful? 1
  • +
  • -

#15 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Use inheritance or interface

Posted 14 May 2013 - 07:01 PM

Oh that's right! I forgot Java's IO uses the decorator pattern!

Thanks cfoley!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1