473,789 Members | 2,728 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 2320
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
3372
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
4424
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
20913
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
10303
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
9511
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
10200
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...
0
9986
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
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
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
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.