面向对象编程VS泛型编程

面向对象编程VS泛型编程
1、面向对象浅析 
       OOP是对data&&operation的封装,是对同类事物的抽象,跟结构化编程相比它更接近自然语言。继承使得OO具有了更强的表达能力,进一步地接近了自然语言的属性。而多态则是OO的最为巧妙和强大的地方,它催生了一系列的设计模式,而设计模式的应用体现了OOP的精髓。

2、泛型编程
       泛型编程是对class&&operation的抽象,对class的抽象可以说是对interface的抽象,而operation抽象则是对具有同一interface的class的operation的抽象。

下面是一个简单的Demo通过上面两种方式实现对加法和减法的抽象,从而来看看它们的不同。

Demo1:通过工厂模式对创建操作类进行了封装
/**
 *	Author: ACb0y
 *	File Name: Test.cpp
 *	Create Time: 2011年9月20日22:30:40
 *	Version: 1.0
 */

#include 
using namespace std;

class OperationInterface
{
public:
	int m_nLvalue;
	int m_nRvalue;
public:
	void Input() { cin >> m_nLvalue >> m_nRvalue; }
	virtual int Operation() = 0;
};

class OperationA : public OperationInterface
{
public:
	int Operation() 
	{ 
		int res = m_nLvalue + m_nRvalue;
		cout << "The result = " << res << endl;
		return res; 
	}
};

class OperationM : public OperationInterface
{
public:
	int Operation() 
	{ 
		int res = m_nLvalue - m_nRvalue;
		cout << "The result = " << res << endl;
		return res; 
	}
};

class OperationFactory 
{
private:
	OperationInterface * m_pOperation;
public: 
	OperationFactory() : m_pOperation(NULL) {}
	~OperationFactory() { delete m_pOperation; }
	OperationInterface * CreateOperation(char type);
};

OperationInterface * OperationFactory::CreateOperation(char type) 
{
	if (m_pOperation != NULL) 
	{
		delete m_pOperation;
	}
	switch (type) 
	{
	case 'A':
		m_pOperation = new OperationA();
		break;
	case 'M':
		m_pOperation = new OperationM();
		break;
	default:
		break;
	}
	return m_pOperation;
}


int main()
{
	OperationFactory operationFactory;	
	
	OperationInterface * pOperation = operationFactory.CreateOperation('A');
	
	pOperation->Input();
	
	pOperation->Operation();
	
	pOperation = operationFactory.CreateOperation('M');
	
	pOperation->Input();
	
	pOperation->Operation();
	
	return 0;
}

Demo2:通过泛型编程实现。
/**
 *	Author: ACb0y
 *	File Name: Test.cpp
 *	Create Time: 2011年9月20日23:38:50
 *	Version: 1.0
 */

#include 
using namespace std;

class OperationInterface
{
public:
	int m_nLvalue;
	int m_nRvalue;
public:
	void Input() { cin >> m_nLvalue >> m_nRvalue; }
	virtual int Operation() = 0;
};

class OperationA : public OperationInterface
{
public:
	int Operation() 
	{ 
		int res = m_nLvalue + m_nRvalue;
		cout << "The result = " << res << endl;
		return res; 
	}
};

class OperationM : public OperationInterface
{
public:
	int Operation() 
	{ 
		int res = m_nLvalue - m_nRvalue;
		cout << "The result = " << res << endl;
		return res; 
	}
};

template
class Operation
{
private:
	OperationType m_Op;
public:
	void InputData() { m_Op.Input(); }
	void PrintResult() { m_Op.Operation(); }
};

int main()
{
	Operation OpA;
	
	OpA.InputData();
	
	OpA.PrintResult();
	
	Operation OpM;
	
	OpM.InputData();
	
	OpM.PrintResult();
	
	return 0;
}
总结: 
       通过上面的比较我们知道这两种方式都较好的对变化进行了抽象和封装,不同的是Demo1中使用了多态来实现封装变化,而Demo2只是通过用不同的类来实例化模板类来实现对变化的封装。说白点泛型编程实际上就是实现了一坨的类。所以说OO是对是对事物data&&operation的抽象,泛型编程是对OO的抽象。
 
更详尽的区别:http://blog.csdn.net/wuliaoshengji/article/details/449870



你可能感兴趣的:(读书笔记,随笔,C++,语言)