473,657 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Encapsulation idiom

Hi,

This is a design question. I have a class named "DataBuffer " that
stores some data. After "DataBuffer " is created it can not be modified.
All the methods of "DataBuffer " are const as data can not be modified
after it was created.

Up to here everything is fine. The problem is when I want to get clever
with data storage. My program has an array of "DataBuffer s" that gets
pre-allocated. If I want to use that memory instead of creating a new
"DataBuffer " each time, I get a pointer to the internal data with a
method and then I use that to fill the data directly. The problem with
this is that it breaks encapsulation as everybody has now access to the
internal data.

Alternatively, I can still use the constructor option, create a new
instance of "DataBuffer " on the stack (or previsouly allocated) with
the new data and then do a copy (operator=) to the data previously
allocated. This works and doesn't break encapsulation but is rather
inefficient as it uses two copies of the data.

I though on declaring the method of the class that stores the data a
"friend" of "DataBuffer ". Unfortunately, I use inheritance and a
virtual method so "friend" does not work.

Does anybody knows of a way to do this without braking encapsulation
and without paying an efficiency penalty?

Thanks

Feb 3 '06 #1
5 1670
TB
jm*******@gmail .com sade:
Hi,

This is a design question. I have a class named "DataBuffer " that
stores some data. After "DataBuffer " is created it can not be modified.
All the methods of "DataBuffer " are const as data can not be modified
after it was created.

Up to here everything is fine. The problem is when I want to get clever
with data storage. My program has an array of "DataBuffer s" that gets
pre-allocated. If I want to use that memory instead of creating a new
"DataBuffer " each time, I get a pointer to the internal data with a
method and then I use that to fill the data directly. The problem with
this is that it breaks encapsulation as everybody has now access to the
internal data.

Alternatively, I can still use the constructor option, create a new
instance of "DataBuffer " on the stack (or previsouly allocated) with
the new data and then do a copy (operator=) to the data previously
allocated. This works and doesn't break encapsulation but is rather
inefficient as it uses two copies of the data.

I though on declaring the method of the class that stores the data a
"friend" of "DataBuffer ". Unfortunately, I use inheritance and a
virtual method so "friend" does not work.

Does anybody knows of a way to do this without braking encapsulation
and without paying an efficiency penalty?

Thanks


Provide an iterator instead of a raw pointer.

--
TB @ SWEDEN
Feb 3 '06 #2
jm*******@gmail .com wrote:
This is a design question. I have a class named "DataBuffer " that
stores some data. After "DataBuffer " is created it can not be modified.
All the methods of "DataBuffer " are const as data can not be modified
after it was created.

Up to here everything is fine. The problem is when I want to get clever
with data storage. My program has an array of "DataBuffer s" that gets
pre-allocated. If I want to use that memory instead of creating a new
"DataBuffer " each time, I get a pointer to the internal data with a
method and then I use that to fill the data directly. The problem with
this is that it breaks encapsulation as everybody has now access to the
internal data.
Uh... Why do you need to "get a pointer to the internal data"? Can't you
simply use 'placement new' and _construct_ the object? Of course, you
have to remember to destroy the element once it's not needed any longer.

I would recommend to have a _factory_ for your objects that would do what
you describe. It will _own_ the array of elements and give out the stored
objects by pointer by creating another one in place. This is "pooling",
you have a "pool" of elements (or placeholders) and reuse them. Of course
the pool has to be inaccessible for anybody else but the factory.
[...]


V
Feb 3 '06 #3
* jm*******@gmail .com:

This is a design question. I have a class named "DataBuffer " that
stores some data. After "DataBuffer " is created it can not be modified.
All the methods of "DataBuffer " are const as data can not be modified
after it was created.
Good so far, you have logically const DataBuffer obejcts.

Up to here everything is fine. The problem is when I want to get clever
with data storage.
Ah, there's the rub, yes.

General advice is, _don't_ be "clever".

But since you probably have good reasons (unstated), I'll address that
question.

My program has an array of "DataBuffer s" that gets pre-allocated.
A pool.

If I want to use that memory instead of creating a new
"DataBuffer " each time, I get a pointer to the internal data with a
method and then I use that to fill the data directly. The problem with
this is that it breaks encapsulation as everybody has now access to the
internal data.
Yes, that's a problem.

It's simple.

Have a pool of storage instead of a pool of DataBuffer objects.

When constructing a DataBuffer object, obtain a pointer to storage from
the pool, and pass that to the DataBuffer constructor.

