0

Basically I have a class with a private method and lots of public methods that call this private method. I want to group these public methods logically (preferably to separate files) so it'll be organized, easier to use and maintain.

public class MainClass
{
    private void Process(string message)
    {
        // ...
    }

    public void MethodA1(string text)
    {
        string msg = "aaa" + text;
        Process(msg);
    }
    public void MethodA2(int no)
    {
        string msg = "aaaaa" + no;
        Process(msg);
    }

    public void MethodB(string text)
    {
        string msg = "bb" + text;
        Process(msg);
    }

    // lots of similar methods here
}

Right now I'm calling them like this:

MainClass myMainClass = new MainClass();
myMainClass.MethodA1("x");
myMainClass.MethodA2(5);
myMainClass.MethodB("y");

I want to be able to call them like this:

myMainClass.A.Method1("x");
myMainClass.A.Method2(5);
myMainClass.B.Method("y");

How can I achieve it? There is probably an easy way that I'm not aware of.

user1061894
  • 109
  • 2
  • 11
  • If you want to group the methods logically via separate files, it sounds like the methods should be moved to their own classes. Can you elaborate more on what you'r trying to achieve? – Tim Mar 11 '17 at 19:57
  • You are right. They should be in their own classes. What I can't figure out is that how they would use main class's methods, be a member of main class and be a separate class at the same time. Hope I'm making sense. – user1061894 Mar 11 '17 at 20:15

3 Answers3

5

You're looking for object composition.

In computer science, object composition (not to be confused with function composition) is a way to combine simple objects or data types into more complex ones.

BTW, you shouldn't think that such refactor is grouping methods logically: it's just you need to implement your code with a clear separation of concerns:

In computer science, separation of concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern.

Practical example:

public class A
{
    // B is associated with A
    public B B { get; set; }
}

public class B
{
     public void DoStuff() 
     {
     }
}

A a = new A();
a.B = new B();
a.B.DoStuff();
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I also need to run a method of A from B. So I should pass A as an argument to B or better use events, right? – user1061894 Mar 12 '17 at 10:11
  • @user1061894 Bad idea... Avoid cyclic references... Probably you need to design a third class with that shared method, and then inject an instance of let's say `C` into `A` and `B`. BTW if you want a definitive answer to this concern, please update your answer and share actual code instead of pseudo-code.. – Matías Fidemraizer Mar 12 '17 at 20:59
2

You may move methods to separate classes. Classes may be new classes with dependency to MainClass with public/internal Process method, nested in MainClass or inherited from MainClass. Example with inheritance:

public class MainClass
{
   protected void Process(string message)
   {
    // ...
   }
}

public class A: MainClass
{
    // methods for A go here
}

public class B: MainClass
{
    // methods for B go here
}
Set
  • 47,577
  • 22
  • 132
  • 150
1

You can use nested classes:

public class MainClass
{
    // private method here

    public class A
    {
        // methods for A go here
    }

    public class B
    {
        // methods for B go here
    }
}

If you want them in different files, you can use a partial class for MainClass

// file 1
public partial class MainClass
{
    public class A { }
}

// file 2
public partial class MainClass
{
    public class B { }
}
itsme86
  • 19,266
  • 4
  • 41
  • 57