Are new objects constantly normal?

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;
        //----a couple of more commands
    }
}

private void GetInfo(Channel channel, string message)
{
    Match match = Regex.Match(message, "@info (.+)");
    if (match.Success)
    {
        string search = match.Groups[1].Value;
        //Get stored data on the word or sentence, and send the result to chat.
        new CommandInfo().Execute(search);  //<-------------------- over here.
        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;

        //Gets message of 'type' by 'id', and store it back after editing.
        new CommandEdit().Execute(type, id, toReplace, replaceWith); //<-here.
    }
}
+4
source share
5 answers

It's hard to tell from your sample code, but you seem to be creating procedural code with a mock top layer of object orientation.

I can't say anything about your code from your object model. For example, what exactly does this line do?

new CommandInfo().Execute(search);

What is a CommandInfo object? What does he represent? What does the function Execute () do? I have no idea.

The whole purpose of objects is that they encapsulate some kind of internal state. If you are constantly updating things, then they obviously have no condition.

, , , , ?

Message.GetInfo();
Message.Edit();

... , , , , ?.

+2

, , CommandEdit CommandInfo -, Execute

private CommandInfo mInfo = new CommandInfo();

private void GetInfo(Channel channel, string message)
{
    Match match = Regex.Match(message, "@info (.+)");
    if (match.Success)
    {
        string search = match.Groups[1].Value;
        //Get stored data on the word or sentence, and send the result to chat.
        mInfo.Execute(search);  //<-------------------- over here again.
        return;
    }
    Chat.SendAdminMessage("Message not found.");
}
+3

, 1-1. - , , , , .

:

public class Work
{
 private static readonly Lazy<Work> _work = new Lazy<Work>(()=> new Work());

 public static Work Instance{get{return _work.Value;}} 

 public void DoWork(){}
}

:

Work.Instance.DoWork();
+1

, , ? , , :

public class myClass{
...
private ICommandInfo commandInfo= new CommandInfo();

private void GetInfo(Channel channel, string message)
{
    Match match = Regex.Match(message, "@info (.+)");
    if (match.Success)
    {
        string search = match.Groups[1].Value;
        //Get stored data on the word or sentence, and send the result to chat.
        commandInfo.Execute(search);  //<-------------------- no object creation.
        return;
    }
    Chat.SendAdminMessage("Message not found.");
}
...
}

, ICommandInfo, .

+1

, - , . ( , ) .

public class Consumer
{
    private ICommandService _commandInfo;

    public Consumer(ICommandService commandInfo)
    {
        _commandInfo = commandInfo;
    }

    public void DoSomething()
    {
        _commandInfo.Execute();
    }
}

. Singleton.

. (. Mock object).


, Execute, CommandInfo , . : "... ". , ?

_chat.Send(search);

, new CommandInfo().Execute(search);, . , , , - . , , .

+1

Source: https://habr.com/ru/post/1528998/


All Articles