Book a Demo

Author Topic: Modeling friend classes  (Read 11981 times)

EricP

  • EA User
  • **
  • Posts: 122
  • Karma: +0/-0
    • View Profile
Modeling friend classes
« on: November 08, 2009, 04:58:38 am »
A question about modeling friend classes...

Consider the following:

class A
{
public:
  friend class B;
private:
  int x;
  ...
};

Class B
{
public:
  int GetX() { return x; }
};

Class A has declared class B to be a friend, so that GetX can now directly access (and return) the value of Class A attribute x.

EA sort of supports modeling this by using the Dependency association (dotted line with arrowhead) with the stereotype <<friend>> (even though EA doesn't generate any code for it, which is the topic of another discussion on the "Bugs and Issues" forum...).

My question is, which way does the arrowhead go... towards the diagram for Class B (the friend), or towards the diagram for Class A (which declared B as a friend)?

Thanks...

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: Modeling friend classes
« Reply #1 on: November 08, 2009, 10:07:50 pm »
Hi,

Not sure, but I would say the dependency direction should be from B to A. Syntactically A is dependent on B (must have a forward declaration there). But IMHO what should be expressed is the semantical dependency that B needs to be a friend of A, to work as intended.

BTW: The coding rules in our company forbid using 'friend' for a number of good reasons. And as I've learned recently on this forum, it's even obsolete in UML 2.x superstructure definition. I think using friend dependencies doesn't lead to proper OO design, there are better ways, to prevent accessing interfaces of a class from the public, but still give access to these for selected classes. E.g. specify a reference to B as parameter for the needed operations, where instances of B can't be constructed or obtained otherwise from the public.

HTH
Günther
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

EricP

  • EA User
  • **
  • Posts: 122
  • Karma: +0/-0
    • View Profile
Re: Modeling friend classes
« Reply #2 on: November 08, 2009, 11:50:16 pm »
Quote
Not sure, but I would say the dependency direction should be from B to A. Syntactically A is dependent on B (must have a forward declaration there). But IMHO what should be expressed is the semantical dependency that B needs to be a friend of A, to work as intended.

Good morning, Gunter.

Ah, but who is dependent on whom?  Your answer seems to indicate both (unless I misunderstand your answer).

Does A depend on B being its friend, or does B depend on A allowing B to be its friend?

In order to work correctly, B needs access to A's private parts.  On the other hand, A can work correctly whether B has access or not.  So, I'd say B is dependent on A.  Make sense?

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: Modeling friend classes
« Reply #3 on: November 09, 2009, 02:15:19 am »
Quote
On the other hand, A can work correctly whether B has access or not.
A should be able to work correctly without any need to have friends ;).

Quote
So, I'd say B is dependent on A.  Make sense?
Yes! Concentrate on the semantical dependencies, rather than the language specifics in your UML model.

But read my other notes about the topic on your second question. Don't use friend, you ain't gotta need it. I have used friend in some designs. But it's really really rare, and I still think about what would be the best way to remove it from these.

WBR
Günther
« Last Edit: November 09, 2009, 02:39:40 am by g.makulik »
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/