0

I'm student learning Inheritance in Java.

I'm making a program (Bitcoin Mining Game). In the program, there are different kind of digger (Mining Machine):

  • ordinary digger (this only does digging coin),

  • overclock digger (this can dig and can overclock to dig faster) and

  • durability recover digger (this can dig, overclock and recover durability (every digger has durability, durability drops while digging).

I made this class diagram (There are some attributes and methods that I didn't describe in pictures. I thought those are not needed for my question.):

enter image description here

I'm not sure that I did it right, in particular, the use if Inheritance.

Instead of using inheritance, should I just make a class like this? Because only one function adds to the classes:

enter image description here

1
  • hi and welcome to StackOverflow! please edit your question to clarify your specific problem or add additional details to highlight exactly what you need. Commented Jan 20, 2020 at 3:44

1 Answer 1

1

This might be a good candidate for the Decorator Pattern, which allows you to apply customized functionality to an individual object at run time. A good reference for this can be found on the Decorator Pattern Wikipedia page, which includes a detailed example of a Windowing system, with a UML class diagram for reference. A snippet of the code shows the decorator and the concrete decorator:

// abstract decorator class - note that it implements Window abstract class WindowDecorator implements Window { private final Window windowToBeDecorated; // the Window being decorated public WindowDecorator (Window windowToBeDecorated) { this.windowToBeDecorated = windowToBeDecorated; } @Override public void draw() { windowToBeDecorated.draw(); //Delegation } @Override public String getDescription() { return windowToBeDecorated.getDescription(); //Delegation } } // The first concrete decorator which adds vertical scrollbar functionality class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window windowToBeDecorated) { super(windowToBeDecorated); } @Override public void draw() { super.draw(); drawVerticalScrollBar(); } private void drawVerticalScrollBar() { // Draw the vertical scrollbar } @Override public String getDescription() { return super.getDescription() + ", including vertical scrollbars"; } } 

In your case the decorator would be called DiggerDecorator and the concrete decorators could be called OverclockDecorator and RecoverDurabilityDecorator.

The code to instantiate a fully decorated object might look like this:

Digger decoratedDigger = new OverclockDecorator(new RecoverDurabilityDecorator(new SimpleDigger())); 

As you can see, this solution still uses inheritance, but individual functionality can be "mixed in" as needed.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. I didnt learn abstract class. So i am thinking im just using the first picture of diagram.
Im wondering if i use the first one is there any point that im using inheritance wrong?
And having question about decorator pattern. If i use that pattern there would be classes digger class, simple digger class. And digger decorator class , overclock digger class and durablity digger dlass. Is it right?
Digger would be an interface and the others would be classes, with DiggerDecorator being an abstract class.
To answer your question about whether you're using inheritence wrongly, it looks like a DurabilityRecover class doesn't necessarily need to be the sub-class of a OverclockDigger and this doesn't adhere to the Single Responsibility principle. In my view, this could be overcome by mixing in the functionality instead. Does this help?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.