2

I would need 2 object : max heap and min heap. Both objects will be the same but some their methods like swap or bubbleUp compare object in different way. Only comparing lines are different:

while (curr > 0 && (heap[parent].compareTo(heap[curr]) < 0)) {

Is it better to create Heap class that has got boolean value that stores information is it max or min heap? Or is better to create subclasses for min and max heap that will have got their own methods?

  public abstract class Heap {
    private int[] values = new int[];

    public void SomeHeapMethod()
    {
if(values[0].compareTo(values[1]) > 0 ) //this would be diffent for max and min heap
    }
}
Mateusz
  • 604
  • 8
  • 20
  • 2
    Have an abstract class with method listed and have different implementations in both classes of same method. in this way, you don't need to explicitly provide the method. – Aditya Peshave Nov 12 '15 at 22:38

2 Answers2

2

Create two classes with their own methods. If you are creating a class, that can act as two different classes you are violation one of the principles of the highly renowned Clean Code book.

In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility….

Another perspective is readability, if another programmer is ever gonna look at your code, it is way harder to pick up that a class has two states of functionality compared two a polymorphism or two classes solution.

Here is how i would solve the solution using polymorphism. Where the shared functionality is inherited and the custom functionality is defined in each class.

public abstract class Heap {
    private Integer[] values;

    public int compare(int i , int j)
    {
        throw new RuntimeException("Not implemented");
    }

    public void SomeHeapMethod()
    {
        if(this.compare(values[0], values[1]) > 0)
            return;
    }
}

class MinHeap extends Heap
{
    public int compare(int i , int j)
    {
        return i + j % 2;
    }
}

class MaxHeap extends Heap
{
    public int compare(int i , int j)
    {
        return i + j % 1;
    }
}
mrhn
  • 17,961
  • 4
  • 27
  • 46
  • 2
    but both heaps have got the same methods, and the other are very similiar. If I create 2 classes I will duplicate 99% of code – Mateusz Nov 12 '15 at 22:46
  • I read your question again and the question is more complex then initially analysed by me, give an example of the comparing methods and i will show how it could be done. – mrhn Nov 12 '15 at 22:57
  • ok, but this is comparator for heap object. what if i want to change comparators of some atributes of this class? For eg. change comparator for "int value" atribute of heap class? – Mateusz Nov 12 '15 at 23:21
  • There is nothing limiting you, you can access everything from the extending classes to the Super class. But come with concrete examples, if you want code examples from me. – mrhn Nov 12 '15 at 23:22
  • In your example you have got an array of integers called "someHeapField". And when you have: minheap.someHeapField[0].compareTo(minheap.someHeapField[1]) you dont change comparer of types in that array – Mateusz Nov 12 '15 at 23:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94991/discussion-between-martin-henriksen-and-dsadsa-dsadsad). – mrhn Nov 12 '15 at 23:33
  • @dsadsadsadsad If it solved your problem, it would be nice to get the answer accepted, based on our chat yesterday, it seemed like it fixed it. – mrhn Nov 13 '15 at 13:40
0

It would be easier if you can take the compare function as an argument. For reference you could check Help with understanding a function object or functor in Java which discusses how to deal with said functions.

Community
  • 1
  • 1