An interesting example shows you the main difference between object-oriented programming and process oriented programming

Yesterday, a little friend asked me what is the main difference between object-oriented programming and process oriented programming. What are the advantages of object-oriented programming.
For these two problems, we might as well give an example.

Scenario description

Xiaohao Shouzhuo bakery, who lives in Sichuan, is listed as follows:

Xiaohao's hand cake is crispy and delicious. Coupled with Xiaohao's exclusive formula, it attracts many locals to taste Xiaohao's hand cake.
Due to his good management, Xiaohao makes a lot of money, but Xiaohao is very ambitious and wants to open chain stores in other places. In order to ensure that the hand grabbing cakes made by each chain store are basically the same, Xiaohao decided that the exclusive formula of hand grabbing cakes made by all chain stores will be released by the head office.
One of them plans to open in Shandong. After field investigation, Xiao Hao found that Shandong people are not very spicy, but they like to put five spices and so on. Xiaohao therefore fine tuned the feeding menu of Shandong chain stores.

problem

Now Xiaohao wants you to design a system to help him manage his chain stores.
Main functions:

  1. Calculate price per order
  2. Provide the production process of each order

Object oriented VS process oriented

Process oriented

//Sichuan chain store
//Sichuan cake price calculation
//shredded pancake
double SichuanbingPrice() {
	return 5.0;
}
//Spicy ham
double MalahuotuiPrice() {
	return 1.2;
}
//Spicy chicken chops
double MalajipaiPrice() {
	return 2.30;
}
//Spicy pork chop
double XianglazhupaiPrice() {
	return 1.0;
}
double SichuanSumPrice(vector<int> &foods) {
	double sum = 0.0;
	for (size_t i = 0; i < foods.size(); i++) {
		if (foods[i] == 0) {
			sum += SichuanbingPrice();
		}
		if (foods[i] == 1) {
			sum += MalahuotuiPrice();
		}
		if (foods[i] == 2) {
			sum += MalajipaiPrice();
		}
		if (foods[i] == 3) {
			sum += XianglazhupaiPrice();
		}
	}
	return sum;
}
//Sichuan hand cake making
void SichuanMake(vector<int> &foods) {
	cout << "Here, Xiaohao's hand grasping cake making process is used to make it (chain stores cannot change it)" << endl;
	string temp = "";
	for (size_t i = 0; i < foods.size(); i++) {
		if (foods[i] == 0) {
			temp += " Add cake ";
		}
		if (foods[i] == 1) {
			temp += " Add spicy ham ";
		}
		if (foods[i] == 2) {
			temp += " Add spicy chicken chops ";
		}
		if (foods[i] == 3) {
			temp += " Add spicy pork chop ";
		}
	}
	cout << temp << endl;
	cout << "Add Xiaohao's exclusive formula (chain stores cannot change it)" << endl;

}

//Shandong chain store
//Price calculation of Shandong hand grabbing cake
//shredded pancake
double ShandongbingPrice() {
	return 5.0;
}
//Spiced ham
double WuxianghuotuiPrice() {
	return 1.5;
}
//Spiced chicken chops
double WuxiangjipaiPrice() {
	return 2.0;
}
//Spiced pork chop
double WuxiangzhupaiPrice() {
	return 1.0;
}
double ShandongSumPrice(vector<int> &foods) {
	double sum = 0.0;
	for (size_t i = 0; i < foods.size(); i++) {
		if (foods[i] == 0) {
			sum += ShandongbingPrice();
		}
		if (foods[i] == 1) {
			sum += WuxianghuotuiPrice();
		}
		if (foods[i] == 2) {
			sum += WuxiangjipaiPrice();
		}
		if (foods[i] == 3) {
			sum += WuxiangzhupaiPrice();
		}
	}
	return sum;
}
//Shandong hand grab cake making
void ShandongMake(vector<int> &foods) {
	cout << "Here, Xiaohao's hand grasping cake making process is used to make it (chain stores cannot change it)" << endl;
	string temp = "";
	for (size_t i = 0; i < foods.size(); i++) {
		if (foods[i] == 0) {
			temp += " Add cake ";
		}
		if (foods[i] == 1) {
			temp += " Add spiced ham ";
		}
		if (foods[i] == 2) {
			temp += " Add spiced chicken chops ";
		}
		if (foods[i] == 3) {
			temp += " Add spiced pork chop ";
		}
	}
	cout << temp << endl;
	cout << "Add Xiaohao's exclusive formula (chain stores cannot change it)" << endl;

}

