2

I have two classes ExceptionLog and DebugLog.

public class ExceptionLog {
    public static String StackTrace {get; set;}
    public static String ClassName {get; set;}
    public static String MethodName {get; set;}
    public static String LogType {get;set;}
    public static Exception ex {get;set;}

    public Static void Debug(Exception ex)
    {
        logType = 'EXCEPTION'; 
        ex = ex;
        log();
    }

    public Static void log()
    {
        try
        {
            extractException(); 
            writeToObject(); 

        }
        catch(Exception e)
        {
            //new ExceptionLog().Module('LogException').log(e);            
        }    
    }

    public static void extractException()
    {
        // Logic here            
    }

    public static void writeToObject()
    {        
        // data save to object logic here       
    }    
}

and

public class DebugLog {
    public static String LogType {get;set;}
    public static String DebugMessage {get;set;} 

    public Static void Debug(String message)
    {
        Debug(null, message);
    }

    public Static void Debug(LoggingLevel level, String message)
    {
        if(level != null )
        {
            LogType = String.valueOf(level);             
        }        

        DebugMessage = message;

        log();
    }

    public Static void log()
    {
        // Log logic here   
    }        

}

What I want to achieve is, write a controller class that will decide which debug method needs to be called

public class Log {
    public void Debug(String message)
    {
        DebugLog.Debug(message);
    }
    public void Debug(loggingLevel loggingLevel, String message)
    {
        DebugLog.Debug(loggingLevel, message);    
    }
    public void Debug(Exception ex)
    {
        ExceptionLog.Debug(ex);
    }
}

That is, if I pass Exception in the debug method, it will call the ExceptionLog.Debug(ex) else it will call the debug method from DebugLog class.

How can I design the classes more elegantly or any design pattern fit here?

Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48

1 Answers1

1

You can send in an un-typed argument and find the type in the Log class as seen below:

public class Log {
    public void debug(Object genericLog){
        if(genericLog instanceof String){
            DebugLog.Debug(String.valueOf(message));
        }
        else if(genericLog instanceof Exception){
            ExceptionLog.Debug((Exception)genericLog);
        }
        else if(genericLog instanceof DebugWrapper){
            DebugWrapper wrapper = (DebugWrapper)genericLog;
            DebugLog.Debug(wrapper.loggingLevel, wrapper.message);
        }
        else{
            //do whatever
        }
    }

    public void debug(String message, LoggingLevel loggingLevel){
        debug(wrapLog(message, loggingLevel));
    }

    public class DebugWrapper{
        public String message;
        public LoggingLevel loggingLevel;

        public DebugWrapper(String message, LoggingLevel loggingLevel){
            this.message = message;
            this.loggingLevel = loggingLevel;
        }
    }

    public DebugWrapper wrapLog(String message, LoggingLevel loggingLevel){
        return new DebugWrapper(message, loggingLevel);
    }
}

Alternatively, you can implement the Apex Callable interface which is designed to handle a map of generic arguments. Your class would have a call(String action, Map<String, Object> args) method defined. Passing the action through a switch case will determine which method is called.

TechingCrewMatt
  • 486
  • 2
  • 7