SE 350: worksheet #3-Composite Pattern O What problem does the composite pattern
ID: 3880036 • Letter: S
Question
SE 350: worksheet #3-Composite Pattern O What problem does the composite pattern address? MagicItems AT WizardBag WizardItems Fill in key data structures and key operations in the composite structure shown above. Assume that the Wizard has several wizard bags which he can put inside each other. Bags can contain other bags or items. He can make the entire contents of a bag or a single item disappear by saying the words: Evanesco Ocsenave. write Java code for the disappear method in Wizard Items: 0 Write java code for the disappear method in Wizard Bag:Explanation / Answer
Composite design pattern addresses the problem as
whenever the user wants to treat the individual object in the same way as the collection of those individual objects such as a wizard items can be made disappeared and wizard bag can also be made disappeared which also disappears the magic items that wizard bag contains.This shows both the wizardbag and wizarditems have same functionality and user had not to worry about that.
here are the contents of
Magic Items interface - this is an interface because we want to have a common functionality for its objects.
1.visibilityOff //a boolean variable
2.disppear function-// a method to make item disappeara or set the boolean variable to true
/*here is the code for the interface magicitems and wizard items disappear method;disappear method make the boolean variable visibilityoff to true */
Interface MagicItems
{
public boolean disappear();
}
class WizardItems implements MagicItems
{
boolean visibilityOff;
@Override
public void disappear()
{
if(visibilityOff==false)
{
visibilityOff=true;
}
}
}
here is the code for composite object wizard bag
class WizardBag implements MagicItems
{
List<Boolean> childvisibility=new ArrayList<Boolean>();
public void disappear()
{
for(Boolean b:childvisibility)
{
b.disappear();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.