QtDesignPatterns/main.cpp

332 lines
10 KiB
C++
Raw Permalink Normal View History

2024-02-06 17:21:37 +08:00
//#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()
{
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
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();
*/
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
Image *image = new ProxyImage("maoye.jpg");
image->display();
*/
//ģ<><C4A3>ģʽ
/*
Game *game = new Cricket();
game->play();
cout << endl;
game = new Football();
game->play();
*/
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
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;
*/
//<2F><>Ԫģʽ
/*
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();
}
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
/*
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);
*/
//<2F>н<EFBFBD><D0BD><EFBFBD>ģʽ
/*
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("!");
*/
//<2F>۲<EFBFBD><DBB2><EFBFBD>ģʽ
/*
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;
*/
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
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();
*/
//װ<><D7B0>ģʽ
/*
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();
*/
//<2F>Ž<EFBFBD>ģʽ
/*
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();
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
/*
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;
}
*/
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
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;
}
}
*/
//<2F><><EFBFBD><EFBFBD>ģʽ
/*
ShapeMaker shapeMaker;
shapeMaker.drawCircle();
shapeMaker.drawSquare();
shapeMaker.drawRectangle();
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
/*
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");
*/
//<2F><><EFBFBD>󹤳<EFBFBD>ģʽ
/*
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();
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
/*
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;
*/
//ԭ<><D4AD>ģʽ
/*
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;
}
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
/*
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;
*/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
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");
//<2F><><EFBFBD><EFBFBD>¼ģʽ
/*
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;
}