java


How to inform method which comparision use


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
}
}
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;
}
}
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.

Related Links

Multiple db appender in log4j?
Want to Add #JsonIgnore property on Password in response Json
Hibernate configuration hbm2ddl.auto
BNF for Java Input Staments
Does polymorphism apply on class attributes in Java?
how do i make it possible for a window with buttons program to run?
Program skips over System.in
Why can't outer classes extend inner classes?
Creating a Custom IntStream-like class
What is going on underneath with this lambda
File path problems in windows environment
Efficient algorithm for finding the in-order rank of a binary tree node
Reduce verbosity of Tomcat Digester logger in log4j.xml
spring error :org.springframework.web.context.ContextLoader - Context initialization failed
How to #Autowire a SessionFactory to a Servlet?
Why can we assign FutureTask<V> object to Future<V> variable?

Categories

HOME
ibm-bluemix
netsuite
kde
angular-material
iot
datastax-java-driver
azure-media-services
dacpac
kentor-authservices
libtiff
solaris-10
connection-string
pythonanywhere
beyondcompare
arabic
icloud-api
trading
pepper
linkerd
buck
wtx
c++-amp
newline
unboundid
amazon-kinesis-kpl
revolution-slider
abstract-class
uft-api
greenrobot-eventbus
janrain
ibpy
eigenvalue
password-encryption
midl
vao
businessworks
apple-news
avconv
osx-mavericks
atomicity
pango
paxos
dropbox-php
yii2-extension
directory-structure
memory-alignment
django-scheduler
specrun
google-cdn
parallel-data-warehouse
url-pattern
passport-google-oauth
elements
transmitfile
fputcsv
markojs
freedesktop.org
hsv
unity-networking
tcpserver
lib.web.mvc
ora-00900
google-places
thredds
comobject
quicklisp
kcachegrind
sniffer
p2
directoryservices
bigbluebutton
facebook-graph-api-v2.4
typekit
asp.net-dynamic-data
relocation
rdl
bitcoinj
project-planning
mesa
flexmojos
c18
geos
aqtime
dbconnection
dotnetnuke-5
plasma
comaddin
gil
coderush
frameset
osql
punbb
cinema-4d
asp.net-mvc-areas
wise
icanhaz.js
gin
ext3
rescale
ncqrs
sector
paul-graham
caching-application-block
document-conversion

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App