Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Refactor static methods for loose coupling.

Refactor static methods for loose coupling.

Scheduled Pinned Locked Moved Design and Architecture
sysadminregexooptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen Raghuvanshi
    wrote on last edited by
    #1

    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!

    B J 2 Replies Last reply
    0
    • P Praveen Raghuvanshi

      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!

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      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 an object[] 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.

      P 1 Reply Last reply
      0
      • P Praveen Raghuvanshi

        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!

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • B Bernhard Hiller

          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 an object[] 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.

          P Offline
          P Offline
          Praveen Raghuvanshi
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • J jschell

            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.

            P Offline
            P Offline
            Praveen Raghuvanshi
            wrote on last edited by
            #5

            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?

            J 1 Reply Last reply
            0
            • P Praveen Raghuvanshi

              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?

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              public static class FactorySource
              {
              // There are all sorts of ways to implement this class

               public static IMyClassFactory Factory { get; set; }
              
               static FactorySource()
               {
                   Factory = new MyClassFactory();
               }
              

              }

              ...
              public void MyMethod()
              {
              IMyClassFactory myClassFactory = FactorySource.Factory;
              ...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups