Archive for friend class pattern

Getters and Setters: a people problem

Posted in Design Issues with tags , , , , , , , on December 10, 2008 by moffdub

Greetings ladies and gentleman, it is I, El MoffDo, here to escort you on this excursion into Programming Excellence for the next 1300 words on this, the EIP web-ring.

For no particular reason, I was perusing a Smalltalk tutorial and duly noted this:

Class Date defines an instance method named “year” that answers the value of the instance variable named “year” in the instance of Date to which the message “year” is sent.

So this was Smalltalk, the revered and noble language that really made me “get” what objects were all about, tacitly sanctioning a getter. Narrowing my Google search down, I was able to find an entire page dedicated to the topic.

Read more »

Friends, revisited

Posted in Design Issues with tags , , , , , , on November 12, 2008 by moffdub

One practical aspect of the idea of C++ friend classes / the Friend Class pattern that I have completely ignored is dependencies between classes and packages / assemblies. I’m only starting to think about it now because I’m being forced at work to carefully consider package design and allocation.

Assuming the standard layered architecture, it is generally a good idea to have dependencies flowing downwards only. This means the UI layer could depend on the Application, Domain, or Infrastructure layers (though ideally it depends only on the Application), but the Application shouldn’t depend on the UI, nor should the Domain depend on either the UI or the Application, etc.

This notion appears to be completely incompatible with both the Friend Class pattern / C++ friend classes and general object-orientation via Tell Don’t Ask. Take these examples where Equipment is a domain object and EquipmentSummaryTO and its factory are in the Application layer:

Read more »

Friends in an unfriendly world

Posted in Java with tags , , , , , , on September 17, 2008 by moffdub

This post is about a pattern to simulate friend classes in languages that do not support them, like Java and C++.

Basic layered architecture, domain-driven design idioms like factories and repositories, and even some of the Gang of Four‘s patterns, like the Memento Pattern, are well-suited for C++’s friend class language feature:

class X
{
private:
int a, b;
friend class F;
public:
X() : a(1), b(2) { }
};

class F
{
public:
void print(X& x)
{
cout < < "a is " << x.a << endl;
cout << "b is " << x.b << endl;
}
};

I have many times lamented the fact that Java and C# don’t have friend classes, but instead have package access and internal access, respectively.

Read more »

Follow

Get every new post delivered to your Inbox.