c# question

  • Thread starter Thread starter Ten
  • Start date Start date
T

Ten

i have 5 non-static classes
Class A, B, C, D, and E

Class A creates an instance of class B. what would be a way for classes C D
E to pass data into that instance of class B.

Class A
{
B b = new B();
}

Tem
 
Ten said:
Class A creates an instance of class B. what would be a way for classes
C D E to pass data into that instance of class B.

You could either make B available via a property on A and call methods
on B directly - if it's OK to have direct access to the object in your
model - or you could add methods to A which manipulate B on behalf of C,
D and E (which is pretty much a façade pattern).

I'd personally go for the façade.
 
Hi Tem,

Since "b" is private inside "A" there's no way for outsiders to send
messages (method calls, property setters, ...) to "b". You can, of
course, use delegation from "A" to "B"; "a" receives a message and
sends a "similar" message to it's private instance of "b".

Other things are possible in certain scenario's; for instance when you
only need to pass certain data to "b" upon construction or
intialization. Is that the case?

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
 
You could either make B available via a property on A and call methods
on B directly - if it's OK to have direct access to the object in your
model - or you could add methods to A which manipulate B on behalf of C,
D and E (which is pretty much a façade pattern).

I'd personally go for the façade.


--
Chris

hikari.vcf
1KDownloaden


Personally, I wouldn't call it a "Façade" nor "Proxy" (having this
little information). "Façade", "Proxy" and "Adapter" are all
applications of "Wrapper" with a different intent. None of these
intents are hinted in the problem statement. The commonality between
the proposed solution, "Façade" and "Proxy" is the usage of
"delegation".

Of course, as a mental model "Façade" is fine...

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
 
i need to pass data to b not just on initialization.

do you know where i can find an exmaple of the first method you mentioned?
delegation

Thanks
Tem
 
i have 5 non-static classes
Class A, B, C, D, and E

Class A creates an instance of class B. what would be a way for classes C D
E to pass data into that instance of class B.

Class A
{
B b = new B();
}

Tem

Two of the ways to do this are:

1. Make a public readonly property of A that returns b. Then the
callers would say:

a.B.SomePropertyOfB = 5

Users of A can access any methods/properties of B.

2. Create properties/methods of A that then call the appropriate
properties and methods of B. In this case the properties/methods of A
may or may not be exactly the same as those of B. You would probably
only use this technique if you wanted to expose only a small subset of
B's interface to the users of A. You are hiding the existence of a B,
and in the future would be free to use a class other than B without
the callers having to change their code as long as it offered the same
functionality that you expose.
 
i need to pass data to b not just on initialization.

do you know where i can find an exmaple of the first method you mentioned?
delegation

Thanks
Tem

You can find an example of delegation here:
http://en.wikipedia.org/wiki/Delegation_(programming). Also usefull:
Explanation of composition and aggregation:
http://en.wikipedia.org/wiki/Aggregation_(object-oriented_programming)#Aggregation.
The examples are in C++, but with a little imagination it can be C#.

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
 
I think it's perfectly reasonable to describe briefly
solutions appropriate to different problem statements, given that the OP
has provided so little to work with.

I agree, but only when the solutions offered are placed into a correct
context. But, anyway, that's not what this post is about...

Kind regards,
Marc Vangrieken
http://vangrieken.wordpress.com
 
Back
Top