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

Class.asSubclass signature
How to print logs in color using log4j2 highlight pattern?
Play video using vlcj in ubuntu
Struts2 performance impact on using tags & OGNL
Get attribute of inner node in XML
How to convert ASCII values to RGB values in Java?
NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
Is java bytecode translated directly to assembler code? [closed]
Spring MVC web application - Permission mechanism for a bank application
assert Vs if(var != null) which way is better or is correct?
ClasscastException on update application
Android: How to set the ImageView src in a list dynamically
How to synchronize a code which is deployed on a cluster?
How to bind data in a JavaFX Cell
Archiva / Maven as corporate repository
how to get the existed wireless network characteristics and the activity executed once only

Categories

HOME
gitlab
webpack
fluentd
single-sign-on
fme
relay
objectgears
twitter-bootstrap-4
jgroups
webpack-2
callback
esper
amazon-cloudformation
elasticsearch-hadoop
cloudkit
python-unittest
modx-revolution
windows-server-2012
hapi
worldwind
solaris-10
connection-string
amazonsellercentral
dcevm
shopware
pingfederate
strncpy
ejabberd-module
srcset
jspresso
vsts-build-task
gammu
http-digest
serve
sqlite2
dism
galen
nand2tetris
mime
qsslsocket
python-c-api
pnotify
blogengine.net
hls.js
elgg
lift-json
android-cursor
media-player
cudafy.net
flow-control
imanage
hittest
orthogonal
quartz-composer
deadbolt-2
nxt
instant
login-control
bstr
player
asp.net5
wapiti
freelancer.com-api
separator
xna-4.0
ionic
cakephp-3.1
myo
generic-programming
angular-local-storage
pretty-print
clipperlib
inmobi
offloading
c3
device-manager
websocket4net
issuu
starcluster
valuechangelistener
datagridviewcolumn
resty-gwt
prettify
commoncrypto
apc
listings
easy-install
html4
chronoforms
cassini-dev
delphi-6
sublist
gil
dsn
jquery-ui-layout
eclipse-templates
gwt-ext
hirefire
sproutcore-2
icanhaz.js
thunderbird-lightning
multi-tier
photoshop-cs4
aquaticprime
putchar
ubuntu-9.04
w3wp
msdev

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App