473,779 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this break encapsulation?

If I have a control that adds itself to its container, does that break
encapsulation? Is it a bad thing?

Here's what I mean:

public class fred
{
public barny b;
public fred()
{
b = new barny(this);
}
....
}

public class barny
{
public barny(fred f)
{
...
f.Controls.Add( this);
}
}

Cheers,
..pd.
Jul 21 '05 #1
2 2319
You have public members in Fred and barney....you could fix this by making
barney private and declaring a property that assigns a value to barny.
Remember that encapsulation doesn't deal with what a class does internally
per se, it deals with how other classes can access things inside your class.
And if outside classes can manipulate your members directly, without being
marshalled through a property, then you broke encapsulation.

Is it a bad thing? Generally speaking, yes and I would recommend avoiding
it. Once again, it mainly depends on how its being used----I know many use
Public variables and think properties are a waste of code - I'm not one of
them. I'd use properties and go from there...

HTH,

Bill
".pd." <sp*****@your.p eril> wrote in message
news:Xn******** *************** ******@194.83.1 79.102...
If I have a control that adds itself to its container, does that break
encapsulation? Is it a bad thing?

Here's what I mean:

public class fred
{
public barny b;
public fred()
{
b = new barny(this);
}
...
}

public class barny
{
public barny(fred f)
{
...
f.Controls.Add( this);
}
}

Cheers,
.pd.

Jul 21 '05 #2
William Ryan wrote on Tue 04 Nov 2003 05:38:33p:

Thanks for replying.
You have public members in Fred and barney...
Well, that's just an example. IRL, "b" is a protected member of the
instantiating form, so that only inheritors can manipulate it.
it deals with how other classes can access
things inside your class.
So having b's constructor manipulating f's controls is perfectly
valid/safe/acceptable?

The way I figure, and I'm open to correction, a control - say a Panel -
is always going to have a parent. If part of the Panel's setup is to
make decisions based on its parent's fields/properties then why make the
instantiator have to:

(a) call a separate method in the barny object that would have to check
that a parent has been assigned before it could do any processing
(b) remember not to call said setup routine until after it has added the
control to itself.

Compared to my original example, this looks very convoluted.

public class fred
{
public fred()
{
barny b = new barny();
this.Controls.A dd(b);
b.Setup();
}
}

public class barny
{
public barny()
{
// can't do any initialisation stuff based on parent
// here cuz we don't know who we belong to yet
}
public void Setup()
{
// but we still have to check even here
if (Parent == null)
throw new Exception("You haven't added the control *to*
anything yet!");
else
{
// finally we get to do the initialisation
}
}
}
And if outside classes can manipulate your
members directly, without being marshalled through a property, then
you broke encapsulation.
Okay - but Controls being a public member of the Control class isn't
something I can change. So can I assume M$'s decision to make it public
is an indication that anyone with a reference to the object may add
controls to it with impunity?
I'm not one of them.


Me either :-)

Thanks again for your response,

..pd.
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1796
by: K.K. | last post by:
Consider the following code: >>>>>>>>>>> // Define an empty class public class ZorgleCollection : Dictionary<string, Zorgle> { } // Somewhere outside ZorgleCollection:
9
7137
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I am trying to assign a Delegate to an Event using reflection. I use FieldInfo.GetValue to supposedly get the field object, but casting the result to the type of the field and assigning the Delegate does not catch the event. 1. Does GetValue really return the *actual* object held in the field ? 2. Is there an easier way to assign a delegate in a containing class to an
2
279
by: .pd. | last post by:
If I have a control that adds itself to its container, does that break encapsulation? Is it a bad thing? Here's what I mean: public class fred { public barny b; public fred() {
47
3370
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) {return
26
4422
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't treat that as a null token, it just treats the 2 colons as a single delimiter. Is that the intended behavior?
32
4229
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike files; but now and then I go on an opaque type joyride, and create minimalist header files that define very clean interfaces. The problem with that is that it prevents some optimizations:
52
20910
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
162
10300
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
1
1762
by: Pallav singh | last post by:
Hi i find Following statement while reading Design pattern in C++ ( author - Erich Gamma) "Inheritance break Encapsulation " because inheritance exposes to subclass the detail of its parent's Implementation Is it correct ??? as we only expose interface ( function prototype) to derived class But not how it being implemented
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10137
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.