0

I am unaware of C# concept which is defined below.

I have following code below but i am unaware of what exactly is the use of last new DecoratorB( new DecoratorA(component)) in the Main function.

1) Can you please explain me what exactly the above code is doing and how references are set for a given object.

2) Also Display(string s, IComponent c) accepts IComponent as parameter so what exactly does the DecoratorA and DecoratorB class returns.

EDIT what exactly those nested constructor calls are doing in this example.

I tried debugging the code but no clue what exactly is happening.

 static void Display(string s, IComponent c) {
     Console.WriteLine(s+ c.Operation( ));
 }

 static void Main(){
     IComponent component = new Component( );
     Display("1. Basic component: ", component);
     Display("2. A-decorated : ", new DecoratorA(component));
     Display("3. B-decorated : ", new DecoratorB(component));
     Display("4. B-A-decorated : ", new DecoratorB(
     new DecoratorA(component)));
 }


interface IComponent {
 string Operation( );
 }

 class Component : IComponent {
 public string Operation ( ) {
 return "I am walking ";
 }
 }

 class DecoratorA : IComponent {
 IComponent component;

 public DecoratorA (IComponent c) {
 component = c;
 }

 public string Operation( ) {
 string s = component.Operation( );
 s += "and listening to Classic FM ";
 return s;
 }
 }

 class DecoratorB : IComponent {
 IComponent component;
 public string addedState = "past the Coffee Shop ";

 public DecoratorB (IComponent c) {
 component = c;
 }

 public string Operation ( ) {
 string s = component.Operation ( );
 s += "to school ";
 return s;
 }

 public string AddedBehavior( ) {
 return "and I bought a cappuccino ";
 }
 }

Also if i am missing a basic then please guide me to the required link

Thank you

EDIT 2

I want to know what kind of robustness is provided accessing the function using interface object. link here

//System.Console.WriteLine("Length: {0}", box1.getLength());
//System.Console.WriteLine("Width: {0}", box1.getWidth());

System.Console.WriteLine("Length: {0}", dimensions.getLength());
System.Console.WriteLine("Width: {0}", dimensions.getWidth());



static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);

        // Declare an interface instance dimensions:
        IDimensions dimensions = (IDimensions)box1;

        // The following commented lines would produce compilation 
        // errors because they try to access an explicitly implemented
        // interface member from a class instance:                   
        //System.Console.WriteLine("Length: {0}", box1.getLength());
        //System.Console.WriteLine("Width: {0}", box1.getWidth());

        // Print out the dimensions of the box by calling the methods 
        // from an instance of the interface:
        System.Console.WriteLine("Length: {0}", dimensions.getLength());
        System.Console.WriteLine("Width: {0}", dimensions.getWidth());
    }
  • Give me answers please or guide me i am pretty new at this. – Ashwani Kumar Apr 22 '16 at 11:27
  • Are you asking what this means in the context of the relevant classes, or are you confused about constructors in general? – harold Apr 22 '16 at 11:27
  • Read about constructor and constructor parameters & decorators. – L-Four Apr 22 '16 at 11:27
  • 1
    It's the decorator pattern. My duplicate link explains it. If it's constructors that you are wondering about, a simple google search will aid you. Google for instance "c# constructors" – jgauffin Apr 22 '16 at 11:29
  • I am not sure how the constructors are creating the chaining and how they are actually keeping the references, it is pretty confusing. Also what is returned by them here. – Ashwani Kumar Apr 22 '16 at 11:29
  • Yes it is decorator pattern but what exactly those nested constructor calls are doing in this example. – Ashwani Kumar Apr 22 '16 at 11:30
  • why don't you just debug the code and see what happens? – jgauffin Apr 22 '16 at 11:30
  • debugging the code is so confusing, i already tried but then as i said i am unable to understand the nested constructor calls. – Ashwani Kumar Apr 22 '16 at 11:31
  • Nested constructors do the same thing as non-nested constructors, there's nothing special about them – harold Apr 22 '16 at 11:33
  • that is a random answer not properly stated or please explain it first. – Ashwani Kumar Apr 22 '16 at 11:35
  • 1
    There is nothing to explain. If you know what a constructor does, then you also know what happens when you construct an object and pass that as argument to some constructor. It's not a special case. – harold Apr 22 '16 at 11:40
  • hmm so what does the constructor is returning here? – Ashwani Kumar Apr 22 '16 at 11:42
  • 1
    Constructors return the object they construct. – harold Apr 22 '16 at 11:45
  • Exactly so how they are passed to `Display(string s, IComponent c)` function when it accepts `IComponent` type object and a `string`. Sorry i do not mean to confuse you though but what i mean is if it return `DecoratorA` type object then how it is mapping to the `IComponent` parameter in `Display` function – Ashwani Kumar Apr 22 '16 at 11:46
  • The same way as in the lines above, they implement that interface – harold Apr 22 '16 at 11:50
  • if you could please explain what you mean and how does that matter(implementing the interface) then it would provide me answer for my doubt, or any link would be helpful too. – Ashwani Kumar Apr 22 '16 at 11:55
  • You can learn about interfaces [here](https://msdn.microsoft.com/en-us/library/ms173156.aspx) – harold Apr 22 '16 at 11:59
  • Harold i read that all but can you please tell me or provide me a link how three different `Component, DecoratorA, DecoratorB` classes are equal if they implement same Interface? – Ashwani Kumar Apr 25 '16 at 05:29
  • What i mean is how it is possible that _**Decorator inherits from IComponent means that Decorator objects can be used wherever IComponent objects are expected.**_ – Ashwani Kumar Apr 25 '16 at 05:38

0 Answers0