Archived

This topic is now archived and is closed to further replies.

AnotherNewbie

newbie smalltalk encapsulation

Recommended Posts

Hi I''m a newbie to Smalltalk and have been trying to understand encapsulation. I understand the advantages but can''t get how making an object reference another object: obj1:=obj2 Is different to using setter/getter, accessor messages: obj1:obj2 Have been scratching my head for days on this, can anyone help me with this? Any help with this would be really appreciated. AnotherNewbie

Share this post


Link to post
Share on other sites
I''m not sure if this is what you want but if you want another object to "reference" the other object you could do this:


cObj2 : public cObj1
{
// class definition

};


or if you want a pointer-to-object to reference another object you could do this:


cObj1 myObj1;
cObj1* pto = &myObj1;

Share this post


Link to post
Share on other sites
...

...

... I take it the word "Smalltalk" means nothing to you? Hint: It''s a programming language, very different from C++.

Share this post


Link to post
Share on other sites
Thanks for the reply DIRECTXMEN

The problem I''m having is understanding why using accessors is any different to referencing.

In the methods:

self colour: red
The reciver object sets itself

Whereas:

colour:= red
self updateUserInterfaces
The receiver objects makes colour reference red then updates itself

Can''t understand how this works, is this any clearer?

AnotherNewbie

Share this post


Link to post
Share on other sites
Encapsulation isn''t tied to Smalltalk or any other languages in fact. It''s simply a OO concept.

By using an accessor you can add code within the method, to perform validation for example. Let''s say you have a foregroundColor and a backgroundColor. Maybe you would like to validate that they are always different. Otherwise the user interface has the potential to be unusable (black on black isn''t what I would call readable!). You could perform theses validations in the setter method.

self foregroundColor: newColor
(newColor ~= backgroundColor)
ifTrue: [
foregroundColour := newColor
]
ifFalse: [
do whatever is needed...
].

^foregroundColor

If you used a simple assignment, you can''t perform such validation. In fact you could do it right after you assigned a new colour. Though if you change your colour at multiple places in your code, you will have to duplicate theses validation everytime you assign a new colour. Which is not cool, because you will have multiple places in the code to update if you ever change your validation rules. Whereas if you used a setter method with validation, you only change one place and every client of this method will be subject to the same rules. Which probably make more sense.

[OT] I miss programming in Smalltalk this is probably the best language I have used in my life. I liked so much the idea of code block that you can pass as parameters. I worked in an environment where we used object oriented database (Gemstone), the persistence was so much easier than doing the OO/Relational mapping. Sigh..

-nosfy
Lead programmer
Creation Objet Inc.

Share this post


Link to post
Share on other sites
Thanks for the reply, nosfy

This makes more sense now, so accessors allow code to be added and assigment only gives an answer?.

Thanks for the help, I''m starting out on a computing Bsc and have been sailing through but this got me stumped. Not used to an oo approach, started off programming with a C-64!.

Thanks again

AnotherNewbie

Share this post


Link to post
Share on other sites
Yup you seem to have understood what encapsulation is about. In fact, the responsibility of changing data shoud always belong to the object that possesses the member.

It''s more easy to change your code if it''s encapsulated than if it is not. Especially if it is business software. In games I can understand that there is a huge performance issue, but accessors/gettors are rarely the bottleneck. Only after you investigated and are really certain that the methods are the bottleneck you should think to not use them. And in C++ you may try to inline the code before removing gets/sets metthods. Usually performance problem are the result of not using the best algorithm for a given task. Before removing the accessors, a lot more can be done to improve performance...

Okay I will stop there, I think you understand my point! ;-)

-nosfy
Lead programmer
Creation Objet Inc.

Share this post


Link to post
Share on other sites
Thanks again nosfy

I think the penny has dropped now, your explanation was the first one that actually made some sense.
Like I say I''m just starting out and haven''t come across performance issues yet, am hoping that Smalltalk will help me understand c++ better. Have done some c++ but found it quite confusing. My goal is to be able to write some games eventually, thanks for helping on my way.

AnotherNewbie

Share this post


Link to post
Share on other sites