|
|
|
|
|
|
Implementing a class factory in a managed C++ DLL
In this tutorial I will show you how to expose your class factory in a quick and easy manner when writing a managed C++ library to be exposed to .NET
Class factories are very useful for a variety of purposes, however if you choose to expose all of the implementation classes for your interfaces as managed classes, this can be very time consuming. Instead we implement an interface handler class to be managed.
The code for the managed C++ portion is shown below
#include "stdafx.h"
#include "stdlib.h"
using namespace System;
class IShape
{
protected:
IShape(){}
public:
virtual ~IShape(){}
virtual double GetArea() = 0;
};
class Circle : public IShape
{
public:
double GetArea()
{
return 1.0;
}
};
class Square : public IShape
{
public:
double GetArea()
{
return 2.0;
}
};
class Parallelogram : public IShape
{
public:
double GetArea()
{
return 3.0;
}
};
namespace ManagedCPPTest {
__abstract __gc public class ManagedShape
{
public:
virtual double GetArea() = 0;
};
__sealed __gc public class ShapeInterfaceHandler : public ManagedShape
{
private:
IShape* pBackObj;
public:
ShapeInterfaceHandler( IShape* pShape ) : pBackObj( pShape ) {}
~ShapeInterfaceHandler()
{
if( pBackObj ) {
delete pBackObj;
pBackObj = 0;
}
}
double GetArea()
{
if( pBackObj ) {
return pBackObj->GetArea();
}
return 0;
}
};
__gc public class ShapeFactory
{
public:
static ShapeInterfaceHandler* CreateCircle()
{
return new ShapeInterfaceHandler( new Circle() );
}
static ShapeInterfaceHandler* CreateSquare()
{
return new ShapeInterfaceHandler( new Square() );
}
static ShapeInterfaceHandler* CreateParallelogram()
{
return new ShapeInterfaceHandler( new Parallelogram() );
}
static void CreateCircle( ManagedShape** ppi )
{
if( ppi ) {
*ppi = new ShapeInterfaceHandler( new Circle() );
}
}
static void CreateSquare( ManagedShape** ppi )
{
if( ppi ) {
*ppi = new ShapeInterfaceHandler( new Square() );
}
}
static void CreateParallelogram( ManagedShape** ppi )
{
if( ppi ) {
*ppi = new ShapeInterfaceHandler( new Parallelogram() );
}
}
};
};
Notes:
- Note we expose a managed abstract interface, which is then implemented using a managed implementation
- The "__abstract" keyword tells the compiler the class is an abstract interface, the "__sealed" keyword tells the compiler this is a finalized class and that there will no derived implementations of the class
- Also note we have exposed two types of creation functions for the factory. The first returns the created object. The second takes a pointer to the value that is to receive the object.
The C# portion is shown below
private void Form1_Load(object sender, System.EventArgs e)
{
ManagedCPPTest.ManagedShape shape1 = ManagedCPPTest.ShapeFactory.CreateCircle();
ManagedCPPTest.ManagedShape shape2 = ManagedCPPTest.ShapeFactory.CreateSquare();
ManagedCPPTest.ManagedShape shape3 = ManagedCPPTest.ShapeFactory.CreateParallelogram();
ManagedCPPTest.ManagedShape shape4 = null;
ManagedCPPTest.ShapeFactory.CreateCircle( ref shape4 );
ManagedCPPTest.ManagedShape shape5 = null;
ManagedCPPTest.ShapeFactory.CreateSquare( ref shape5 );
ManagedCPPTest.ManagedShape shape6 = null;
ManagedCPPTest.ShapeFactory.CreateParallelogram( ref shape6 );
}
Notes:
- Note we use both types of object creation.
- This methodology can be extended to implement the Abstract Factory Pattern as well, since the exposed interface has no idea what the underlying implementation is
Return to tutorial index
|
|
|
|
|
|