object-oriented

First, we design the class diagram according to the demand, imitating the simple factory pattern
We have designed two basic classes: one is the basic class of grab cake: foodbase (product) and the other is the basic class of chain store: storebase (factory)


Design the class according to the above class diagram

class FoodBase {
public :
	FoodBase() = default;
	virtual double getPrice() =0;
	virtual string getMethod() = 0;
};
class ShandongFoods :public FoodBase {
public:
	ShandongFoods() = default;
};

class Wuxianghuotui :public ShandongFoods {
public:
	Wuxianghuotui() = default;
	double getPrice() {
		return 1.5;
	}
	string getMethod() {
		return " Add spiced ham ";
	}
};
class Wuxiangjipai :public ShandongFoods {
public:
	Wuxiangjipai() = default;
	double getPrice() {
		return 2.0;
	}
	string getMethod() {
		return " Add spiced chicken chops ";
	}
};

class Wuxiangzhupai :public ShandongFoods {
public:
	Wuxiangzhupai() = default;
	double getPrice() {
		return 1.0;
	}
	string getMethod() {
		return " Add spiced pork chop ";
	}
};

class Shandongbing :public ShandongFoods {
public:
	Shandongbing() = default;
	double getPrice() {
		return 5.0;
	}
	string getMethod() {
		return " Add a cake ";
	}
};

//Sichuan
class SichuanFoods :public FoodBase {
public:
	SichuanFoods() = default;
};

class Malahuotui :public SichuanFoods {
public:
	Malahuotui() = default;
	double getPrice() {
		return 1.2;
	}
	string getMethod() {
		return " Add spicy ham ";
	}
};
class Malajipai :public SichuanFoods {
public:
	Malajipai() = default;
	double getPrice() {
		return 2.3;
	}
	string getMethod() {
		return " Add spicy chicken chops ";
	}
};

class Sichuanbing :public SichuanFoods {
public:
	Sichuanbing() = default;
	double getPrice() {
		return 5.0;
	}
	string getMethod() {
		return " Add a cake ";
	}
};

class Xianglazhupai :public SichuanFoods {
public:
	Xianglazhupai() = default;
	double getPrice() {
		return 1.0;
	}
	string getMethod() {
		return " Add spicy pork chop ";
	}
};


class StoreBase {
public:
	StoreBase() = default;
	virtual string getMethods() = 0;
	virtual double getPrice() = 0;
protected:
	string first_step, last_step;
	vector<int> foods_list;
	
	
private:
	void setSecret() {
		first_step = "Here, Xiaohao's hand grasping cake making process is used to make it (chain stores cannot change it)";
		last_step = "Add Xiaohao's exclusive formula (chain stores cannot change it)";
	}
	
};
class ShandongStore :public StoreBase {
public:
	ShandongStore(vector<int>foods) {
		foods_list = foods;
	}
	string getMethods() {
		string method = "";
		method += first_step + "\n";
		ShandongFoods *shandong_foods = nullptr;
		for (size_t i = 0; i < foods_list.size(); i++) {
			if (foods_list[i] == 0) {
				shandong_foods = new Shandongbing;
				method += shandong_foods->getMethod();
			}
			if (foods_list[i] == 1) {
				shandong_foods = new Wuxianghuotui;
				method += shandong_foods->getMethod();
			}
			if (foods_list[i] == 2) {
				shandong_foods = new Wuxiangjipai;
				method += shandong_foods->getMethod();
			}
			if (foods_list[i] == 3) {
				shandong_foods = new Wuxiangzhupai;
				method += shandong_foods->getMethod();
			}
		}
		delete shandong_foods;
		method += "\n"+last_step+"\n";
		return method;
	}
	double getPrice() {
		double price = 0.0;
		ShandongFoods *shandong_foods = nullptr;
		for (size_t i = 0; i < foods_list.size(); i++) {
			if (foods_list[i] == 0) {
				shandong_foods = new Shandongbing;
				price += shandong_foods->getPrice();
			}
			if (foods_list[i] == 1) {
				shandong_foods = new Wuxianghuotui;
				price += shandong_foods->getPrice();
			}
			if (foods_list[i] == 2) {
				shandong_foods = new Wuxiangjipai;
				price += shandong_foods->getPrice();
			}
			if (foods_list[i] == 3) {
				shandong_foods = new Wuxiangzhupai;
				price += shandong_foods->getPrice();
			}
		}
		delete shandong_foods;
		return price;
	}

};


