刚开始接触设计模式。看w3cshool设计模式教程上面讲的不错,但代码是用Java实现的。在此用C++实现下作为练习。
现把代码贴出来,记录自己的成长。同时也希望自己拙劣的代码能给刚学设计模式的同学一些帮助。也希望大家多多指点批评。
====================================================================
Shape.h
=====================================================================
#ifndef SHAPE_H
#define SHAPE_H
class Shape{
public:
virtual void draw();//定义为抽象类
};
#endif
=======================================================================
Shape.cpp
========================================================================
#include "shape.h"
#include <iostream>
void Shape::draw()
{
std::cout << "Inside Shape::draw() method" << std::endl;
}
========================================================================
Circle.h
========================================================================
#include "shape.h"
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle: public Shape{
public:
void draw();
};
#endif
===========================================================================
Circle.cpp
===========================================================================
#include "circle.h"
#include <iostream>
void Circle::draw()
{
std::cout << "Inside Circle::draw() method" << std::endl;
}
===========================================================================
Rectangle.h
===========================================================================
#include "shape.h"
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle: public Shape{
public:
void draw();
};
#endif
===========================================================================
Rectangle.cpp
===========================================================================
#include "rectangle.h"
#include <iostream>
void Rectangle::draw()
{
std::cout << "Inside Rectangle::draw() method" << std::endl;
}
===========================================================================
Square.h
===========================================================================
#include "shape.h"
#ifndef SQUARE_H
#define SQUARE_H
class Square: public Shape{
public:
void draw();
};
#endif
=============================================================================
Square.cpp
=============================================================================
#include "square.h"
#include <iostream>
void Square::draw()
{
std::cout << "Inside Square::draw() method" << std::endl;
}
=============================================================================
ShapeFactory.h
==============================================================================
#include "shape.h"
#include <string>
#ifndef SHAPE_FACTORY_H
class ShapeFactory{
public:
Shape* GetShape(std::string ShapeType);
};
#endif
==============================================================================
ShapeFactory.cpp
===============================================================================
#include "shapefactory.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
Shape* ShapeFactory::GetShape(std::string ShapeType)
{
if(ShapeType == "circle")
return new Circle();
else if(ShapeType == "rectangle")
return new Rectangle();
else if(ShapeType == "square")
return new Square();
return NULL;
}
==================================================================
FactoryPatternDemo.cpp
===================================================================
#include "shapefactory.h"
#include "Shape.h"
int main()
{
ShapeFactory* factory;
Shape *shape_1, *shape_2, *shape_3;//提醒自己注意指针的声明
shape_1 = factory->GetShape("circle");
shape_1->draw();
shape_2 = factory->GetShape("rectangle");
shape_2->draw();
shape_3 = factory->GetShape("square");
shape_3->draw();
return 0;
}
==============================================================================
附上运行结果:
最后,明天我要参加去哪儿面试了,跟自己说声GOOK LUCK!