Reputation: 507
I have a class with a lot of functions in it:
MyClass.cs Methods:
Login
Upload file
Upload Folder
Upload large file
Download
and many more.
the problem is that the class grows and grows, and it's starting to be not comfortable to write the whole methods in the same class.
In one hand I want to invoke the functions like:
var myclass=new MyClass();
myclass.SomeMethod();
but the price is to put all the classes in the same file.
I there any design pattern for this? or any other ideas would be appreciated
Upvotes: 2
Views: 903
Reputation: 9610
What about using dependency inversion?
You can then define your main class such as this
public class MyClass
{
public IUserManagement UserManager { get; set; }
public IFileManagement FileManager { get; set; }
public IFolderManagement FolderManager { get; set; }
public MyClass()
{
}
}
With these interfaces
public interface IUserManagement
{
void Login(string username, string password);
void Logout();
}
public interface IFileManagement
{
void UploadFile(string path);
void UploadLargeFile(string path);
void DownloadFile(string filename, string savePath);
}
public interface IFolderManagement
{
void DownloadFolder(string path);
void UploadFolder(string path);
}
The Bulk of your code then goes into classes which perform a specific task such as this.
public class MyFileManager : IFileManagement
{
public void DownloadFile(string filename, string savePath)
{
//Add code here
}
public void UploadFile(string path)
{
//Add code here
}
public void UploadLargeFile(string path)
{
//Add code here
}
}
public class MyUserManager : IUserManagement
{
public void Login(string username, string password)
{
//Add code here
}
public void Logout()
{
//Add code here
}
}
public class MyFoldermanager : IFolderManagement
{
public void DownloadFolder(string path)
{
//Add code here
}
public void UploadFolder(string path)
{
//Add code here
}
}
You can then create your class and call methods likes this
MyClass myClass = new MyClass()
{
FileManager = new MyFileManager(),
FolderManager = new MyFoldermanager(),
UserManager = new MyUserManager()
};
myClass.FileManager.DownloadFile("myfilename", @"c:\path\to\file.txt");
If you wanted, then you could also add wrappers in your MyClass to hide some of the complexity such as this.
//Add in MyClass
public void UploadFile(string path)
{
this.FileManager.UploadFile(path);
}
//Called like this
myClass.UploadFile(@"c:\path\to\other\file.txt");
This way, you can delegate responsibility for each functional area to a specific class which is easier to manage.
Upvotes: 3
Reputation: 3784
Just extract the code inside the methods of your MyClass
to other classes. In MyClass
you just call/delegate to the other classes.
This way you can reduce the amount of code in MyClass
without reducing its method list.
Upvotes: 1