ANSWER: Even if you instead turn the classes into variables and then call them it will work, in this case this is most likely a sign of poor design. A better design will have a side effect, because of which it is not necessary to start creating new instances of classes.
=============
Recently, I (try) to apply SRP to my code, dividing all the responsibilities into separate classes. It worked very well, maintainability and code reuse increased ... but I find myself constantly doing things like new Table.Type("messages").Get(id);.
Most of my classes contain only one method. However ... he feels awkward. The Gites say this is where I could turn them all into static classes.
So, I decided that I would turn to my more experienced elderly people, often write a "new class" (). Is the () method usually done? Or is there a better way to handle this?
Code example:
public void AdminCommands(Channel channel, IrcUser user, string message)
{
var command = message.Split(' ')[0];
switch (command)
{
case "@info":
GetInfo(channel, message);
break;
}
}
private void GetInfo(Channel channel, string message)
{
Match match = Regex.Match(message, "@info (.+)");
if (match.Success)
{
string search = match.Groups[1].Value;
new CommandInfo().Execute(search);
return;
}
Chat.SendAdminMessage("Message not found.");
}
private void EditMessage(Channel channel, string message)
{
Match match = Regex.Match(message, "@edit (.+?) (.+?) (.+?)=(.+)");
if (match.Success)
{
string type = match.Groups[1].Value;
string id = match.Groups[2].Value;
string toReplace = match.Groups[3].Value;
string replaceWith = match.Groups[4].Value;
new CommandEdit().Execute(type, id, toReplace, replaceWith);
}
}
source
share