Win a copy of Terraform in Action this week in the Cloud forum!

Husam Alsulimani

+ Follow
since Apr 08, 2013
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Husam Alsulimani

well Thanks jeff

I did a quick search and found that strategy pattern is the solution to the case i had in mind

you are right we should not use instanceof

thanks again
8 years ago
as I understand you have a super class such as Animals and sub classes cat and dogs
then you have a list of Animals type which can holds cat and dogs and you need to distinguish between them to take an action
based on the object type in the Animal list

if this is what you want you can use the keyword instanceof

8 years ago
First point
PizzaType variable must be private for encapsulation and access it through public getter and setter methods
if you declare it public you violate encapsulation rules

second point
if you have a variable in super class you don't need to declare it again in the subclass because subclass already inherent this variable

I assume you are learning decorator pattern?
if this is the case you need more classes
The following code explain the concept

This interface or you can use abstract class

this is your super calss which define a normal pizza type implements the Pizzainterface or extend Abstract class if you goes with this choice

This is abstract class represent a base class for the topping exists

This class define one of the toppings available it must extends the decorator class and you need sub class for each topping this is the main idea behind the decorator pattern which is extends
behavior with out recompile

This is another type of topping

This is the client calss which use this code

Hope this helps
8 years ago