none
Chain of Responsibility Pattern In C# RRS feed

  • Question

  • public abstract class AbstractLogger {
       public static int INFO = 1;
       public static int DEBUG = 2;
       public static int ERROR = 3;
    
       protected int level;
    
       protected AbstractLogger nextLogger;
    
       public void setNextLogger(AbstractLogger nextLogger){
          this.nextLogger = nextLogger;
       }
    
       public void logMessage(int level, String message){
          if(this.level <= level){
             write(message);
          }
          if(nextLogger !=null){
             nextLogger.logMessage(level, message);
          }
       }
    
       abstract protected void write(String message);
        
    }
    public class ConsoleLogger extends AbstractLogger {
    
       public ConsoleLogger(int level){
          this.level = level;
       }
    
       @Override
       protected void write(String message) {        
          System.out.println("Standard Console::Logger: " + message);
       }
    }
    public class ErrorLogger extends AbstractLogger {
    
       public ErrorLogger(int level){
          this.level = level;
       }
    
       @Override
       protected void write(String message) {        
          System.out.println("Error Console::Logger: " + message);
       }
    }
    public class FileLogger extends AbstractLogger {
    
       public FileLogger(int level){
          this.level = level;
       }
    
       @Override
       protected void write(String message) {        
          System.out.println("File::Logger: " + message);
       }
    }
    public class ChainPatternDemo {
        
       private static AbstractLogger getChainOfLoggers(){
    
          AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
          AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
          AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);
    
          errorLogger.setNextLogger(fileLogger);
          fileLogger.setNextLogger(consoleLogger);
    
          return errorLogger;    
       }
    }
    Wednesday, December 20, 2017 2:49 PM

Answers

  • And you posted the code. Why?

    Probably for the same mysterious reason she started this other thread:

    Decorator Pattern In C#
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3100a195-15b3-454e-9687-5610b7dd52cd/decorator-pattern-in-c?forum=csharpgeneral

    - Wayne

    • Marked as answer by Audrey She Sunday, December 24, 2017 3:04 PM
    Wednesday, December 20, 2017 9:59 PM
  • Hello Audrey,

    The code you provide is using Java language, If you want to convert the java to C#, please use code converter by yourself rather than post all code.

     public abstract class AbstractLogger
        {
            public static int INFO = 1;
            public static int DEBUG = 2;
            public static int ERROR = 3;
            protected int level;
            protected AbstractLogger nextLogger;
            public void setNextLogger(AbstractLogger nextLogger)
            {
                this.nextLogger = nextLogger;
            }
            public void logMessage(int level, String message)
            {
                if (this.level <= level)
                {
                    write(message);
                }
                if (nextLogger != null)
                {
                    nextLogger.logMessage(level, message);
                }
            }
            abstract protected void write(String message);
        }
        public class ConsoleLogger : AbstractLogger
        {
            public ConsoleLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("Standard Console::Logger: " + message);
            }
        }
        public class ErrorLogger : AbstractLogger
        {
            public ErrorLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("Error Console::Logger: " + message);
            }
        }
        public class FileLogger : AbstractLogger
        {
            public FileLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("File::Logger: " + message);
            }
        }

    For more detailed about chain of responsibility pattern, you could see here.

    https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 21, 2017 5:26 AM
    • Marked as answer by Audrey She Sunday, December 24, 2017 3:04 PM
    Thursday, December 21, 2017 5:25 AM
    Moderator

All replies

  • And you posted the code. Why?

    Wednesday, December 20, 2017 6:51 PM
  • And you posted the code. Why?

    Probably for the same mysterious reason she started this other thread:

    Decorator Pattern In C#
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3100a195-15b3-454e-9687-5610b7dd52cd/decorator-pattern-in-c?forum=csharpgeneral

    - Wayne

    • Marked as answer by Audrey She Sunday, December 24, 2017 3:04 PM
    Wednesday, December 20, 2017 9:59 PM
  • Hello Audrey,

    The code you provide is using Java language, If you want to convert the java to C#, please use code converter by yourself rather than post all code.

     public abstract class AbstractLogger
        {
            public static int INFO = 1;
            public static int DEBUG = 2;
            public static int ERROR = 3;
            protected int level;
            protected AbstractLogger nextLogger;
            public void setNextLogger(AbstractLogger nextLogger)
            {
                this.nextLogger = nextLogger;
            }
            public void logMessage(int level, String message)
            {
                if (this.level <= level)
                {
                    write(message);
                }
                if (nextLogger != null)
                {
                    nextLogger.logMessage(level, message);
                }
            }
            abstract protected void write(String message);
        }
        public class ConsoleLogger : AbstractLogger
        {
            public ConsoleLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("Standard Console::Logger: " + message);
            }
        }
        public class ErrorLogger : AbstractLogger
        {
            public ErrorLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("Error Console::Logger: " + message);
            }
        }
        public class FileLogger : AbstractLogger
        {
            public FileLogger(int level)
            {
                this.level = level;
            }
            protected override void write(string message)
            {
                Console.WriteLine("File::Logger: " + message);
            }
        }

    For more detailed about chain of responsibility pattern, you could see here.

    https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 21, 2017 5:26 AM
    • Marked as answer by Audrey She Sunday, December 24, 2017 3:04 PM
    Thursday, December 21, 2017 5:25 AM
    Moderator