Merge pull request #4 from ichdream/dp-ich

改用shared_ptr统一管理对象
master
Qiangguo Feng 2021-09-07 13:26:14 +08:00 committed by GitHub
commit b7b878eabf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 66 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
bin
obj
main
*.o

View File

@ -0,0 +1,29 @@
DIR_INC = ./
DIR_SRC = ./
DIR_OBJ = ./obj
DIR_BIN = ./bin
SRC = $(wildcard ${DIR_SRC}/*.cpp)
OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC}))
TARGET = main
BIN_TARGET = ${DIR_BIN}/${TARGET}
CC = g++
CFLAGS = -g -Wall -I${DIR_INC}
${BIN_TARGET}:${OBJ}
mkdir -p $(DIR_BIN)
$(CC) $(OBJ) -o $@
${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp
mkdir -p $(DIR_OBJ)
$(CC) $(CFLAGS) -c $< -o $@
.PHONY:clean
clean:
rm -rf bin obj
rm -rf ./main

View File

@ -5,20 +5,22 @@
#include <string.h>
using namespace std;
//抽象产品类AbstractProduct
//抽象产品类AbstractProduct
class AbstractSportProduct
{
public:
AbstractSportProduct(){
}
virtual ~AbstractSportProduct(){}
//抽象方法:
virtual void printName(){};
virtual void play(){};
virtual ~AbstractSportProduct(){};
//抽象方法:
// virtual void printName(){};
virtual void printName() = 0;
// virtual void play(){};
virtual void play() = 0;
};
//具体产品类Basketball
//具体产品类Basketball
class Basketball :public AbstractSportProduct
{
public:
@ -30,7 +32,7 @@ public:
{
}
//具体实现方法
//具体实现方法
void printName(){
printf("Jungle get Basketball\n");
}
@ -39,7 +41,7 @@ public:
}
};
//具体产品类Football
//具体产品类Football
class Football :public AbstractSportProduct
{
public:
@ -51,7 +53,7 @@ public:
{
}
//具体实现方法
//具体实现方法
void printName(){
printf("Jungle get Football\n");
}
@ -60,7 +62,7 @@ public:
}
};
//具体产品类Volleyball
//具体产品类Volleyball
class Volleyball :public AbstractSportProduct
{
public:
@ -72,7 +74,7 @@ public:
{
}
//具体实现方法
//具体实现方法
void printName(){
printf("Jungle get Volleyball\n");
}
@ -84,17 +86,17 @@ public:
class Factory
{
public:
AbstractSportProduct *getSportProduct(string productName)
std::shared_ptr<AbstractSportProduct> getSportProduct(string productName)
{
AbstractSportProduct *pro = NULL;
std::shared_ptr<AbstractSportProduct> pro;
if (productName == "Basketball"){
pro = new Basketball();
pro = std::shared_ptr<AbstractSportProduct>(new Basketball());
}
else if (productName == "Football"){
pro = new Football();
pro = std::shared_ptr<AbstractSportProduct>(new Football());
}
else if (productName == "Volleyball"){
pro = new Volleyball();
pro = std::shared_ptr<AbstractSportProduct>(new Volleyball());
}
return pro;
}

View File

@ -1,30 +1,25 @@
#include <iostream>
#include <memory>
#include "SimpleFactory.h"
int main()
{
printf("简单工厂模式\n");
printf("简单工厂模式\n");
//定义工厂类对象
Factory *fac = new Factory();
AbstractSportProduct *product = NULL;
product = fac->getSportProduct("Basketball");
if (product) {
delete product;
}
//定义工厂类对象
std::shared_ptr<Factory> fac = std::make_shared<Factory>();
// std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Basketball"));
std::shared_ptr<AbstractSportProduct> product = fac->getSportProduct("Basketball");
fac = std::make_shared<Factory>();
product = fac->getSportProduct("Football");
if (product) {
delete product;
}
// product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Football"));
fac = std::make_shared<Factory>();
product = fac->getSportProduct("Volleyball");
if (product) {
delete product;
}
delete fac;
// product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct("Volleyball"));
#ifdef win32
system("pause");
#endif
return 0;
}

View File

@ -1,44 +1,23 @@
#include <iostream>
#include "FactoryMethod.h"
#include <memory>
int main()
{
printf("工厂方法模式\n");
//定义工厂类对象和产品类对象
AbstractFactory *fac = NULL;
AbstractSportProduct *product = NULL;
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;
}
std::shared_ptr<AbstractFactory> fac = make_shared<BasketballFactory>();
std::shared_ptr<AbstractSportProduct> product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
fac = make_shared<FootballFactory>();
// product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
fac = make_shared<VolleyballFactory>();
product = std::shared_ptr<AbstractSportProduct>(fac->getSportProduct());
system("pause");
return 0;
}