s*****w 发帖数: 1527 | 1 this is the compiled code, in the end i got "get 0", instead of "get
30".
why ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test2
{
class MainApp
{
static void Main()
{
ConcreteComponent c = new ConcreteComponent(5);
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB();
d1.SetComponent(c);
|
c**t 发帖数: 2744 | 2 ConcreateDecoratorB has its own localComponent with initial val: 0.
That's why you got 0.
this is the compiled code, in the end i got "get 0", instead of "get
30".
why ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test2
{
class MainApp
{
static void Main()
{
ConcreteComponent c = new ConcreteComponent(5);
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
【在 s*****w 的大作中提到】 : this is the compiled code, in the end i got "get 0", instead of "get : 30". : why ? : using System; : using System.Collections.Generic; : using System.Linq; : using System.Text; : namespace test2 : { : class MainApp
|
c**t 发帖数: 2744 | 3 in your SetComponent
{
this.localComponent = component;
if( component is ConcreateDecorationA)
localComponent.var = ((ConcreateDecoratorA)component).localComponent.
var;
}
if you set the var, you'll get 15.
【在 c**t 的大作中提到】 : ConcreateDecoratorB has its own localComponent with initial val: 0. : That's why you got 0. : : this is the compiled code, in the end i got "get 0", instead of "get : 30". : why ? : using System; : using System.Collections.Generic; : using System.Linq; : using System.Text;
|
s*****w 发帖数: 1527 | 4 Hi cogt,
thanks for reply 1st.
your solution is kind of against decorator pattern.
if i have DecoratorA/B/C/D/E/F/..., the code will be very complicated.
basically i'm looking for the correct/efficient way to do decorator in
c#.
((ConcreateDecoratorA)component).localComponent.
【在 c**t 的大作中提到】 : in your SetComponent : { : this.localComponent = component; : if( component is ConcreateDecorationA) : localComponent.var = ((ConcreateDecoratorA)component).localComponent. : var; : } : if you set the var, you'll get 15.
|
X****r 发帖数: 3557 | 5 Make val private, add get/set accessors for val, and override them
in Decorator class to read/write the val of its localComponent.
【在 s*****w 的大作中提到】 : Hi cogt, : thanks for reply 1st. : your solution is kind of against decorator pattern. : if i have DecoratorA/B/C/D/E/F/..., the code will be very complicated. : basically i'm looking for the correct/efficient way to do decorator in : c#. : : ((ConcreateDecoratorA)component).localComponent.
|
c**t 发帖数: 2744 | 6 solution:
class ConcreteDecoratorA : Decorator
{
public override void Operation()
{
base.Operation();
Console.WriteLine("ConcreteDecoratorA.Operation()");
localComponent.var *= 2;
var = localComponent.var;
localComponent.get();
}
}
class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
AddedBehavior();
Console
【在 s*****w 的大作中提到】 : Hi cogt, : thanks for reply 1st. : your solution is kind of against decorator pattern. : if i have DecoratorA/B/C/D/E/F/..., the code will be very complicated. : basically i'm looking for the correct/efficient way to do decorator in : c#. : : ((ConcreateDecoratorA)component).localComponent.
|
s*****w 发帖数: 1527 | 7 it works, thanks a lot !
now i need to understand why this is simple in c++, but so complicated in
c#.
【在 c**t 的大作中提到】 : solution: : class ConcreteDecoratorA : Decorator : { : public override void Operation() : { : base.Operation(); : Console.WriteLine("ConcreteDecoratorA.Operation()"); : localComponent.var *= 2; : var = localComponent.var; : localComponent.get();
|
s*****w 发帖数: 1527 | 8 cogt,
i still have trouble understanding this,
in c++ why it's much simpler ?
do you have easier ways to implement decorator ?
【在 c**t 的大作中提到】 : solution: : class ConcreteDecoratorA : Decorator : { : public override void Operation() : { : base.Operation(); : Console.WriteLine("ConcreteDecoratorA.Operation()"); : localComponent.var *= 2; : var = localComponent.var; : localComponent.get();
|
n*w 发帖数: 3393 | |
n*w 发帖数: 3393 | 10 这个应该是最佳解决。
【在 X****r 的大作中提到】 : Make val private, add get/set accessors for val, and override them : in Decorator class to read/write the val of its localComponent.
|
s*****w 发帖数: 1527 | 11 think i get it work like c++, finially |