CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Dec 2018
    Posts
    15

    Creating Basic Wrapper Class in C#

    Creating a Basic Wrapper.
    These three patterns, proxy, decorator, and adapter are all implemented identically. They are all wrappers at the basic level. The functionality they provide is how we can make the distinction between them. Let's first look at a template for a basic wrapper and then at how to use a wrapper to implement each of these patterns can help us in our quest for the bizarre.
    Before we begin to understand how to perform super-feats, we need to understand the basic super-power all C# developers are born with - our secret ability to wrap (not to be confused with rap). Here is how a wrapper works:
    Let's say we have a Thing with a ThingString property and a method PrintThingString() as below:

    Code:
    class Thing
    {
              private string m_string;
              public string ThingString
              {
                        get { return m_string; }
                        set { m_string = value; }
              }
              public void PrintThingString()
              {
                        Console.WriteLine(m_string);
              }
    }
    Here is how we create a basic ThingWrapper to wrap the Thing. It contains a reference to the Thing being wrapped and has the same signature. The ThingWrapper just passes requests into the wrapped Thing.

    Code:
    class ThingWrapper
    {      
              private Thing m_thing;
              public ThingWrapper(Thing pThing)
              {
                        m_thing = pThing;
              }
              public string ThingString
              {
                        get { return m_thing.ThingString; }
                        set { m_thing.ThingString = value; }
              }
              public void PrintThingString()
              {
                        m_thing.PrintThingString();
              }
    }
    Because the ThingWrapper has the same signature as the Thing, it is really no different dealing with the Thing or the ThingWrapper. We can work with either the Thing or the ThingWrapper access our internal string m_string, or perform the base functionality which is to print a line to the console.

    Code:
    Thing t = new Thing();
    t.ThingString = "This is the ThingString";
    t.PrintThingString();
     
    ThingWrapper tWrap = new ThingWrapper(t);
    tWrap.PrintThingString();
    tWrap.ThingString = "This is still the ThingString";
    tWrap.PrintThingString();
    We can wrap any class this way. Now that we understand this "wrapping power", we should only use it for good (or to do some cool stuff). Just remember, things can get pretty complicated if we have wrappers that are really not needed. Sometimes, however, we just can't live without them. Starting in the next article, let's next look at some of the super powers we can manifest using wrappers.
    Last edited by 2kaud; January 4th, 2019 at 07:26 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured