Is it better to accept an object argument or use a member object?

I have a class that I can write as follows:

class FileNameLoader
{
     public:
         virtual bool LoadFileNames(PluginLoader&) = 0;
         virtual ~FileNameLoader(){}
};

Or that:

class FileNameLoader
{
     public:
         virtual bool LoadFileNames(PluginLoader&, Logger&) = 0;
         virtual ~FileNameLoader(){}
};

The first assumes that FileNameLoaderthere is a member in the implementation Logger&. The second is not. However, I have several classes that have many methods that they use Logger. So the second way would make me write more code in this case. Logger- monophonic moment. I suppose that will be so. What is even more “beautiful” of the two and why? What is the usual practice?

EDIT: What if this class was not named Logger? :). I have and Builder. How about that?

+5
source share
6

, ( !), infact , , , , Logger - ...

, ? , ?

+7

, . ( , Logger).

+5

, , Logger . FileNameLoader Logger&?

Logger a Singleton. , , , . , Log, , std::ostream . Singleton Logger - , . , - Logger? , , ? , , , PrefixLogger, Logger, , . .

-, , FileNameLoader LoadFileNames , .

, Logger , Logger (a) API (b) : . logger 0, 0 " -". , , FileNameLoader , LoadFileNames, .

, Logger , - . - , , .

[ Builder: , , - . , "Builder, FileNameLoader", , "Builder, ", , LoadFileNames .

, Builder Singleton. . .]

+3

, , . , , "" - , , , .

, Logger , , FileNameLoader , .

.

, , . :

  • ( ).
  • ( , , , , ...)
+1

Logger singleton. , , - . , . (, Singleton < > ):

class Logger : public Singleton<Logger>
{
public:
    void Log(const std::string& _sink, const std::string& _data);
};

:

class FileNameLoader
{
 public:
     virtual bool LoadFileNames(PluginLoader& _pluginLoader)
     {
         Logger.getSingleton().Log("FileNameLoader", "loading xyz");
     };

     virtual ~FileNameLoader(){}
};

Log Manager , . Log() , , . , , .

+1

, , - Logger ( , ).
, , , .

This has the advantage over your second approach that you cannot (accidentally) create a situation where members of the same class cannot be easily identified as such in the log files.

0
source

All Articles