设计模式例程
This commit is contained in:
79
inc/abstract_factory.hpp
Normal file
79
inc/abstract_factory.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class Shape // <20><>״<EFBFBD>ӿ<EFBFBD>
|
||||
{
|
||||
public:
|
||||
virtual void draw() = 0;
|
||||
|
||||
};
|
||||
|
||||
class Rectangle :public Shape
|
||||
{
|
||||
public:
|
||||
void draw();
|
||||
};
|
||||
|
||||
class Square :public Shape
|
||||
{
|
||||
public:
|
||||
void draw();
|
||||
};
|
||||
|
||||
class Circle :public Shape
|
||||
{
|
||||
public:
|
||||
void draw();
|
||||
};
|
||||
|
||||
class Color // <20><>ɫ<EFBFBD>ӿ<EFBFBD>
|
||||
{
|
||||
public:
|
||||
virtual void fill() = 0;
|
||||
|
||||
};
|
||||
|
||||
class Red :public Color
|
||||
{
|
||||
public:
|
||||
void fill();
|
||||
};
|
||||
|
||||
class Green :public Color
|
||||
{
|
||||
public:
|
||||
void fill();
|
||||
};
|
||||
|
||||
class Yellow :public Color
|
||||
{
|
||||
public:
|
||||
void fill();
|
||||
};
|
||||
|
||||
class AbstractFactory
|
||||
{
|
||||
public:
|
||||
virtual Color* getColor(string color) = 0;
|
||||
virtual Shape* getShape(string shape) = 0;
|
||||
};
|
||||
|
||||
class ShapeFactory :public AbstractFactory
|
||||
{
|
||||
public:
|
||||
Shape* getShape(string shape);
|
||||
Color* getColor(string color);
|
||||
};
|
||||
|
||||
class ColorFactory :public AbstractFactory
|
||||
{
|
||||
public:
|
||||
Shape* getShape(string shape);
|
||||
Color* getColor(string color);
|
||||
};
|
||||
|
||||
class FactoryProducer
|
||||
{
|
||||
public:
|
||||
AbstractFactory* getFactory(string choice);
|
||||
};
|
Reference in New Issue
Block a user