// refactoringmotherfucker.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include<iostream>#include<vector>#include<memory>// before refactoring of split loopclassPeople{public:People(double _age,double _salary):age(_age),salary(_salary){}double age;double salary;};classBeforeApplication{public:voiddoWork(){init();printValues();}voidinit(){peoples_.push_back(std::make_shared<People>(23,8000));peoples_.push_back(std::make_shared<People>(25,13000));peoples_.push_back(std::make_shared<People>(30,15000));}voidprintValues(){double averageAge =0;double totalSalary =0;for(int i =0; i < peoples_.size(); i++){averageAge += peoples_.at(i)->age;totalSalary += peoples_.at(i)->salary;}averageAge = averageAge / peoples_.size();std::cout <<"the average age is: "<< averageAge << std::endl;std::cout <<"the total salary is: "<< totalSalary << std::endl;}private:std::vector<std::shared_ptr<People>> peoples_;};// after refactoring of split loopclassAfterApplication{public:voiddoWork(){init();printValues();}voidinit(){peoples_.push_back(std::make_shared<People>(23,8000));peoples_.push_back(std::make_shared<People>(25,13000));peoples_.push_back(std::make_shared<People>(30,15000));}voidprintValues(){std::cout <<"the average age is: "<<averageAge()<< std::endl;std::cout <<"the total salary is: "<<totalSalary()<< std::endl;}doubleaverageAge(){double result =0;for(int i =0; i < peoples_.size(); i++){result += peoples_[i]->age;}return result / peoples_.size();}doubletotalSalary(){double result =0;for(int i =0; i < peoples_.size(); i++){result += peoples_[i]->salary;}return result;}private:std::vector<std::shared_ptr<People>> peoples_;};intmain(){// refactoring beforestd::shared_ptr<BeforeApplication> before_app = std::make_shared<BeforeApplication>();before_app->doWork();// refactoring afterstd::shared_ptr<AfterApplication> after_app = std::make_shared<AfterApplication>();after_app->doWork();}