Mixing Functionality

[Previous] [Next]

Once all this groundwork is in place, it becomes easy to create different kinds of bands. In this example, the difference between bands is really just who plays the instruments. We can create different kinds of rock bands by mixing in different musicians. The code below shows how four bands distinguish themselves by the musicians who participate.

 void ListenToRockBands() {     CRockBand<CGuitarist, CBassist,                CDrummer, CVocalist> NoName;     CRockBand<CAlexLifeson, CGeddyLee,                CNeilPeart, CGeddyLee> Rush;     CRockBand<CEddieVanHalen, CMichaelAnthony,                CAlexVanHalen, CGaryCherone> VanHalen;     CRockBand<CEddieVanHalen, CGeddyLee,                CNeilPeart, CGeddyLee> WhatsThis;     MessageBox(NULL,                 "A non-name band is opening",                 NULL, MB_OK);     NoName.PlayMusic();     MessageBox(NULL,                 "Rush is on second",                 NULL, MB_OK);     Rush.PlayMusic();     MessageBox(NULL,                 "Van Halen is on third",                 NULL, MB_OK);     VanHalen.PlayMusic();     MessageBox(NULL,                 "What's This is on fourth",                 NULL, MB_OK);     WhatsThis.PlayMusic(); } 

Remember that the original goal of this exercise was to let the client form the rock band. Here's where the templates come in. When the client wants to form a band, the client creates an instance of the CRockBand class based on a particular set of musicians, as shown in the preceding code.

After looking at this code for a bit, you should realize just what a cool, flexible tool templates are. If you chose to, you could implement a whole host of musicians to use as template parameters for the rock band. Templates add the ability to mix types. But what does all this have to do with COM and ATL?

Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net