Is it ok(for java and SOLID principals) to extend abstract classes in order to set properties from the super class? What I want to achieve is to apply different values to p2 depending on what p1 has and also p3 values will depend of what the value of p2 is(something similar when you cascade drop down menus). Could this scenario be dealt with a design pattern?
public interface Iface{
public void set_p1(int i);
public void set_p2(int i);
public void set_p3(int i);
}
public abstract class A implements Iface{
private int p1;
private int p2;
private int p3;
public void set_p1(int i){
this.p1 = i;
}
public void set_p2(int i){
this.p2 = i;
}
public void set_p3(int i){
this.p3 = i;
}
}
Here I set p1 to 100
public abstract class setOne extends A {
public setOne(){
set_p1(100);
}
}
now I set p2 depending on the values of p1
public abstract class setTwo extends setOne {
public setTwo(){
//do some work using p1
set_p2(200);
}
}
now I instantiate my setTwo abstract class
public class TestClass extends setTwo {
public TestClass(){
super();
}
}
TestClass myObj = new TestClass();
now I expect the values of the object as follows:
myObj.p1 = 100
and
myObj.p2 = 200;
This is something complex implantation. I feel that you over think about single responsible principle.
The design based on what you have to do depend on p1 and p2. you can just use POJO to over come this.
public abstract class A {
private int p1;
private int p2;
private int p3;
public void setP1(int i){
this.p1 = i;
}
public void setP2(int i){
//do some work using p1
this.p2 = i;
}
public void setP3(int i){
this.p3 = i;
}
}
If you have to perform some advance logic base on value you can provide implementation to setP2 method.
public interface NumberFunction {
int doCalculate(int number);
}
And modify function as below.
public void setP2(int i, NumberFunction function){
int functionResult = function.doCalculate(p1);
this.p2 = i;
}
So number function hold responsible to modify your numbers. and A class responsible to keep values.
Hope this will helps you.