diff --git a/.vs/DesignPattern/v15/.suo b/.vs/DesignPattern/v15/.suo new file mode 100644 index 0000000..77276bf Binary files /dev/null and b/.vs/DesignPattern/v15/.suo differ diff --git a/.vs/DesignPattern/v15/Browse.VC.db b/.vs/DesignPattern/v15/Browse.VC.db new file mode 100644 index 0000000..e6d468e Binary files /dev/null and b/.vs/DesignPattern/v15/Browse.VC.db differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/18e49952f9760469/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/18e49952f9760469/MAIN.ipch new file mode 100644 index 0000000..1e26bec Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/18e49952f9760469/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/1ff66978e541ecd2/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/1ff66978e541ecd2/MAIN.ipch new file mode 100644 index 0000000..c1a8594 Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/1ff66978e541ecd2/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/413db72e3e838c91/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/413db72e3e838c91/MAIN.ipch new file mode 100644 index 0000000..4228369 Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/413db72e3e838c91/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/86e8fdc107eae59f/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/86e8fdc107eae59f/MAIN.ipch new file mode 100644 index 0000000..0e392db Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/86e8fdc107eae59f/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/a3607a1363ab1d1b/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/a3607a1363ab1d1b/MAIN.ipch new file mode 100644 index 0000000..8b67c29 Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/a3607a1363ab1d1b/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/ad31c5b55b10d33d/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/ad31c5b55b10d33d/MAIN.ipch new file mode 100644 index 0000000..c656059 Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/ad31c5b55b10d33d/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/cb9285039b0d2305/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/cb9285039b0d2305/MAIN.ipch new file mode 100644 index 0000000..0d4a16f Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/cb9285039b0d2305/MAIN.ipch differ diff --git a/.vs/DesignPattern/v15/ipch/AutoPCH/e39d8e953d64f1b0/MAIN.ipch b/.vs/DesignPattern/v15/ipch/AutoPCH/e39d8e953d64f1b0/MAIN.ipch new file mode 100644 index 0000000..f248201 Binary files /dev/null and b/.vs/DesignPattern/v15/ipch/AutoPCH/e39d8e953d64f1b0/MAIN.ipch differ diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..e257ff9 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": "无配置" +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..58dcfba --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,18 @@ +{ + "ExpandedNodes": [ + "", + "\\02.FactoryMethod", + "\\02.FactoryMethod\\2.Code", + "\\03.AbstractFactory", + "\\03.AbstractFactory\\2.Code", + "\\04.BuilderPattern", + "\\04.BuilderPattern\\2.Code", + "\\05.PrototypePattern", + "\\05.PrototypePattern\\2.Code", + "\\06.Singleton", + "\\06.Singleton\\2.Code", + "\\06.Singleton\\2.Code\\Singleton" + ], + "SelectedNode": "\\06.Singleton\\2.Code\\Singleton\\main.cpp", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..01e0575 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/01.SimpleFactory/2.Code/main.cpp b/01.SimpleFactory/2.Code/main.cpp index 6a3837f..6dc0ed4 100644 --- a/01.SimpleFactory/2.Code/main.cpp +++ b/01.SimpleFactory/2.Code/main.cpp @@ -10,11 +10,21 @@ int main() AbstractSportProduct *product = NULL; product = fac->getSportProduct("Basketball"); + if (product) { + delete product; + } product = fac->getSportProduct("Football"); + if (product) { + delete product; + } product = fac->getSportProduct("Volleyball"); + if (product) { + delete product; + } + delete fac; system("pause"); return 0; } \ No newline at end of file diff --git a/02.FactoryMethod/2.Code/main.cpp b/02.FactoryMethod/2.Code/main.cpp index f8b8946..b332a4d 100644 --- a/02.FactoryMethod/2.Code/main.cpp +++ b/02.FactoryMethod/2.Code/main.cpp @@ -11,12 +11,33 @@ int main() fac = new BasketballFactory(); product = fac->getSportProduct(); + if (fac) + { + delete fac; + } + if (product) { + delete product; + } fac = new FootballFactory(); product = fac->getSportProduct(); + if (fac) + { + delete fac; + } + if (product) { + delete product; + } fac = new VolleyballFactory(); product = fac->getSportProduct(); + if (fac) + { + delete fac; + } + if (product) { + delete product; + } system("pause"); return 0; diff --git a/03.AbstractFactory/2.Code/main.cpp b/03.AbstractFactory/2.Code/main.cpp index 1a9659e..3c85d1c 100644 --- a/03.AbstractFactory/2.Code/main.cpp +++ b/03.AbstractFactory/2.Code/main.cpp @@ -10,10 +10,16 @@ int main() fac = new BasketballFactory(); ball = fac->getBall(); shirt = fac->getShirt(); + delete fac; + delete ball; + delete shirt; fac = new FootballFactory(); ball = fac->getBall(); shirt = fac->getShirt(); + delete fac; + delete ball; + delete shirt; system("pause"); return 0; diff --git a/04.BuilderPattern/2.Code/BuilderPattern.h b/04.BuilderPattern/2.Code/BuilderPattern.h index 2272c87..198dc34 100644 --- a/04.BuilderPattern/2.Code/BuilderPattern.h +++ b/04.BuilderPattern/2.Code/BuilderPattern.h @@ -9,18 +9,18 @@ using namespace std; class House { public: - House(){} - void setFloor(string iFloor){ + House() {} + void setFloor(string iFloor) { this->floor = iFloor; } - void setWall(string iWall){ + void setWall(string iWall) { this->wall = iWall; } - void setRoof(string iRoof){ + void setRoof(string iRoof) { this->roof = iRoof; } //ӡHouseϢ - void printfHouseInfo(){ + void printfHouseInfo() { printf("Floor:%s\t\n", this->floor.c_str()); printf("Wall:%s\t\n", this->wall.c_str()); printf("Roof:%s\t\n", this->roof.c_str()); @@ -35,9 +35,17 @@ private: class AbstractBuilder { public: - AbstractBuilder(){ + AbstractBuilder() { house = new House(); } + virtual ~AbstractBuilder() + { + if (house != nullptr) + { + delete house; + house = nullptr; + } + } //󷽷 virtual void buildFloor() = 0; virtual void buildWall() = 0; @@ -51,20 +59,28 @@ public: class ConcreteBuilderA :public AbstractBuilder { public: - ConcreteBuilderA(){ + ConcreteBuilderA() { printf("ConcreteBuilderA\n"); } + ~ConcreteBuilderA() + { + if (this->house != nullptr) + { + delete house; + house = nullptr; + } + } //ʵַ - void buildFloor(){ + void buildFloor() { this->house->setFloor("Floor_A"); } - void buildWall(){ + void buildWall() { this->house->setWall("Wall_A"); } - void buildRoof(){ + void buildRoof() { this->house->setRoof("Roof_A"); } - House *getHouse(){ + House *getHouse() { return this->house; } }; @@ -73,20 +89,28 @@ public: class ConcreteBuilderB :public AbstractBuilder { public: - ConcreteBuilderB(){ + ConcreteBuilderB() { printf("ConcreteBuilderB\n"); } + ~ConcreteBuilderA() + { + if (this->house != nullptr) + { + delete house; + house = nullptr; + } + } //ʵַ - void buildFloor(){ + void buildFloor() { this->house->setFloor("Floor_B"); } - void buildWall(){ + void buildWall() { this->house->setWall("Wall_B"); } - void buildRoof(){ + void buildRoof() { this->house->setRoof("Roof_B"); } - House *getHouse(){ + House *getHouse() { return this->house; } }; @@ -95,12 +119,20 @@ public: class Director { public: - Director(){} + Director() :builder(nullptr) {} + ~Director() + { + if (this->builder != nullptr) + { + delete builder; + builder = nullptr; + } + } //ʵַ - void setBuilder(AbstractBuilder *iBuilder){ + void setBuilder(AbstractBuilder *iBuilder) { this->builder = iBuilder; } - House *construct(){ + House *construct() { builder->buildFloor(); builder->buildWall(); builder->buildRoof(); diff --git a/04.BuilderPattern/2.Code/main.cpp b/04.BuilderPattern/2.Code/main.cpp index 5a24d22..466dcb3 100644 --- a/04.BuilderPattern/2.Code/main.cpp +++ b/04.BuilderPattern/2.Code/main.cpp @@ -14,6 +14,7 @@ int main() director->setBuilder(builder); house = director->construct(); house->printfHouseInfo(); + delete builder; //ָ彨B builder = new ConcreteBuilderB(); @@ -22,5 +23,9 @@ int main() house->printfHouseInfo(); system("pause"); + + delete director; + delete builder; + return 0; } \ No newline at end of file diff --git a/05.PrototypePattern/2.Code/main.cpp b/05.PrototypePattern/2.Code/main.cpp index 68f1c12..274afbd 100644 --- a/05.PrototypePattern/2.Code/main.cpp +++ b/05.PrototypePattern/2.Code/main.cpp @@ -48,5 +48,9 @@ int main() jungleWork->printWorkInfo(); system("pause"); + + delete singleWork; + delete jungleModel; + delete jungleWork; return 0; } \ No newline at end of file