When destroying a DataBuffer object, return its data storage to the
pool.

Alternatively, I can still use the constructor option, create a new
instance of "DataBuffer " on the stack (or previsouly allocated) with
the new data and then do a copy (operator=) to the data previously
allocated. This works and doesn't break encapsulation
(1) It doesn't work because (2) it does break encapsulation (and it's
inconsistent with your earlier claim that all 'methods' are const).

If DataBuffer objects are meant to be logically const, remove access to
copy assignment.

but is rather
inefficient as it uses two copies of the data.
And yes, (3), it's inefficient in addition to being unsafe.

I though on declaring the method of the class that stores the data a
"friend" of "DataBuffer ". Unfortunately, I use inheritance and a
virtual method so "friend" does not work.
To me neither of these two statements are meaningful, sorry.

Does anybody knows of a way to do this without braking encapsulation
and without paying an efficiency penalty?


See above.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 3 '06 #4
Regarding TB's answer, I'll prefer not to have access at all so I
consider an iterator almost the same as direct memory access.

On Victor's answer, the problem that I have with "placement new" is
that "DataBuffer " is a storage class that also has containers. Actually
one of the members is a Qt's QVector. Basically I think I can't pre
allocate a memory pool and then use it as the storage for the array of
DataBuffers.

My other thought (on the factory line) is this. What if the interface
class (the base of the class that actually does the job of reading the
data and store it in a DataBuffer by a virtual method) has a non
inherited method (factory method) that is a "friend" of "DataBuffer "
and then calls the "virtual storage method" that used to have a
DataBuffer* as a parameter but now pass the internal pointer? I can do
that now that the factory is a "friend" method. I'm not sure if this is
too ugly but it doesn't seem to violate encapsulation and nobody
besides the factory method can change the data.

Feb 3 '06 #5
jm*******@gmail .com wrote:
Regarding TB's answer, I'll prefer not to have access at all so I
consider an iterator almost the same as direct memory access.

On Victor's answer, the problem that I have with "placement new" is
that "DataBuffer " is a storage class that also has containers.
Actually one of the members is a Qt's QVector. Basically I think I
can't pre allocate a memory pool and then use it as the storage for
the array of DataBuffers.
This is a common misconception about the size of object that
themselves have some dynamic memory allocation. I don't want to
get into it, twice a week is one time too many. If you can't
preallocate memory, don't.
My other thought (on the factory line) is this. What if the interface
class (the base of the class that actually does the job of reading the
data and store it in a DataBuffer by a virtual method) has a non
inherited method (factory method) that is a "friend" of "DataBuffer "
and then calls the "virtual storage method" that used to have a
DataBuffer* as a parameter but now pass the internal pointer?
So? Do you have a suspicion of something bad? If yes, state it.
If not, guessing game belongs to Vegas.
I can do
that now that the factory is a "friend" method. I'm not sure if this
is too ugly but it doesn't seem to violate encapsulation and nobody
besides the factory method can change the data.


If you can do it now, do it. If it does what you need, why don't
you use it already?

V
--
Please remove capital As from my address when replying by mail
Feb 4 '06 #6

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

Similar topics

2
2298
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() {
3
1789
by: K.K. | last post by:
Consider the following code: >>>>>>>>>>> // Define an empty class public class ZorgleCollection : Dictionary<string, Zorgle> { } // Somewhere outside ZorgleCollection:
3
41531
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
6
2167
by: William H. Burling | last post by:
I am not sure I understand encapsulation. I thought that one of the objectives in OOPs is to hide data structures from other objects so that they did not have to know how to unpack them. However, in some instances, it seems that passing a data structure greatly reduces the number of accesses one might have to make between objects. Ie: a simple database might present an "offering" (which is
13
1421
by: Steven D'Aprano | last post by:
I was playing around with simple memoization and came up with something like this: _cache = {} def func(x): global _cache if _cache.has_key(x): return _cache else: result = x+1 # or a time consuming calculation...
47
3333
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
32
4200
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:
16
2129
by: copx | last post by:
I have recently read "Everything you ever wanted to know about C types" by Peter Seebach (1), and learned about incomplete types. Now, I realise the value of encapsulation, but I wonder whether it is proper C style to use that much of it. By using incomplete types you end up with something like this: object_set_colour(object, RED); colour = object_get_colour(object); instead of:
2
7628
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing the implementation of class member
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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
8842
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
8740
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
8617
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...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.