473,890 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strong Typed generic Composite class

Hello everyone and especially Kai-Uwe Bux
A month ago I post a question in the same name of this one but I didnt
managed to explain my question right, so people didn't understand me.
Now when I have the time to post it again I hope I could manage to
explain my self + to post the answer to my previous question.

So...
When you look at the Composite GOF pattern it's basically

//Snip
class Interface
{
virtual void work() = 0;
}

class compositor : public Interface
{
void work()
{ //Iterate through all the instances that the
//Class holds and call their work function
}
void addItem(Interfa ce* newItem)
{ //you get the point }

vector<Interfac e*m_Items
};
class workerA : public Interface {}
class workerB : public Interface {}
//Snip

If you want to add a new different class that inherits from a new
different Interface you will need to Copy Paste a new compositor.
If you want to Generalize the compositor you are heading to the
problem of how to keep it strong typed.
You can always use a master Object that all object are descendants of
him but you
will loose the strong type characteristic.
You can create a all purpose compositor but than you loose the
polymorphism of the classes
(If your interface "working" function is go() and your compositor
"working" function is Iterate() you cannot use a pointer to the
interface to handle a compositor (thus loosing the point))
OK so I patched up a Strong Typed Generic Compositor I added the code
at the end
(BTW works on VS2008 Express but not on VS6)

My new problem is how to further generalize the function call,
I have two solutions
1) To add a template for each possible function
A template for a function that returns void and 1 arg
A template for a function that -""- -""- and 2 args
and so one .. I've seen it many time but I dislike this solution
2) To add another level of abstraction and to patch up
classes for retVals and argVals (a bit of meta programming)

What do you think is better

//snip
#include <iostream>
#include <vector>

using namespace std;

#define CALL_MEMBER_FN( object,ptrToMem ber) ((object).*(ptr ToMember))
class IWorker
{
public:
virtual void work() = 0;
};

class ILaborer
{
public:
virtual void labor() = 0;
};

template <class T,void (T::*funcPtr)() >
class ABCCompositor
{
public:
typedef vector<T*tItemV ec;

void addItem(T* newItem)
{
m_ItemCol.push_ back(newItem);
}

void IterateThrough( )
{
for (tItemVec::iter ator iDx = m_ItemCol.begin ();
iDx < m_ItemCol.end() ; iDx++)
{
CALL_MEMBER_FN( *(*iDx),funcPtr )();
}
}

protected:
tItemVec m_ItemCol;
};

class WorkerComposito r : public IWorker,
public ABCCompositor<I Worker,&IWorker ::work>
{
public:

void work()
{
IterateThrough( );
}
};

class LaborerComposit or : public ILaborer,
public ABCCompositor<I Laborer,&ILabor er::labor>
{
public:

void labor()
{
IterateThrough( );
}
};

class CPrinter : public IWorker
{
public:
void work()
{
cout << "Hello" << endl;
}
};
class CPrinter2 : public IWorker
{
public:
void work()
{
cout << " OLA!!!" << endl;
}
};

class CPrinter3 : public ILaborer
{
public:
void labor()
{
cout << "Shalom" << endl;
}
};
class CPrinter4 : public ILaborer
{
public:
void labor()
{
cout << "Salam" << endl;
}
};

int main()
{

WorkerComposito r printSession1;
WorkerComposito r printSession2;

printSession1.a ddItem(new CPrinter);
printSession1.a ddItem(new CPrinter2);
printSession1.a ddItem(new CPrinter);
printSession1.a ddItem(new CPrinter2);
printSession1.a ddItem(new CPrinter);
printSession2.a ddItem(&printSe ssion1);
printSession2.a ddItem(new CPrinter2);

printSession2.w ork();
LaborerComposit or printSession3;
LaborerComposit or printSession4;

printSession3.a ddItem(new CPrinter3);
printSession3.a ddItem(new CPrinter4);
printSession3.a ddItem(new CPrinter3);
printSession3.a ddItem(new CPrinter4);
printSession3.a ddItem(new CPrinter3);
printSession4.a ddItem(&printSe ssion3);
printSession4.a ddItem(new CPrinter3);

printSession4.l abor();
return 0;
}

//snip

Thanks
Jul 17 '08 #1
0 1213

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

Similar topics

94
4776
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++. So, are there any examples (without pointers, references, or adress-taking), which would have a different result in Python and in C++? I would appreciate all insights or pointers to literature. TIA,
2
2961
by: j_mckitrick | last post by:
I recently took a one week course on .NET, and they emphasized over and over again that the key is types. Everything is strongly typed and enforced. Python is the exact opposite. Yet both claim they improve productivity and efficiency. I prefer Python, but is .NET likely to change this view of types? jonathon
2
325
by: Freeon | last post by:
Hi, I am looking for a way to sort a strong typed dataset. It would seem the most straightforward way is to use a dataview. The only problem is when I use the dataview I seem to loose the strong typed properties from my original dataset Anyone that can point me to an example of how to sort my dataset and maintain the use of my typed properties would be greatly appreciated Thank, Freeon
11
5469
by: Vincent Finn | last post by:
Hi, Is there any way of having a strongly typed dynamic array in C#? ArrayList requires you to cast every time you want to access an element object isn't dynamic I have resorted writing a hacky Add() function for object (below)
0
1129
by: Aquila Deus | last post by:
Hi all! I found an interesting way to implement strong-typed object attactment with generic: // Attachment should be a Dictionary<K,V> or some user-defined container, rather // than primitive types such as string, int or DateTime. class AttachableObject<AttachmentType> where AttachmentType: new() {
10
3095
by: farseer | last post by:
I'd like my collections to contain specific types of objects. Most of the topics i read on this involves creating a custom collection class that extends DictionaryBase or IEnumerable. But this means for every object type i care about, i must create a custom collection class for. Isn't there a simpler way to say "i want this collection to contain only objects of Class X"? thanks
3
2451
by: Chris Thunell | last post by:
I have 2 forms, on 1 form i use the wizards to create a strong typed dataset with tables from an SQL database... and from that i can do stuff like: me.daEmployee.fill(me.dataset11.tblEmployee) plus some 3rd party controls can see it the datatables etc etc. Is there anyway when i open the second form that this information that has already been loaded into my in-memory dataset be available on all my other forms... and still be strong...
0
971
by: pamela fluente | last post by:
Wrapping the old classic collections was straightforward. But with System.Collections.Generic we have that particular constructor (Of SomeType...) and I am not clear how I can wrap these typed collections. I want to wrap a System.Collections.Generic.Dictionary. The wrapping class should have a constructor similar to that of the dictionary (besides some possible other argument). Intuitively I want something "like":
2
1318
by: ManicQin | last post by:
Hi, I tried to code a -Strong typed generic composite class ... i want to generalize the <function calland the <interfacethat the compositor will implement I tried writing it in many variations but none was suffice, or working. Any idea what should I look for? Maybe a pseudo code will help explain:
0
9825
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
11234
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
10828
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
10467
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
8018
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
7171
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
5854
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...
0
6052
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
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.