473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance vs containment for policies

Hello all,

The methodology of policy-based design states that one should inherit from
policy classes and, of course, these classes must provide an agreed-upon
public interface and semantics.

I would like to understand better why inheritance, rather than containment,
is espoused. The only things I can see that inheritance provides beyond
containment is the possibility of polymorphism and access to protected
members. Since neither of these are needed when trying to gain access to
the functionality of a policy class, why is inheritance espoused over
containment?

Thanks,
Dave
Jul 22 '05 #1
1 3921
"Dave" <be***********@ yahoo.com> wrote
Hello all,

The methodology of policy-based design states that one should inherit from
policy classes and, of course, these classes must provide an agreed-upon
public interface and semantics.

I would like to understand better why inheritance, rather than containment,
is espoused. The only things I can see that inheritance provides beyond
containment is the possibility of polymorphism and access to protected
members. Since neither of these are needed when trying to gain access to
the functionality of a policy class, why is inheritance espoused over
containment?


There are a few minor reasons. The first is space efficiency. Consider the
following examples:

template <typename T>
class ByInheritance : public Policy1<T>, public Policy2<T>
{
// ...
}

and

template <typename T>
class ByComposition
{
Policy1<T> p1;
Policy2<T> p2;
// ...
}

If 'Policy1' and 'Policy2' contain only code, they're basically empty and the
compiler may (and probably will) fold them into 'ByInheritance' such that they
will take no additional space. If 'ByInheritance' had no data members of its
own, its size could conceivably be as small as 1 byte.

By contrast, the class 'ByComposition' , because it contains explicit instances
of 'Policy1' and 'Policy2', will be bigger by no less than 2 bytes. This is
because the instances of 'Policy1' and 'Policy2' must be given unique
identities and therefore unique memory addresses.

A second reason for using inheritance over composition is to simplify the code.
If you want to expose a function 'xyz()' that is defined by one of the
policies, inheritance does so implicitly. Using composition, you would have to
forward the call to the underlying object.

Those are the two most basic reasons.

When you use containment -- which is not the same thing as composition --
you're moving into the realm of aspect-oriented programming in which the
policies can be changed at runtime. It's more versatile, but also much less
efficient.

Claudio Puviani
Jul 22 '05 #2

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

Similar topics

4
1638
by: Peter Hamilton | last post by:
I am trying to implement inheritance but I am having a difficult time with some concepts. What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change. Maybe what I am trying to do is not "inheritance" after...
7
1622
by: Mahesh | last post by:
Hello, I am having a query about inheritance. I have a virtual base class that is derived by to other classes that are hirarchially at the same level. But I still need to ensure that only a single instance of the base class is created. Is this even possible? If yes then how do I go about doing this? Thanks, Mahesh
20
23075
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
14
12897
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
5
2379
by: michael | last post by:
how to make a class inherit from more that one other class or namespace ex: public class MyClass { //some code here } public class MyForm : System.Windows.Forms.Form {
22
23342
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
6
5193
by: Donal McWeeney | last post by:
I was under the impression that in an interface declaration I could inherit another interface declaration and the result would be that the inheriting interface could include the methods and properties defined in the inherited interface. However when playing with this and defining a class to implement the interface the class gets implemented with both sets of interface declarations. eg.
2
1239
by: JAL | last post by:
Comments appreciated. I coded an approach to simulating multiple inheritance of implementation in C# using PIMPL and a type based static class factory that seems to combine the extensibility of inheritance with the encapsulation of containment. Hopefully, the C++/cli coders here can read this C# code. http://www.geocities.com/jeff_louie/oop27.htm
60
4900
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
0
8091
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
8555
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
8232
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
8408
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
7024
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...
1
6064
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
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
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
1
1686
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.