oop - Temporary extending existing object in Java - is this a good idea? -
i'm implementing algorithm during must temporary order existing objects (we compare them according order during execution of algorithm). i'm thinking of best way of doing that, while being consistent oop paradigm.
so let's think following example. have objects of class car, , want use algorithm on such objects. thought of making subclass orderedcar, have unique int in fields. class have function - arraylist<orderedcar> defineorder(arraylist<car> order), output orderedcar list numbers corresponding indices of given car in order table. able compare orderedcars using numbers initialized. execute algorithm on orderedcars , convert them cars after algorithm terminates.
during algorithm need of methods car class, that's why thought of making orderedcar subclass. idea though? also, how create constructor in java, "copy" car , assign number (i'm thinking of public orderedcar(car c, int order), car c in order "copy" it? don't want copy of fields individually, there kind of shortcut in java?)
you favor composition on inheritance , create class contains car , order:
public class orderedcar{ private int order; private car car; public orderedcar(int order, car car){ this.order = order; this.car = car; } public int getorder(){ return order; } public car getcar(){ return car; } } then can sort class order, , whenever needed operate on car, call getcar() function.
Comments
Post a Comment