Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m fairly new at using design patterns and I am currently making a small game

ID: 659070 • Letter: I

Question

I'm fairly new at using design patterns and I am currently making a small game for my uni course that I have to use the decorator pattern for creating the different classes of NPC however I think that I have misunderstood how the decorator pattern works.

At the moment all what changes between the classes of NPC's are the attack value, health and speed stats. What I am really unsure of is if there is a way of passing these values into the decorated object from the decorator or do i have the wrong impression of the decorator pattern.

Explanation / Answer

Think of decorator as wrapping up an object with more functionality. Now this new object has an object inside of it, and the outer object gives it more functionality.

Here's a little pseudo representation of it.

class Watch implements Clock
{
private Clock clockToBeDecorated;

public Watch(Clock clock)
{
clockToBeDecorated = clock;
}

public void StrappToWrist()
{
//strap the watch to your wrist
}

public DateTime GetTime()
{
return clock.GetTime();
}
}

interface Clock
{
DateTime GetTime();
}
So now we have all the functionality of a Clock but we can also strap it to our wrist (like you'd expect with a wrist). This is good when you wish to dynamically pass the object into another, so we may wish to put that click into a Watch or a PocketWatch etc.

Wikipedia has a good example using Scrolling Windows in Java as well which may help.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote