Refactor static methods for loose coupling.
-
Hi, I have been trying to refactor the legacy code and make it loosly coupled. I came across a class which resembles as shown below.
public Class MyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public static MyClass FromBytes(byte\[\] params) { int a = BitConverter.ToInt32(params, 0); ushort b = BitConverter.ToUInt16(params, 1); int c = BitConverter.ToInt32(params, 2); return new MyClass(a, b, c); } public int A { get { return \_a; } set { \_a = value; } }
}
public Class MyConsumer
{
public void MyMethod()
{
// 1st way to create an instance
MyClass myClass1 = new MyClass(2, 3, 4);
int a = myClass1.A;// 2nd way of creating an instance byte\[\] bytes = {8, 7, 6}; MyClass myClass2 = MyClass.FromBytes(bytes); int a1 = myClass2.A; }
}
'MyClass' has been created in two ways. - creating an instance using a 'new' operator. - Through a static method 'FromBytes'. As seen above MyClass is tightly coupled to MyConsumer class. I need to provide an abstraction for MyClass. I thought of creating an Inteface 'IMyClass' which has a property 'A'.
interface IMyClass
{
int A { get; set;}
}MyClass shall have 2 overloaded constructor as shown below.
public Class MyClass : IMyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public MyClass(byte\[\] params) { \_a = BitConverter.ToInt32(params, 0); \_b = BitConverter.ToUInt16(params, 1); \_c = BitConverter.ToInt32(params, 2); } public int A { get { return \_a; } set { \_a = value; } }
}
I wanted to create an instance of MyClass in MyConsumer class through some creational pattern such as Factory. I don't know how to create an instance of a class which has its constructor overloaded. Questions: - How to provide abstraction to static methods? - How to create an instance of a class which has its constructor overloaded? I found this link useful, however it does not server my purpose of creating an object through a factory. Kindly share your view on this and let me know if you need more information. Thanks in advance!
-
Hi, I have been trying to refactor the legacy code and make it loosly coupled. I came across a class which resembles as shown below.
public Class MyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public static MyClass FromBytes(byte\[\] params) { int a = BitConverter.ToInt32(params, 0); ushort b = BitConverter.ToUInt16(params, 1); int c = BitConverter.ToInt32(params, 2); return new MyClass(a, b, c); } public int A { get { return \_a; } set { \_a = value; } }
}
public Class MyConsumer
{
public void MyMethod()
{
// 1st way to create an instance
MyClass myClass1 = new MyClass(2, 3, 4);
int a = myClass1.A;// 2nd way of creating an instance byte\[\] bytes = {8, 7, 6}; MyClass myClass2 = MyClass.FromBytes(bytes); int a1 = myClass2.A; }
}
'MyClass' has been created in two ways. - creating an instance using a 'new' operator. - Through a static method 'FromBytes'. As seen above MyClass is tightly coupled to MyConsumer class. I need to provide an abstraction for MyClass. I thought of creating an Inteface 'IMyClass' which has a property 'A'.
interface IMyClass
{
int A { get; set;}
}MyClass shall have 2 overloaded constructor as shown below.
public Class MyClass : IMyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public MyClass(byte\[\] params) { \_a = BitConverter.ToInt32(params, 0); \_b = BitConverter.ToUInt16(params, 1); \_c = BitConverter.ToInt32(params, 2); } public int A { get { return \_a; } set { \_a = value; } }
}
I wanted to create an instance of MyClass in MyConsumer class through some creational pattern such as Factory. I don't know how to create an instance of a class which has its constructor overloaded. Questions: - How to provide abstraction to static methods? - How to create an instance of a class which has its constructor overloaded? I found this link useful, however it does not server my purpose of creating an object through a factory. Kindly share your view on this and let me know if you need more information. Thanks in advance!
Constructors are a specialised kind of static methods. I often prefer a "normal" static method over a constructor when it has a specialised purpose, e.g.
public static MyClass FromFile(string filename)
. When it comes to the factory method with overloaded constructors of the classes, there are some possibilities: - use an overloaded factory method, corresponding to the overloads of the constructors - use an empty constructor and then set the properties - create an extra constructor accepting anobject[] args
parameter Personally I do not like any of those options, I doubt that the factory method is a good option in such a case. -
Hi, I have been trying to refactor the legacy code and make it loosly coupled. I came across a class which resembles as shown below.
public Class MyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public static MyClass FromBytes(byte\[\] params) { int a = BitConverter.ToInt32(params, 0); ushort b = BitConverter.ToUInt16(params, 1); int c = BitConverter.ToInt32(params, 2); return new MyClass(a, b, c); } public int A { get { return \_a; } set { \_a = value; } }
}
public Class MyConsumer
{
public void MyMethod()
{
// 1st way to create an instance
MyClass myClass1 = new MyClass(2, 3, 4);
int a = myClass1.A;// 2nd way of creating an instance byte\[\] bytes = {8, 7, 6}; MyClass myClass2 = MyClass.FromBytes(bytes); int a1 = myClass2.A; }
}
'MyClass' has been created in two ways. - creating an instance using a 'new' operator. - Through a static method 'FromBytes'. As seen above MyClass is tightly coupled to MyConsumer class. I need to provide an abstraction for MyClass. I thought of creating an Inteface 'IMyClass' which has a property 'A'.
interface IMyClass
{
int A { get; set;}
}MyClass shall have 2 overloaded constructor as shown below.
public Class MyClass : IMyClass
{
private int _a;
private ushort _b;
private int _c;public MyClass(int a, ushort b, int c) { \_a = a; \_b = b; \_c = c; } public MyClass(byte\[\] params) { \_a = BitConverter.ToInt32(params, 0); \_b = BitConverter.ToUInt16(params, 1); \_c = BitConverter.ToInt32(params, 2); } public int A { get { return \_a; } set { \_a = value; } }
}
I wanted to create an instance of MyClass in MyConsumer class through some creational pattern such as Factory. I don't know how to create an instance of a class which has its constructor overloaded. Questions: - How to provide abstraction to static methods? - How to create an instance of a class which has its constructor overloaded? I found this link useful, however it does not server my purpose of creating an object through a factory. Kindly share your view on this and let me know if you need more information. Thanks in advance!
-
Constructors are a specialised kind of static methods. I often prefer a "normal" static method over a constructor when it has a specialised purpose, e.g.
public static MyClass FromFile(string filename)
. When it comes to the factory method with overloaded constructors of the classes, there are some possibilities: - use an overloaded factory method, corresponding to the overloads of the constructors - use an empty constructor and then set the properties - create an extra constructor accepting anobject[] args
parameter Personally I do not like any of those options, I doubt that the factory method is a good option in such a case.Thanks Bernhard for the your time. If we go with the static methods, I believe it would be tightly coupled and factory seems reasonable to me...
-
Use a factory. The factory is derived from an interface. The real factory uses the methods on the class that already exists. The consumer object uses the real factory unless a static method (on option) is called to set a different factory.
Thanks jschell for your time. As per my understanding based on your inputs, we need to create a factory. So, in the sample code, it would be something like this. interface IMyClassFactory { IMyClass Create(int a, ushort b, int c); IMyClass Create(byte[] bytes); } and its implementation would be something like this. public class MyClassFactory : IMyClassFactory { public IMyClass Create(int a, ushort b, int c) { return new MyClass(a, b, c); } public IMyClass Create(byte[] bytes) { int a = BitConverter.ToInt32(params, 0); ushort b = BitConverter.ToUInt16(params, 1); int c = BitConverter.ToInt32(params, 2); return new MyClass(a, b, c); } } Consumer: public Class MyConsumer { public void MyMethod() { IMyClassFactory myClassFactory = new MyClassFactory(); // we can inject this // 1st way to create an instance IMyClass myClass1 = myClassFactory.Create(2, 3, 4); int a = myClass1.A; // 2nd way of creating an instance byte[] bytes = {8, 7, 6}; MyClass myClass2 = myClassFactory.Create(bytes); int a1 = myClass2.A; } } Please correct me if I have misunderstood something here. Appreciate, if you can explain with a small example. Is it acceptable to create an overloaded method in a factory as factory takes and parameter which defines the type of object required?
-
Thanks jschell for your time. As per my understanding based on your inputs, we need to create a factory. So, in the sample code, it would be something like this. interface IMyClassFactory { IMyClass Create(int a, ushort b, int c); IMyClass Create(byte[] bytes); } and its implementation would be something like this. public class MyClassFactory : IMyClassFactory { public IMyClass Create(int a, ushort b, int c) { return new MyClass(a, b, c); } public IMyClass Create(byte[] bytes) { int a = BitConverter.ToInt32(params, 0); ushort b = BitConverter.ToUInt16(params, 1); int c = BitConverter.ToInt32(params, 2); return new MyClass(a, b, c); } } Consumer: public Class MyConsumer { public void MyMethod() { IMyClassFactory myClassFactory = new MyClassFactory(); // we can inject this // 1st way to create an instance IMyClass myClass1 = myClassFactory.Create(2, 3, 4); int a = myClass1.A; // 2nd way of creating an instance byte[] bytes = {8, 7, 6}; MyClass myClass2 = myClassFactory.Create(bytes); int a1 = myClass2.A; } } Please correct me if I have misunderstood something here. Appreciate, if you can explain with a small example. Is it acceptable to create an overloaded method in a factory as factory takes and parameter which defines the type of object required?
public static class FactorySource
{
// There are all sorts of ways to implement this classpublic static IMyClassFactory Factory { get; set; } static FactorySource() { Factory = new MyClassFactory(); }
}
...
public void MyMethod()
{
IMyClassFactory myClassFactory = FactorySource.Factory;
...