fix memory leak
parent
ed859f709e
commit
80c928a497
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"CurrentProjectSetting": "无配置"
|
||||
}
|
|
@ -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
|
||||
}
|
Binary file not shown.
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -48,5 +48,9 @@ int main()
|
|||
jungleWork->printWorkInfo();
|
||||
|
||||
system("pause");
|
||||
|
||||
delete singleWork;
|
||||
delete jungleModel;
|
||||
delete jungleWork;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue