• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

For decorator pattern better Interface or Abstract Class?

 
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I want to create a simply drinks shop but I have some doubts about the class structure.

I have 3 types of coffee with some CoffeeDecorator and 3 Types of Cocktails with some DrinksDecorator.

Do you think is better to use also the Composite pattern for the 3 types of coffee and another composite for cocktails?

For the drinks, is better to have a Interface Drinks or an Abstract Class?

Thanks in advance!
M.Lars
 
Michael Lars
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Idea?
 
Ranch Hand
Posts: 209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

It depends on the implementation details,

You could use both an interface and an abstract class,

You could use an interface to define what the method signatures that other methods can call,

The abstract class could then be used for common code between the 3 classes, if there is no common code then its better just to have an interface but if there is common code then its good to have an abstract class,

Hope this helps,

Niall
 
Michael Lars
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Niall!

I'm attaching an image where I made a simply structure.

And my question is, will this structure work? - Is ok if in the future this project will be expanded for an invoice print, or other drinks?

Thanks again, I appreciated it!
mo.jpg
[Thumbnail for mo.jpg]
 
Michael Lars
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or is better this class diagram?

Thank you!
78.png
[Thumbnail for 78.png]
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either will work. I can't off the top of my head think of a reason to favor one over the other. I would probably use the second approach, just because the decorator needs to appear as if it is the thing it decorates, so why not make that literally true.
 
Michael Lars
Greenhorn
Posts: 20
jQuery Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!