I have implemented the following code:
class A
{
abstract void f1(Object obj, Object data);
}
class A1:A
{
void f1(Object obj, Object data)
{
m1(obj,data);
}
void m1(Object obj, Object data)
{
}
}
class A2:A
{
void f1(Object obj, Object data)
{
m2(obj, data);
}
void m2(Object obj, Object data)
{
}
}
static class Factory
{
public static A GetInstance(int i)
{
if (i == 1)
return new A1();
else if (i == 2)
return new A2();
else
return null;
}
}
Methods are being called from some other part of the application, as following:
var a = Factory.GetInstance(1);
a.f1(obj, data);
In the above code, the abstract method f1()
has two parameters obj
and data
. While I reviewed the code, I found the parameter data
can be determined from the first parameter obj
itself. The area of the application from where the method f1()
is being called, there the Libraries for getting data are not available. So I refactored the code further as following in order to send one parameter obj
. Following is the implementation:
class A
{
public Object data { get; set; }
abstract void f1(Object obj);
}
class A1:A
{
void f1(Object obj)
{
m1(obj,data);
}
void m1(Object obj, Object data)
{
}
}
class A2:A
{
void f1(Object obj)
{
m2(obj, data);
}
void m2(Object obj, Object data)
{
}
}
static class Factory
{
public static A GetInstance(int i)
{
if (i == 1)
return new A1();
else if (i == 2)
return new A2();
else
return null;
}
}
Now, I am calling the method as following:
var a = Factory.GetInstance(1);
a.data = GetData();
a.f1(obj);
Please let me know whether or not the refactoring is correct. Also, please suggest some ways for better refactoring.