Let say I have the following interface,
interface Component{
public String name();
public Integer id();
}
class ButtonComponent implements Component{
public String name(){
return "button";
}
public Integer id(){
return 123;
}
}
Then I'm using the component like this.
class ComponentHolder {
public static Component getButtonComponent(){
return new ButtonComponent();
}
}
Component component = ComponentHolder.getButtonComponent();
component.name();
component.id();
Later on, I have the new component called ImageComponent and I want to get dimension of that image so how I can add the dimension method in the ImageComponent without editing the parent because if I edit the parent, I have to define the dimension in the ButtonComponent also.
class ImageComponent implements Component{
public String name(){
return "Image"
}
public String id(){
return 111;
}
public String dimension(){
return new int[]{300,250};
}
}
class ComponentHolder {
public static Component getButtonComponent(){
return new ButtonComponent();
}
public static Component getImageComponent(){
return new ImageComponent();
}
}
Component component = ComponentHolder.getImageComponent();
component.name();
component.id();
component.dimension(); // Compile Time Error, no dimension is defined in the Component Class.
Aucun commentaire:
Enregistrer un commentaire