面向对象编程VS泛型编程

35 篇文章 0 订阅
18 篇文章 0 订阅
面向对象编程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 <iostream>
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 <iostream>
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 OperationType>
class Operation
{
private:
	OperationType m_Op;
public:
	void InputData() { m_Op.Input(); }
	void PrintResult() { m_Op.Operation(); }
};

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



  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值