class SichuanStore :public StoreBase {
public:
	SichuanStore(vector<int>foods) {
		foods_list = foods;
	}
	string getMethods() {
		string method = "";
		method += first_step + "\n";
		SichuanFoods *sichuan_foods = nullptr;
		for (size_t i = 0; i < foods_list.size(); i++) {
			if (foods_list[i] == 0) {
				sichuan_foods = new Sichuanbing;
				method += sichuan_foods->getMethod();
			}
			if (foods_list[i] == 1) {
				sichuan_foods = new Malahuotui;
				method += sichuan_foods->getMethod();
			}
			if (foods_list[i] == 2) {
				sichuan_foods = new Malajipai;
				method += sichuan_foods->getMethod();
			}
			if (foods_list[i] == 3) {
				sichuan_foods = new Xianglazhupai;
				method += sichuan_foods->getMethod();
			}
		}
		delete sichuan_foods;
		method += "\n" + last_step + "\n";
		return method;
	}
	double getPrice() {
		double price = 0.0;
		SichuanFoods *sichuan_foods = nullptr;
		for (size_t i = 0; i < foods_list.size(); i++) {
			if (foods_list[i] == 0) {
				sichuan_foods = new Sichuanbing;
				price += sichuan_foods->getPrice();
			}
			if (foods_list[i] == 1) {
				sichuan_foods = new Malahuotui;
				price += sichuan_foods->getPrice();
			}
			if (foods_list[i] == 2) {
				sichuan_foods = new Malajipai;
				price += sichuan_foods->getPrice();
			}
			if (foods_list[i] == 3) {
				sichuan_foods = new Xianglazhupai;
				price += sichuan_foods->getPrice();
			}
		}
		delete sichuan_foods;
		return price;
	}

};

(the above examples are fictional)

Object oriented advantage

modularization

In process oriented programming, a large number of functions are stored together without logic, which is not easy to manage when there are more functions.

Functions in object-oriented programming are encapsulated, and functions with certain logic are put together. When several functions with the same parameters are encapsulated in a class, there is no need to pass parameters repeatedly. (as shown in the figure above, both getMethods and getPrice functions need the foods variable. When encapsulated in a class, you can directly use the constructor to receive the variable value, and then there is no need to pass parameters.)

protection mechanism

In Xiaohao's requirements, the first step of making hand grabbing cake and adding exclusive secret recipe must be done according to the requirements of the head office, and the chain store cannot modify it by itself.

In process oriented programming, these two are written directly to other condiments, and the protection mechanism is very poor.
For object-oriented methods, the two methods that must be enforced and confidential are written in the private of the base class, so that the subclass cannot modify these methods.

Easy to expand

For object-oriented programming, if you want to add menus or chain stores in other regions, you only need to extend the corresponding class, and there is little change in the control class.
However, for process oriented programming, the new information still needs to be changed in the control class.

Easy to maintain


In the above figure, the left figure is process oriented programming and the right figure is object-oriented programming.
If you want to change the production method of spiced ham, for the object-oriented method, we can directly modify its production function in the corresponding class of spiced ham. However, for the process oriented method, we need to modify it in the production function of the whole process.

Strong reusability

The commonness and individuality of multiple implementations can be observed, and the commonness can be encapsulated into methods, while the individuality can adapt to the needs of different services by means of parameter transmission. Thus, the cohesion of functions is improved, the coupling with business code is reduced, and the principle of single responsibility of encapsulation is fulfilled.

Keywords: C++

Added by lala on Thu, 10 Feb 2022 13:35:24 +0200