Question

I am following this link: http://codebetter.com/jpboodhoo/2007/10/15/the-static-gateway-pattern/ to understand the Gateway pattern.

The author shares a sample of the "gateway" logger class and related interfaces:

public class Log
{
    private static ILogFactory logFactory;

    public static void InitializeLogFactory(ILogFactory logFactory)
    {
        Log.logFactory = logFactory;
    }

    public void InformationalMessage(string informationalMessage)
    {
        logFactory.Create().InformationalMessage(informationalMessage);
    }
}

public interface ILogFactory
{
    ILog Create();
}

public interface ILog
{
    void InformationalMessage(string message);
}

This is the calling API

public class Calculator
{                
public int Add(int number1,int number2)
{
Log.InformationalMessage("About to add two numbers");            
return number1 + number2;
}
}

I am unable to understand where the initialization of the concrete logging class happens here. What is the entry point of the gateway?

Was it helpful?

Solution

With regards to concrete implementation of the Interfaces, there are examples at the bottom of the article which show how it can be implemented.

OTHER TIPS

The way the example is laid out is a bit deceiving, as the calling of the API is more relevant to the first example. Note the test cases that follow where the init is called. In addition you would have a Log instance which the author does not have, being a bit more symbolic in the example. The other option is a static Log class.

EDIT: Disregard this please, it is incorrect. Only reason I am not deleting is so my comment responding to the person who pointed it out can stay on.

Note that the 'Log` class has static constructor:

public static void InitializeLogFactory(ILogFactory logFactory)
    {
        Log.logFactory = logFactory;
    }

The first time the Log class is loaded (upon first reference), the static constructor will run, initializing its logFactory with a concrete realization of the factory.

Then, every time InformationalMessage is called, the concrete factory is used to create an instance of a logger to log the message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top