Classes and Interface

Rule 9: Prefer class hierarchies to tagged classes

Tagged class values unneccessarily increase the footprint of object and clutters the code. this can be overcome by using seprate class hieracrchy i.e putting common implementable method to abstract class and then extending to different tagged class.

Example;
With Issue: Memory foot print and cluttered by switch

class Figure{
//tagged values
enum shape{CIRCLE,RECTANGLE}

//unnecessaary clutteredby switches
double calcutae area(){
switch(shape){
case "CIRCLE":
return 4.14*somethinf;
case "RECTANGE":
return L*B;
}
}

}

Refactored

beneifits:
1.reduced memory footprint as only specitif class gets called
2.more readable

abstract class Figure{
double area();
}

class CIRCLE extends Figure{
@overidde
double area(){
//do sonemthing
}
}


class RECTANGLE extends Figure{
@overidde
double area(){
//do sonemthing
}
}

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment