首页 > 解决方案 > 从两个不同的类有条件地调用方法

问题描述

我有两个班级ExceptionLogDebugLog.

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

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   
    }        

}

我想要实现的是,编写一个控制器类来决定debug需要调用哪个方法

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);
    }
}

也就是说,如果我在调试方法中传递 Exception ,它将调用ExceptionLog.Debug(ex)else 它将从DebugLog类中调用调试方法。

我怎样才能更优雅地设计课程或任何适合这里的设计模式?

标签: oopdesign-patternssalesforceapex

解决方案


您可以发送一个未键入的参数并在 Log 类中找到类型,如下所示:

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);
    }
}

或者,您可以实现 Apex Callable 接口,该接口旨在处理通用参数的映射。您的类将call(String action, Map<String, Object> args)定义一个方法。通过 switch case 传递操作将确定调用哪个方法。


推荐阅读