C++实现w3cshool设计模式教程--设计模式中的Java代码

刚开始接触设计模式。看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!








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值