The factory method pattern for instantiation

Now, we should take care of the instantiation of the right object based on the parameter received (often retrieved  from a  configuration file )  to identify the strategy.

Note

We will use the GoF factory method pattern to instantiate the LogStrategy object. By checking the loggertype parameter, the appropriate concrete class will be instantiated.

    public static LogStrategy CreateLogger(string loggertype) 
    { 
      if (loggertype == "DB") 
        return new DbLogStrategy(); 
      else if (loggertype == "FILE") 
        return new FileLogStrategy(); 
      else if (loggertype == "NET") 
        return new NetLogStrategy(); 
      else 
        return new NullLogStrategy(); 
    } 

The application developer can also control the logging strategy through a configuration entry. ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.