332 lines
10 KiB
C++
332 lines
10 KiB
C++
//#include "factorypattern/shapefactory.h"
|
|
//#include "proxypattern/interface.h"
|
|
//#include "templatepattern/interface.h"
|
|
//#include "strategypattern/context.h"
|
|
//#include "flyweightpattern/shapefactory.h"
|
|
//#include "visitorpattern/son.h"
|
|
//#include "mediatorpattern/usr.h"
|
|
//#include "observerpattern/subject.h"
|
|
//#include "statepattern/interface.h"
|
|
//#include "commandpattern/broker.h"
|
|
//#include "decoratorpattern/second_interface.h"
|
|
//#include "bridgepattern/second_interface.h"
|
|
//#include "iteratorpattern/interface.h"
|
|
//#include "compositepattern/employee.h"
|
|
//#include "facadepattern/shapemaker.h"
|
|
//#include "adapterpattern/second_interface.h"
|
|
//#include "abstractfactorypattern/factoryproducer.h"
|
|
//#include "builderpattern/meakbuilder.h"
|
|
//#include "prototypepattern/interface.h"
|
|
//#include "interpreterpattern/interface.h"
|
|
#include "chainofresponsibilitypattern/interface.h"
|
|
#include "mementopattern/originator.h"
|
|
#include "mementopattern/caretaker.h"
|
|
|
|
int main()
|
|
{
|
|
//工厂模式
|
|
/*
|
|
ShapeFactory shapeFactory;
|
|
Circle *circle = static_cast<Circle*>(shapeFactory.getShape("Circle"));
|
|
circle->draw();
|
|
cout << endl;
|
|
Square *square = static_cast<Square*>(shapeFactory.getShape("Square"));
|
|
square->draw();
|
|
cout << endl;
|
|
Rectangle *rectangle = static_cast<Rectangle*>(shapeFactory.getShape("Rectangle"));
|
|
rectangle->draw();
|
|
*/
|
|
//代理模式
|
|
/*
|
|
Image *image = new ProxyImage("maoye.jpg");
|
|
image->display();
|
|
*/
|
|
//模板模式
|
|
/*
|
|
Game *game = new Cricket();
|
|
game->play();
|
|
cout << endl;
|
|
game = new Football();
|
|
game->play();
|
|
*/
|
|
//策略模式
|
|
/*
|
|
Strategy *operationAdd = new OperationAdd();
|
|
Strategy *operationSubstract = new OperationSubstract();
|
|
Strategy *operationMultiply = new OperationMultiply();
|
|
Context *context = new Context(operationAdd);
|
|
cout << "10 + 5 = " << context->executeStrategy(10, 5) << endl;
|
|
context = new Context(operationSubstract);
|
|
cout << "10 - 5 = " << context->executeStrategy(10, 5) << endl;
|
|
context = new Context(operationMultiply);
|
|
cout << "10 * 5 = " << context->executeStrategy(10, 5) << endl;
|
|
*/
|
|
//享元模式
|
|
/*
|
|
ShapeFactory shapeFactory;
|
|
string array[] = { "Red", "Blue", "Red", "Yellow", "Blue" };
|
|
for (auto color : array)
|
|
{
|
|
int x = rand() % 100;
|
|
int y = rand() % 100;
|
|
int radius = rand() % 100;
|
|
Circle *circle = static_cast<Circle*>(shapeFactory.getCircle(color));
|
|
circle->setX(x);
|
|
circle->setY(y);
|
|
circle->setRadius(radius);
|
|
circle->draw();
|
|
}
|
|
*/
|
|
//访问者模式
|
|
/*
|
|
ComputerPartDisplayVisitor *cp = new ComputerPartDisplayVisitor();
|
|
ComputerPart *computer = new Computer();
|
|
ComputerPart *mouse = new Mouse();
|
|
ComputerPart *keyboard = new Keyboard();
|
|
ComputerPart *monitor = new Monitor();
|
|
computer->accept(cp);
|
|
cout << endl;
|
|
mouse->accept(cp);
|
|
keyboard->accept(cp);
|
|
monitor->accept(cp);
|
|
*/
|
|
//中介者模式
|
|
/*
|
|
User *user1 = new User("Billy");
|
|
User *user2 = new User("Kitty");
|
|
User *user3 = new User("Alice");
|
|
cout << endl;
|
|
user1->showMessage("hello");
|
|
user2->showMessage("world");
|
|
user3->showMessage("!");
|
|
*/
|
|
//观察者模式
|
|
/*
|
|
Subject *subject = new Subject(10);
|
|
Observer *binaryObserver = new BinaryObserver();
|
|
Observer *octalObserver = new OctalObserver();
|
|
Observer *hexaObserver = new HexaObserver();
|
|
subject->attach(binaryObserver);
|
|
subject->attach(octalObserver);
|
|
subject->attach(hexaObserver);
|
|
cout << subject->getState() << endl;
|
|
subject->setState(20);
|
|
cout << subject->getState() << endl;
|
|
*/
|
|
//状态模式
|
|
/*
|
|
Context *context = new Context();
|
|
State *startState = new StartState();
|
|
startState->doAction(context);
|
|
cout << context->getState()->getString() << endl;
|
|
State *stopState = new StopState();
|
|
stopState->doAction(context);
|
|
cout << context->getState()->getString() << endl;
|
|
*/
|
|
//命令模式
|
|
/*
|
|
Stock *stock = new Stock();
|
|
Order * buyStock = new BuyStock(stock);
|
|
Order * sellStock = new SellStock(stock);
|
|
Broker * broker = new Broker();
|
|
broker->addOrder(buyStock);
|
|
broker->addOrder(sellStock);
|
|
broker->executeAllOrder();
|
|
*/
|
|
//装饰模式
|
|
/*
|
|
Shape *circle = new Circle();
|
|
ShapeDecorator *redCircle = new RedShapeDecorator(new Circle());
|
|
ShapeDecorator *redRectangle = new RedShapeDecorator(new Rectangle());
|
|
circle->draw();
|
|
cout << endl;
|
|
redCircle->draw();
|
|
cout << endl;
|
|
redRectangle->draw();
|
|
*/
|
|
//桥接模式
|
|
/*
|
|
DrawApi *redDrawApi = new RedCircle();
|
|
DrawApi *greenDrawApi = new GreenCircle();
|
|
Shape *redCircle = new Circle(100, 10, 10, redDrawApi);
|
|
Shape *greenCircle = new Circle(100, 10, 10, greenDrawApi);
|
|
redCircle->draw();
|
|
greenCircle->draw();
|
|
*/
|
|
//迭代器模式
|
|
/*
|
|
std::vector<string> vector;
|
|
vector.push_back("Billy");
|
|
vector.push_back("Alice");
|
|
vector.push_back("Kitty");
|
|
vector.push_back("Ben");
|
|
vector.push_back("Jason");
|
|
vector.push_back("Jack");
|
|
vector.push_back("Miss");
|
|
NameRepository *nameRepository = new NameRepository(0, vector);
|
|
for(Iterator *iterator = nameRepository->getIterator(); iterator->hasNext();)
|
|
{
|
|
cout << iterator->next() << endl;
|
|
}
|
|
*/
|
|
//组合模式
|
|
/*
|
|
Employee *CEO = new Employee("Billy", "CEO", 50000);
|
|
Employee *vice_president = new Employee("Killy", "vice_president", 40000);
|
|
Employee *lead_engineer = new Employee("Ben", "lead_engineer", 40000);
|
|
Employee *marketing_manager1 = new Employee("Jason", "marketing_manager1", 30000);
|
|
Employee *marketing_manager2 = new Employee("Jon", "marketing_manager2", 30000);
|
|
Employee *artisan1 = new Employee("Alice", "artisan1", 10000);
|
|
Employee *artisan2 = new Employee("Sam", "artisan2", 10000);
|
|
CEO->add(vice_president);
|
|
CEO->add(lead_engineer);
|
|
vice_president->add(marketing_manager1);
|
|
vice_president->add(marketing_manager2);
|
|
lead_engineer->add(artisan1);
|
|
lead_engineer->add(artisan2);
|
|
cout << endl << "CEO: " << CEO->getDetails() << endl;
|
|
for (auto employee : CEO->getVector())
|
|
{
|
|
cout << endl << "Leader: " << employee->getDetails() << endl;
|
|
cout << "Employee: " << endl;
|
|
for (auto e : employee->getVector())
|
|
{
|
|
cout << e->getDetails() << endl;
|
|
}
|
|
}
|
|
*/
|
|
//外观模式
|
|
/*
|
|
ShapeMaker shapeMaker;
|
|
shapeMaker.drawCircle();
|
|
shapeMaker.drawSquare();
|
|
shapeMaker.drawRectangle();
|
|
*/
|
|
//适配器模式
|
|
/*
|
|
AudioPlayer *audioPlayer = new AudioPlayer();
|
|
audioPlayer->play("mp3", "beyond the horizon.mp3");
|
|
audioPlayer->play("mp4", "alone.mp4");
|
|
audioPlayer->play("vlc", "far far away.vlc");
|
|
audioPlayer->play("avi", "mind me.avi");
|
|
*/
|
|
//抽象工厂模式
|
|
/*
|
|
FactoryProducer factoryProducer;
|
|
AbstractFactory * shapeFactory = factoryProducer.getFactory("ShapeFactory");
|
|
AbstractFactory * colorFactory = factoryProducer.getFactory("ColorFactory");
|
|
cout << endl;
|
|
Shape * circle = shapeFactory->getShape("Circle");
|
|
circle->draw();
|
|
cout << endl;
|
|
Shape * square = shapeFactory->getShape("Square");
|
|
square->draw();
|
|
cout << endl;
|
|
Shape * rectangle = shapeFactory->getShape("Rectangle");
|
|
rectangle->draw();
|
|
cout << endl;
|
|
Color * red = colorFactory->getColor("Red");
|
|
red->fill();
|
|
cout << endl;
|
|
Color * green = colorFactory->getColor("Green");
|
|
green->fill();
|
|
cout << endl;
|
|
Color * blue = colorFactory->getColor("Blue");
|
|
blue->fill();
|
|
*/
|
|
//建造者模式
|
|
/*
|
|
MealBuilder * mealBuilder = new MealBuilder();
|
|
Meal * vegMeal = mealBuilder->prepareVegMeal();
|
|
cout << "Veg Meal" << endl;
|
|
vegMeal->showFoods();
|
|
cout << "Total Cost: " << vegMeal->getCost() << endl;
|
|
Meal * nonVegMeal = mealBuilder->prepareNonVegMeal();
|
|
cout << "\n\nNon-Veg Meal" << endl;
|
|
nonVegMeal->showFoods();
|
|
cout << "Total Cost: " << nonVegMeal->getCost() << endl;
|
|
*/
|
|
//原型模式
|
|
/*
|
|
Shape *circle1 = new Circle("circle");
|
|
circle1->test();
|
|
cout << endl;
|
|
Shape *circle2 = circle1->clone();
|
|
circle2->test();
|
|
cout << endl;
|
|
if (circle1 == circle2)
|
|
{
|
|
cout << "same" << endl;
|
|
}
|
|
else
|
|
{
|
|
cout << "different" << endl;
|
|
}
|
|
*/
|
|
//解析器模式
|
|
/*
|
|
Expression *robert = new TerminalExpression("Robert");
|
|
Expression *john = new TerminalExpression("John");
|
|
Expression *isMale = new OrExpression(robert, john);
|
|
Expression *julie = new TerminalExpression("Julie");
|
|
Expression *married = new TerminalExpression("Married");
|
|
Expression *isMarriedWoman = new AndExpression(julie, married);
|
|
string ret1 = isMale->interpret("John") ? "true" : "false";
|
|
string ret2 = isMarriedWoman->interpret("Married Julie") ? "true" : "false";
|
|
cout << endl;
|
|
cout << "John is male? " + ret1 << endl;
|
|
cout << "Julie is a married women? " + ret2 << endl;
|
|
*/
|
|
//责任链模式
|
|
|
|
AbstractLogger *errorLogger = new ErrorLogger(AbstractLogger::ERROR);
|
|
AbstractLogger *fileLogger = new FileLogger(AbstractLogger::DEBUG);
|
|
AbstractLogger *consoleLogger = new ConsoleLogger(AbstractLogger::INFO);
|
|
cout << endl;
|
|
errorLogger->setNextLogger(fileLogger);
|
|
fileLogger->setNextLogger(consoleLogger);
|
|
errorLogger->logMessage(AbstractLogger::INFO, "This is an information");
|
|
errorLogger->logMessage(AbstractLogger::DEBUG, "This is an debug level information");
|
|
errorLogger->logMessage(AbstractLogger::ERROR, "This is an error information");
|
|
cout << endl;
|
|
fileLogger->logMessage(AbstractLogger::INFO, "This is an information");
|
|
fileLogger->logMessage(AbstractLogger::DEBUG, "This is an debug level information");
|
|
fileLogger->logMessage(AbstractLogger::ERROR, "This is an error information");
|
|
|
|
//备忘录模式
|
|
/*
|
|
Originator *originator = new Originator();
|
|
Caretaker *caretaker = new Caretaker();
|
|
originator->setState("state #1");
|
|
caretaker->add(originator->saveStateToMemento());
|
|
originator->setState("state #2");
|
|
caretaker->add(originator->saveStateToMemento());
|
|
originator->setState("state #3");
|
|
caretaker->add(originator->saveStateToMemento());
|
|
originator->setState("state #4");
|
|
caretaker->add(originator->saveStateToMemento());
|
|
originator->setState("state #5");
|
|
caretaker->add(originator->saveStateToMemento());
|
|
cout << endl;
|
|
for(size_t i = 0; i < caretaker->getVectorLength(); i++)
|
|
{
|
|
originator->getStateFromMemento(caretaker->getMemento(i));
|
|
cout << "current state: " + originator->getState() << endl;
|
|
}
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|