// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
//#include "Factory.h"
using namespace std;
class Product
{
public:
virtual void display() = 0;
};
class ProductA:public Product
{
void display();
};
class ProductB:public Product
{
void display();
};
class Factory
{
public:
Product* CreatProduct(const char* strType)
{
if(strcmp(strType, "ProductA") == 0)
{
return new ProductA;
}
else if(strcmp(strType, "ProductB") == 0)
{
return new ProductB;
}
else
{
return 0;
}
}
};
void ProductA::display()
{
cout<<"this is ProductA"<<endl;
}
void ProductB::display()
{
cout<<"this is ProductB"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Factory* pFactory = new Factory;
if(pFactory != NULL)
{
Product* pProductA = pFactory->CreatProduct("ProductA");
if(pProductA != NULL)
{
pProductA->display();
}
Product* pProductB = pFactory->CreatProduct("ProductB");
if(pProductB != NULL)
{
pProductA->display();
}
}
getchar();
return 0;
}
//
#include "stdafx.h"
#include <iostream>
//#include "Factory.h"
using namespace std;
class Product
{
public:
virtual void display() = 0;
};
class ProductA:public Product
{
void display();
};
class ProductB:public Product
{
void display();
};
class Factory
{
public:
Product* CreatProduct(const char* strType)
{
if(strcmp(strType, "ProductA") == 0)
{
return new ProductA;
}
else if(strcmp(strType, "ProductB") == 0)
{
return new ProductB;
}
else
{
return 0;
}
}
};
void ProductA::display()
{
cout<<"this is ProductA"<<endl;
}
void ProductB::display()
{
cout<<"this is ProductB"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Factory* pFactory = new Factory;
if(pFactory != NULL)
{
Product* pProductA = pFactory->CreatProduct("ProductA");
if(pProductA != NULL)
{
pProductA->display();
}
Product* pProductB = pFactory->CreatProduct("ProductB");
if(pProductB != NULL)
{
pProductA->display();
}
}
getchar();
return 0;
}