由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c# decorator pattern question
相关主题
还请教一个关于C++的问题怎么才能避免每次都写using namespace std (转载)
两个继承问题c++ class definition
static 变量放在哪里?C++namespace defined in another file
请教一个static 函数的问题private destructor
球python大牛给指导一下:写python最大的难点是什么?最重要的品质是什么?inline function是否可以递归?
最初级的白痴C++问题Java题求指导 (转载)
g++-2.95 -> g++-3.3/3.4some interview questions
question about Design Patternsfunction declaration
相关话题的讨论汇总
话题: decorator话题: using
进入Programming版参与讨论
1 (共1页)
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
9
这个用c++写也是一样的行为吧?
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
1 (共1页)
进入Programming版参与讨论
相关主题
function declaration球python大牛给指导一下:写python最大的难点是什么?最重要的品质是什么?
C array最初级的白痴C++问题
A C++ compiler related interview questiong++-2.95 -> g++-3.3/3.4
*** help needed! on MATLAB GUI ***question about Design Patterns
还请教一个关于C++的问题怎么才能避免每次都写using namespace std (转载)
两个继承问题c++ class definition
static 变量放在哪里?C++namespace defined in another file
请教一个static 函数的问题private destructor
相关话题的讨论汇总
话题: decorator话题: using