O'Reilly logo

.NET Design Patterns by Shine Xavier, Praseed Pai

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required