2

I'm a newbie that has just started diving in this world of Design Patterns by reading the Judith Bishop book "C# 3.0 Design Patterns".

As an exercise at the end of the Proxy chapter, the author suggests to implement a custom class that "intercepts, monitors and logs every call to Stream methods" using the Proxy pattern.

I'm a bit unsure of what I have come with and I wanted to ask for advice. After some thoughts I discarded (eg. starting with an Interface exposing some of the most used Stream methods), I finally created a class that inherits from Stream and overrides some of its methods. This class also has a FileStream property which I use to call the actual Stream methods and properties, which I previously have wrapped with the methods to log every of their calls:

    public class MyStream : Stream
    {
        private FileStream _stream;

        public MyStream()
        {
            _stream = new FileStream();
        }

        public override bool CanRead
        {
            get 
            {
                LogCall("CanRead");
                return _stream.CanRead;
            }
        }

        //More stream methods to override...
    }

However, this seems more like a Decorator to me. Besides, a "bad" Decorator since the exercise ask about the "Stream methods" and I'm using a "FileStream" as an actual class.

I don't feel this is the most accurate example of a Proxy pattern and I'm a bit unsure if I have understood the exercise.

Any advice on how I should approach this exercise? Thanks in advance,

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Moz
  • 21
  • 2
  • You can prevent the dependency on an actual stream implementation like FileStream by changing your constructor to take a Stream as argument. This is often refered to as dependency injection. – Marwijn Jan 27 '14 at 09:41

0 Answers0