工厂模板模式(c++)
#include<iostream> #include<vector> #include<algorithm> using namespace std; //水果基类 class AstractFruit { public: virtual void showname() = 0; }; class Apple :public AstractFruit { public: void showname() { cout << "我是苹果" << endl; } }; class Pear :public AstractFruit { public: void showname() { cout << "我是梨" << endl; } }; class Banana :public AstractFruit { public: void showname() { cout << "我是香蕉" << endl; } }; //工厂基类 class AstractFactory { public: virtual AstractFruit* createFruit() = 0; }; class AppleFactory :public AstractFactory { public: AstractFruit* createFruit() { return new Apple; } }; class PearFactory :public AstractFactory { public: AstractFruit* createFruit() { return new Pear; } }; class BananaFactory :public AstractFactory { public: AstractFruit* createFruit() { return new Banana; } }; void test() { //创建一个苹果工厂 AstractFruit* fruit; AstractFactory* factory; factory = new AppleFactory; fruit = factory->createFruit(); fruit->showname(); delete factory; delete fruit; } //int lengthOfLIS(vector<int>& nums) { // int count = 1; // int temp= *nums.begin(); // for (auto it = nums.begin(); it != nums.end(); it++) // { // if (*it < temp)temp = *it; // } // bool flag = false; // for (auto it = nums.begin(); it != nums.end(); it++) // { // if (!flag) { // if (*it == temp)flag = true; // } // else { // if (*it > temp) { // temp = *it; // count++; // } // } // // // } // return count; //} //vector<int> twoSum(vector<int>& nums, int target) { // vector<int> rel; // for (int i = 0; i < nums.size(); i++) // { // for (int j = i+1; j < nums.size(); j++) // { // if (nums[i] + nums[j] == target) { // rel.push_back(i); // rel.push_back(j); // } // } // } // return rel; //} int main(){ //vector<int> nums = { 3,2,4 }; //auto ret = twoSum(nums,6); //for (int i = 0; i < ret.size(); i++) //{ // cout << ret[i] << "\t"; //} //cout << endl; //for (int i = 0; i < ret.size(); i++) //{ // cout << nums[ret[i]] << "\t"; //} test(); cin.get(); }