multiple inheritance - JAVA - extends vs interface - Strategy design pattern -
i have scenario multiple concrete classes extends
multiple abstract
classes. @ loss come clean structure, reduce number of files , avoid code repetition.
the ask display various sensor values in different ways based on criteria. sensor values temperature, voltage, current etc can have anlaog widget, numeric label or combination of both. have 3 abstract
classes 3 different kind of views. these 3 abstract
classes implement method defines how view drawn. each sensor view extends
3 abstract
classes , implements methods read sensor, perform engineering conversion , display sensor value. problem here code implemented concrete classes same no matter abstract
class extends. canalogtempview
, cdigitaltempview
, ccustomtempview
have same implementation extend different classes.
this seems awkward. code repeated , number of source files increases factor of 3. missing simple here? there pattern problem this? can extends
sensor view classes @ run time? actual code more complicated. have on simplified problem statement clarity.
edit: there several sensor views implement abstract
view classes. calculate()
method each sensor different. have listed 3 concrete classes simplicity. in same vein, have canalogvoltageview
, cdigitalvoltageview
, ccustomvoltageview
, canalogcurrentview
, cdigitalcurrentview
, ccustomcurrentview
, on
public abstract class cview { public abstract void draw(); public abstract void calculate(); } public abstract class canalogview extends cview { public void draw() { // draw specific analog view } } public abstract class cdigitalview extends cview { public void draw() { // draw specific digital view } } public abstract class ccustomview extends cview { public void draw() { // draw specific custom view } } // concrete implementations public class canalogtempview extends canalogview { public void calculate() { // read , calculate sensor value here } } public class cdigitaltempview extends cdigitalview { public void calculate() { // calculate here. same canalogtempview::calculate() } } public class ccustomtempview extends ccustomview { public void calculate() { // calculate here. same canalogtempview::calculate() } }
strategy design pattern you. 1 thing should kept in mind.
use less
extends
keyword possible, better use interfaces or composition.
solution:
encapsulate calculate()
because calculate()
may change in future , has different implementations.
process:
1) make interface calcinterface
has calculate()
.
2) implement calcinterface
3 different classes canalogtempcalc
, cdigitaltempcalc
, ccustomtempcalc
. implement calculate()
in each class.
3) time composition
. have sensor
class (main class) ... make object of type calcinterface
. ps: has display()
common all.
4) , make 3 different classes extends sensor
analogsensor
, tempsensor
, customsensor
...
5) @ runtime make of type's ( canalogtempcalc
, cdigitaltempcalc
, ccustomtempcalc
) object.
edit :
following class diagram, not art ... diagram give idea classes , interfaces , how use them efficiently.
now able implement many customcalcuations want implementing calcinterface
...
this power accommodate change without changing current implementation have following strategy design pattern.
Comments
Post a Comment