面向对象编程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 <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的抽象。
 



### 面向对象编程编程的核心区别及应用场景 #### 核心区别 1. **关注点不同** 面向对象编程(OOP)更关注对象及其之间的交互,强调对象的行为和状态。它通过类和对象来封装数据和行为,使用继承和多态来实现代码的灵活性和扩展性[^3]。而编程更关注通用算法和数据结构的设计实现,强调代码的通用性和重用性[^4]。 2. **设计方法不同** 面向对象编程通过类和对象来组织代码,利用继承和多态实现代码的复用和扩展。例如,通过定义基类 `Shape` 和派生类 `Circle`、`Rectangle`,可以实现多态调用 `draw()` 方法[^3]。编程则通过参数化类和函数实现通用算法和数据结构,利用模板机制使得代码能够适应不同的数据类[^4]。 3. **抽象层次不同** 面向对象编程的抽象主要体现在类和对象上,通过封装、继承和多态实现高层次的抽象。编程的抽象体现在类参数化上,通过模板机制允许程序员编写具体类无关的代码[^4]。 #### 应用场景 1. **面向对象编程的应用场景** 面向对象编程更适合描述具体的实体和对象之间的关系,尤其适用于模拟现实世界的场景。例如,游戏开发中的人物、车辆等对象可以通过类和对象进行建模[^3]。以下是一个简单的面向对象编程示例: ```cpp class Animal { public: virtual void sound() = 0; // 纯虚函数 }; class Dog : public Animal { public: void sound() override { std::cout << "Woof!" << std::endl; } }; class Cat : public Animal { public: void sound() override { std::cout << "Meow!" << std::endl; } }; ``` 2. **编程的应用场景** 编程更适合编写通用的算法和数据结构,以适应不同的数据类。例如,STL(Standard Template Library)中的容器(如 `vector`、`list`)和算法(如 `sort`、`find`)都是编程的典例子[^1]。以下是一个编程的示例: ```cpp template <typename T> void Swap(T& a, T& b) { T temp = a; a = b; b = temp; } int main() { int x = 5, y = 10; Swap(x, y); // 调用 Swap<int> return 0; } ``` #### 总结 面向对象编程编程各有其适用场景和核心特点。面向对象编程适合描述具体的实体和对象之间的关系,强调代码的封装性和扩展性;而编程适合编写通用的算法和数据结构,强调代码的通用性和重用性[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值