描述:对修改关闭,对拓展开启。
作用:
描述:子类可以拓展父类的功能,但不能改变父类的功能,子类可以能够替换父类,继承表达类型抽象。
作用:
描述:一个类应仅有一个引起它变化的原因。类的变化方向隐含类的职责。
作用:
描述:高层模块(稳定)不依赖于底层模块(变化),两者都依赖于抽象(稳定)。抽象(稳定)不依赖于细节(变化),实现细节应依赖于抽象(稳定)。就是程序要依赖于抽象的接口,不应该依赖于具体实现。
作用:
描述:不应该强迫客户程序使用它们不用的方法。接口应该小而完备。
作用:
描述:最小知识原则(Least Knowledge Principle)。如果两个软件实体不发生直接的数据通信的话,不应该使用直接的调用,而是通过第三方转发其调用(增加中间层)。
作用:
描述:优先使用对象组合,而不是类的继承。(类的继承称为“白箱复用”,对象组合称为“黑箱复用”)。
作用:
-实现方式:
应提供一个公有的静态函数,一个私有的静态成员变量,一个私有的构造函数。
class singleton
{
public:
static singleton* getInstance();
singleton(const singleton&) = delete;//do not duplicate the singleton class
singleton& operator=(const singleton&) = delete;
private:
singleton() = default;
static singleton* singletonPtr;
};
singleton* singleton::singletonPtr = nullptr;
singleton* singleton::getInstance()
{
if(singletonPtr == nullptr)
{
singletonPtr = new singleton();
}
return singletonPtr;
}
-实现方式:
class baseProduct
{
public:
virtual baseProduct* getProduct();
};
class A_product : public baseProduct
{
//create the A product
};
class B_product : public baseProduct
{
//create the B product
};
class A_productFactory : public A_product
{
public:
virtual baseProduct* getProduct()
{
return new A_product();
}
};
class B_productFactory : public B_product
{
public:
virtual baseProduct* getProduct()
{
return new B_product();
}
};
class A_abstractProduct
{
public:
virtual ~A_abstractProduct()=default;
virtual void doSomething()=0;
};
class B_abstractProduct
{
public:
virtual ~B_abstractProduct()=default;
virtual void doSomething()=0;
};
class abstractFactory
{
public:
virtual ~abstractFactory()=default;
virtual A_abstractProduct* returnProductA()=0;
virtual B_abstractProduct* returnProductB()=0;
};
class A1_concreteProduct : public A_abstractProduct
{
public:
virtual ~A1_concreteProduct()=default;
virtual void doSomething()
{
//work for product A1
}
};
class B1_concreteProduct : public B_abstractProduct
{
public:
virtual ~B1_concreteProduct()=default;
virtual void doSomething()
{
//work for product B1
}
};
class A2_concreteProduct : public A_abstractProduct
{
public:
virtual ~A2_concreteProduct()=default;
virtual void doSomething()
{
//work for product A2
}
};
class B2_concreteProduct : public B_abstractProduct
{
public:
virtual ~B2_concreteProduct()=default;
virtual void doSomething()
{
//work for product B2
}
};
class concreteFactory1 : public abstractFactory
{
public:
virtual ~concreteFactory1()=default;
virtual A_abstractProduct* returnProductA()
{
return new A1_concreteProduct();
}
virtual B_abstractProduct* returnProductB()
{
return new B1_concreteProduct();
}
};
class concreteFactory2 : public abstractFactory
{
public:
virtual ~concreteFactory2()=default;
virtual A_abstractProduct* returnProductA()
{
return new A2_concreteProduct();
}
virtual B_abstractProduct* returnProductB()
{
return new B2_concreteProduct();
}
};
int main(void)
{
//the usage of abstract factory
abstractFactory *productFactory = new concreteFactory1();
A_abstractProduct *productA = productFactory->returnProductA();
}
class building
{
};
class builder
{
public:
virtual ~builder()=default;
virtual void A_componentPart()=0;
virtual void B_componentPart()=0;
virtual void C_componentPart()=0;
virtual building* getResult()=0;
};
class gradenBuilder : public builder
{
public:
virtual ~gradenBuilder()=default;
virtual void A_componentPart()
{
//do something for finalBuilding part A
}
virtual void B_componentPart()
{
//do something for finalBuilding part B
}
virtual void C_componentPart()
{
//do something for finalBuilding part C
}
virtual building* getResult()
{
return finalBuilding;
}
private:
building* finalBuilding;
};
class buildDirect
{
virtual ~buildDirect()=default;
void givenBuildingPlan(builder* buildingPlan)
{
this->buildingPlan = buildingPlan;
}
building* construct()
{
buildingPlan->A_componentPart();
buildingPlan->B_componentPart();
buildingPlan->C_componentPart();
return buildingPlan->getResult();
}
builder *buildingPlan;
};
int main()
{
//************************the usage of abstract factory************************************************
gradenBuilder *gradenBuilderPlan = new gradenBuilder();
buildDirect *gradenDirector = new buildDirect();
gradenDirector->givenBuildingPlan(gradenBuilderPlan);
building *graden = gradenDirector->construct();
}
class constractProtoltype
{
public:
virtual ~constractProtoltype();
virtual constractProtoltype* clone() = 0;
};
class concreteProtoltype : public constractProtoltype
{
public:
virtual ~concreteProtoltype()=default;
virtual concreteProtoltype* clone()
{
return new concreteProtoltype(*this); //this is different from factory method
}
};
class abstractSubject
{
public:
virtual ~abstractSubject()=default;
virtual void process(void *argv)=0;
};
class concreteSubject : public abstractSubject
{
public:
virtual ~concreteSubject()=default;
virtual void process(void *argv)
{
//bussiness for concreteSubject
}
};
class subjectProxy
{
public:
virtual ~subjectProxy()=default;
virtual void proxyInterface(void *argv)
{
if(realSubject == nullptr)
{
realSubject = new concreteSubject();
}
realSubject->process(argv);
}
private:
abstractSubject *realSubject;
};
class existedComponent
{
public:
virtual ~existedComponent()=default;
virtual void existedProcess()
{
//bussiness about existed component
}
};
class adapterInterface
{
public:
adapterInterface(existedComponent *adapteeComponent):adapteeComponent(adapteeComponent){};
virtual ~adapterInterface()=default;
virtual void specificalInterface()
{
//do something to adapte the interface "existedProcess" in adapteeComponent
adapteeComponent->existedProcess();
}
private:
existedComponent* adapteeComponent;
};
class netClass
{
public:
virtual ~netClass()=default;
virtual void send()
{
}
virtual void recv()
{
}
virtual void connect()
{
}
};
class chatRoom : public netClass
{
public:
virtual ~chatRoom()=default;
virtual void sendMessage()
{
//construct a message
netClass::send();
}
virtual void login()
{
netClass::connect();
netClass::recv();
}
};
class baseOperate
{
public:
virtual ~baseOperate()=default;
virtual void decode()
{
}
virtual void encode()
{
}
};
class extraDriveOperate_A : public baseOperate
{
virtual ~extraDriveOperate_A();
virtual void write()
{
//extend function to construct a data stream.This part behaves the composition feature.
baseOperate::encode();
}
virtual void read()
{
baseOperate::decode();
//extend function to read the data stream.This part behaves the composition feature
}
};
class extraDriveOperate_B : public baseOperate
{
virtual ~extraDriveOperate_B();
virtual void write()
{
//extend function to construct a data stream.This part behaves the composition feature.
baseOperate::encode();
}
virtual void read()
{
baseOperate::decode();
//extend function to read the data stream.This part behaves the composition feature
}
};
//这种思想应当体现在统一的简化接口方面,考虑到笔者的时间有限,这个例子省略了。
class memoryBlock
{
public:
virtual ~memoryBlock()=default;
memoryBlock(std::string &key):key(key){}
private:
std::string key;
};
class memoryBlockFactory
{
public:
virtual ~memoryBlockFactory()=default;
virtual memoryBlock* getMemoryBlock(std::string key)
{
memoryBlock* foundMemoryBlock = nullptr;
auto it = memoryBlockPool.find(key);
if(it == memoryBlockPool.end())
{
foundMemoryBlock = new memoryBlock(key);
memoryBlockPool.insert(std::make_pair(key, foundMemoryBlock));
}
else
{
foundMemoryBlock = it->second;
}
return foundMemoryBlock;
}
private:
std::map<std::string, memoryBlock*> memoryBlockPool;
};
class composite
{
public:
virtual ~composite()=default;
virtual void process()=0;
};
class parentPoint : public composite
{
public:
virtual ~parentPoint()=default;
void addElement(composite *element)
{
elements.push_back(element);
}
void removeElement(composite *element)
{
elements.remove(element);
}
void process()
{
//do something about current node
//next step is leaf node process function
for(auto &it : elements)
{
it->process();
}
}
private:
std::list<composite*> elements;
};
class leafPoint : public composite
{
public:
virtual ~leafPoint()=default;
virtual void process()
{
//do something adout this leaf point function
}
};
void treeProcess(composite &point)
{
point.process();
}
int main()
{
parentPoint root;
parentPoint treeNode1;
parentPoint treeNode2;
leafPoint leaf1Node1;
leafPoint leafNode2;
root.addElement(&treeNode1);
root.addElement(&treeNode2);
treeNode1.addElement(&leaf1Node1);
treeNode2.addElement(&leafNode2);
treeProcess(root);
}
class abstractTemplateClass
{
virtual ~abstractTemplateClass()=default;
virtual void skeletonFlow()
{
specificalMethod_A();
abstractMethod_B();
if(hookJudgeFunc_C())
{
hookMethodFunc_D();
}
}
virtual void specificalMethod_A()
{
std::cout<<"the process of specifical Method"<<std::endl;
}
virtual void abstractMethod_B()=0;
virtual bool hookJudgeFunc_C()
{
return true;
}
virtual void hookMethodFunc_D()
{
std::cout<<"the process of abstract Method"<<std::endl;
}
};
class concreteTemplateClass_example_1 : public abstractTemplateClass
{
virtual ~concreteTemplateClass()=default;
virtual void specificalMethod_A()
{
std::cout<<"the drived class object is overwriting the process of specifical Method"<<std::endl;
}
virtual void abstractMethod_B()
{
std::cout<<"the drived class object is define the process of abstract Method"<<std::endl;
}
virtual bool hookJudgeFunc_C()
{
std::cout<<"the drived class object is overwriting the judgment of hook Method"<<std::endl;
return true;
}
virtual void hookMethodFunc_D()
{
std::cout<<"the drived class object is overwriting the process of hook Method"<<std::endl;
}
};
//In next part you can define more drived class if you like.
class abstrateStrategy
{
public:
virtual ~abstrateStrategy()=default;
virtual void strategyProcess()=0;
};
class specificalStrategy_A : public abstrateStrategy
{
public:
virtual ~specificalStrategy_A()=default;
virtual void strategyProcess()
{
//do something about specificalStrategy A type
}
};
class specificalStrategy_B : public abstrateStrategy
{
public:
virtual ~specificalStrategy_B()=default;
virtual void strategyProcess()
{
//do something about specificalStrategy B type
}
};
class strategyInterface
{
public:
strategyInterface():abstrateStrategyObject(nullptr) {};
virtual ~strategyInterface() {delete abstrateStrategyObject;};
virtual void givenStrategy(abstrateStrategy *ptr_strategy)
{
abstrateStrategyObject = ptr_strategy;
}
virtual void callStrategyProcess()
{
return abstrateStrategyObject->strategyProcess();
}
private:
abstrateStrategy *abstrateStrategyObject;
};
int main()
{
specificalStrategy_A *strategyObject_A = new specificalStrategy_A();
strategyInterface strategyInterface_1;
strategyInterface_1.givenStrategy(strategyObject_A);
strategyInterface_1.callStrategyProcess();
}
class abstractCommand
{
public:
virtual ~abstractCommand()=default;
virtual void givenCommand(std::string commandStr)=0;
virtual void commandProcess()=0;
protected:
std::string commandStr_;
};
class command_A : public abstractCommand
{
public:
command_A()=default;
command_A(string commandStr){ commandStr_ = commandStr;}
virtual ~command_A()=default;
virtual void givenCommand(std::string commandStr)
{
commandStr_ = commandStr;
}
virtual void commandProcess()
{
//do something about command A type
std::cout<< commandStr_ <<std::endl;
}
};
class command_B : public abstractCommand
{
public:
command_B()=default;
command_B(string commandStr){ commandStr_ = commandStr;}
virtual ~command_B()=default;
virtual void givenCommand(std::string commandStr)
{
commandStr_ = commandStr;
}
virtual void commandProcess()
{
//do something about command B type
std::cout<< commandStr_ <<std::endl;
}
};
class microCommand
{
public:
virtual ~microCommand()=default;
virtual void insertCommand(abstractCommand *commandNode)
{
commandList.emplace_back(commandNode);
}
virtual void microCommandProcess()
{
for(auto it : commandList)
{
it->commandProcess();
}
}
private:
std::list<abstractCommand*> commandList;
};
int main()
{
command_A *p_command_A = new command_A("command_A");
command_B *p_command_B = new command_B("command_B");
microCommand *p_microCommand = new microCommand();
p_microCommand->insertCommand(p_command_A);
p_microCommand->insertCommand(p_command_B);
p_microCommand->microCommandProcess();
}
//the example code to show the usage of memento methode is empty
class abstractHandle
{
public:
virtual ~abstractHandle()=default;
virtual bool handleProcess(void *argv)=0;
virtual void chainRespInterface(void *argv)
{
if(handleProcess(argv) == false)
{
if(getNextHandle() != nullptr)
{
getNextHandle()->chainRespInterface(argv);
}
else
{
return;
}
}
return;
}
virtual void givenNextHandle(abstractHandle *respHandle)
{
nextResposibility = respHandle;
}
virtual abstractHandle* getNextHandle()
{
return nextResposibility;
}
protected:
abstractHandle *nextResposibility;
};
class resposibility_A : public abstractHandle
{
public:
resposibility_A(){ nextResposibility = nullptr;}
virtual ~resposibility_A()=default;
virtual bool handleProcess(void *argv)
{
//task about resposibility A
std::cout<<"now resp A process"<<std::endl;
return false;
}
};
class resposibility_B : public abstractHandle
{
public:
resposibility_B(){nextResposibility = nullptr;}
virtual ~resposibility_B()=default;
virtual bool handleProcess(void *argv)
{
//task about resposibility B
std::cout<<"now resp B process"<<std::endl;
return false;
}
};
int main()
{
resposibility_A *resp_A = new resposibility_A();
resposibility_B *resp_B = new resposibility_B();
resp_A->givenNextHandle(resp_B);
resp_A->chainRespInterface(NULL);
}
class stateInterfaceClass;
class concreteState_A;
class concreteState_B;
class abstractState
{
public:
virtual ~abstractState()=default;
virtual void process(stateInterfaceClass*)=0;
};
class stateInterfaceClass
{
public:
stateInterfaceClass():currentState(nullptr){}
virtual ~stateInterfaceClass()=default;
virtual void setState(abstractState *stateNode)
{
currentState.reset(stateNode);
}
virtual void startProcess()
{
currentState->process(this);
}
private:
std::shared_ptr<abstractState> currentState; //using smart point because this method can delete class object automaticaly and it is suitable at this situation.
};
class concreteState_A : public abstractState
{
public:
virtual ~concreteState_A()=default;
virtual void process(stateInterfaceClass *interfaceNode);
};
class concreteState_B : public abstractState
{
public:
virtual ~concreteState_B()=default;
virtual void process(stateInterfaceClass *interfaceNode);
};
void concreteState_A::process(stateInterfaceClass *interfaceNode)
{
//task about state A
std::cout<<"state A process"<<std::endl;
abstractState *updateState = new concreteState_B();
interfaceNode->setState(updateState);
}
void concreteState_B::process(stateInterfaceClass *interfaceNode)
{
//task about state B
std::cout<<"state B process"<<std::endl;
abstractState *updateState = new concreteState_A();
interfaceNode->setState(updateState);
}
int main()
{
concreteState_A *state_A = new concreteState_A();
stateInterfaceClass *stateInterface = new stateInterfaceClass();
stateInterface->setState(state_A);
stateInterface->startProcess(); //print A
stateInterface->startProcess(); //print B
stateInterface->startProcess(); //print A
stateInterface->startProcess(); //print B
stateInterface->startProcess(); //print A
}
class abstractObSubject
{
public:
virtual ~abstractObSubject()=default;
virtual void respond()=0;
};
class observerObject
{
public:
observerObject() { observedSubject.clear();}
virtual ~observerObject()=default;
virtual void wakeSubject()
{
for(auto it : observedSubject)
{
it->respond();
}
}
virtual void addSubject(abstractObSubject *subjectNode)
{
observedSubject.emplace_back(subjectNode);
}
private:
std::list<abstractObSubject*> observedSubject;
};
class concreteObSubject_A : public abstractObSubject
{
public:
virtual ~concreteObSubject_A()=default;
virtual void respond()
{
//task about subject A
std::cout<<"observer subject A"<<std::endl;
}
};
class concreteObSubject_B : public abstractObSubject
{
public:
virtual ~concreteObSubject_B()=default;
virtual void respond()
{
//task about subject B
std::cout<<"observer subject B"<<std::endl;
}
};
int main()
{
observerObject *observer = new observerObject();
concreteObSubject_A *obSubject_A = new concreteObSubject_A();
concreteObSubject_B *obSubject_B = new concreteObSubject_B();
observer->addSubject(obSubject_A);
observer->addSubject(obSubject_B);
observer->wakeSubject();
}
class mediatorObject;
class abstractColleagueObject
{
public:
virtual ~abstractColleagueObject()=default;
virtual void registerMediator(mediatorObject *mediator)
{
givenMediator = mediator;
}
virtual void process()=0;
protected:
mediatorObject *givenMediator;
};
class concerteColleague_A : public abstractColleagueObject
{
public:
virtual ~concerteColleague_A()=default;
virtual void process();
};
class concerteColleague_B : public abstractColleagueObject
{
public:
virtual ~concerteColleague_B()=default;
virtual void process();
};
class mediatorObject
{
public:
virtual ~mediatorObject()=default;
virtual void deliverExcludeColleague(abstractColleagueObject *excludeColleague)
{
for(auto &it : colleagueList)
{
if(it != excludeColleague)
{
it->process();
}
}
}
virtual void addColleagueNode(abstractColleagueObject *colleagueNode)
{
colleagueList.emplace_back(colleagueNode);
colleagueNode->registerMediator(this);
}
protected:
std::list<abstractColleagueObject*> colleagueList;
};
void concerteColleague_A::process()
{
//task about colleague A type
std::cout<<"now colleague A process"<<std::endl;
givenMediator->deliverExcludeColleague(this);
}
void concerteColleague_B::process()
{
//task about colleague B type
std::cout<<"now colleague B process"<<std::endl;
givenMediator->deliverExcludeColleague(this);
}
int main()
{
mediatorObject *mediator = new mediatorObject();
concerteColleague_A *colleague_A = new concerteColleague_A();
concerteColleague_B *colleague_B = new concerteColleague_B();
mediator->addColleagueNode(colleague_A);
mediator->addColleagueNode(colleague_B);
colleague_A->process();
}
template<typename T>
class iteratorClass
{
public:
iteratorClass(T value): storeValue(value){}
virtual ~iteratorClass()=default;
virtual T headNode()=0;
virtual T nextNode()=0;
virtual bool end()=0;
virtual T currentNode()=0;
protected:
T storeValue;
};
template<typename T>
class externIteratorClass : public iteratorClass<T>
{
public:
externIteratorClass(T value) : iteratorClass<T>(value){};
virtual ~externIteratorClass()=default;
virtual T headNode()
{
//You can change any thing in there.Author me choose this easiest way to show the iterator theory.
return iteratorClass<T>::storeValue;
}
virtual T nextNode()
{
//You can change any thing in there.Author me choose this easiest way to show the iterator theory.
return iteratorClass<T>::storeValue;
}
virtual bool end()
{
return 0;
}
virtual T currentNode()
{
//You can change any thing in there.Author me choose this easiest way to show the iterator theory.
return iteratorClass<T>::storeValue;
}
};
template<typename T>
class iteratorManage
{
public:
virtual ~iteratorManage()=default;
iteratorClass<T>* generateIter(T value)
{
return new externIteratorClass<T>(value);
}
};
int main()
{
iteratorManage<int> p_iterManage;
auto *it = p_iterManage.generateIter(1);
std::cout<<"this is iterator value " <<it->currentNode()<<endl;
}
class abstractVisitor;
class abstractElement
{
public:
virtual ~abstractElement()=default;
virtual void accept(abstractVisitor *)=0;
public:
//all the data menber should display to visitor
};
class abstractVisitor
{
public:
virtual ~abstractVisitor()=default;
virtual void visitElement_A(abstractElement *element)=0;
virtual void visitElement_B(abstractElement *element)=0;
};
class concreteElement_A : public abstractElement
{
public:
virtual ~concreteElement_A()=default;
virtual void accept(abstractVisitor *visitor) override
{
visitor->visitElement_A(this);
}
};
class concreteElement_B : public abstractElement
{
public:
virtual ~concreteElement_B()=default;
virtual void accept(abstractVisitor *visitor) override
{
visitor->visitElement_B(this);
}
};
class concreteVisitor : public abstractVisitor
{
public:
virtual ~concreteVisitor()=default;
virtual void visitElement_A(abstractElement *element) override
{
//visitor class will handle all the content of element A
std::cout<<"now visit element A"<<std::endl;
}
virtual void visitElement_B(abstractElement *element) override
{
//visitor class will handle all the content of element B
std::cout<<"now visit element B"<<std::endl;
}
};
int main()
{
concreteVisitor *visitor = new concreteVisitor();
concreteElement_A *element_A = new concreteElement_A();
concreteElement_B *element_B = new concreteElement_B();
element_A->accept(visitor);
element_B->accept(visitor);
}
class memento
{
public:
memento(char *data):data(data){}
char* getData()
{
return data;
}
private:
char *data;
};
class mementoManager
{
public:
mementoManager():storedData(nullptr){ storedMemento.clear();}
void givenData(const char *data)
{
storedData = const_cast<char*>(data);
}
void modifyData(const char *data)
{
memento *createMemento = new memento(storedData);
storedMemento.emplace_front(createMemento);
storedData = const_cast<char*>(data);
}
void undoOperator()
{
std::list<memento*>::iterator tempMementoIter;
tempMementoIter = storedMemento.begin();
if(tempMementoIter != storedMemento.end())
{
storedData = (*tempMementoIter)->getData();
storedMemento.pop_front();
}
}
void showStoredData()
{
std::cout<<storedData<<std::endl;
}
private:
char* storedData;
std::list<memento*> storedMemento;
};
int main()
{
mementoManager *p_mementoManager = new mementoManager();
const char *storedData_A = "this is memento A";
const char *storedData_B = "this is memento B";
p_mementoManager->givenData(storedData_A);
p_mementoManager->showStoredData();
p_mementoManager->modifyData(storedData_B);
p_mementoManager->showStoredData();
p_mementoManager->undoOperator();
p_mementoManager->showStoredData();
}
//Interpreter is hard to interpret.
//expression patternt:(1)input: a=10 (2)input:b=20 (3)input: b+a (4)output: 30
class abstractSymbolOperator;
class abstractExpression
{
public:
virtual ~abstractExpression()=default;
virtual void translateExpression(const std::string givenString)=0;
virtual void bindSymbolOperator(abstractSymbolOperator *OperatorNode);
virtual void setProcessValue(std::string &symbol, int calculatedValue) //observer mathod
{
auto it = processedValue.find(symbol);
if(it != processedValue.end())
{
it->second = calculatedValue;
return;
}
processedValue.insert(std::make_pair(symbol, calculatedValue));
}
virtual int getProcessValue(std::string &symbol) //observer mathod
{
auto it = processedValue.find(symbol);
return it->second;
}
protected:
std::map<std::string, abstractSymbolOperator*> storedOperator;
std::map<std::string, int> processedValue;
std::string operatorSymbolString;
};
class terminalExpression : public abstractExpression
{
public:
virtual ~terminalExpression()=default;
virtual void translateExpression(std::string givenString);
};
class abstractSymbolOperator
{
public:
abstractSymbolOperator(const std::string &givenSymbol): givenOperatorSymbol(givenSymbol){}
virtual ~abstractSymbolOperator()=default;
virtual int stringProcess(const std::string &givenString, abstractExpression *expressionObject) = 0;
virtual std::string& getOperatorSymbol()
{
return givenOperatorSymbol;
}
protected:
std::string givenOperatorSymbol;
};
class add_symbolOperator : public abstractSymbolOperator
{
public:
add_symbolOperator(const std::string &givenSymbol): abstractSymbolOperator(givenSymbol){}
virtual ~add_symbolOperator()=default;
virtual int stringProcess(const std::string &givenString, abstractExpression *expressionObject)
{
int occurPos = givenString.find_first_of(givenOperatorSymbol);
if(occurPos != -1 && occurPos != 0)
{
std:: string prevSymbol(givenString.substr(0, occurPos));
std:: string backSymbol(givenString.substr(occurPos + 1, givenString.size() - occurPos - 1));
std::cout<<"the result "<< (expressionObject->getProcessValue(prevSymbol) + expressionObject->getProcessValue(backSymbol))<<std::endl;
}
return false;
}
};
class equal_symbolOperator : public abstractSymbolOperator
{
public:
equal_symbolOperator(const std::string &givenSymbol): abstractSymbolOperator(givenSymbol){}
virtual ~equal_symbolOperator()=default;
virtual int stringProcess(const std::string &givenValueName, abstractExpression *expressionObject) override
{
int occurPos = givenValueName.find_first_of(givenOperatorSymbol);
if(occurPos != -1 || occurPos != 0)
{
std:: string prevSymbol((givenValueName.substr(0, occurPos)));
std:: string backSymbol((givenValueName.substr(occurPos + 1, givenValueName.size() - occurPos - 1)));
int value = std::stoi(backSymbol);
expressionObject->setProcessValue(prevSymbol, value);
return true;
}
return false;
}
};
void terminalExpression::translateExpression(const std::string givenString)
{
int operatorPos = givenString.find_first_of(operatorSymbolString);
if(operatorPos != 0 && operatorPos != -1)
{
std::string objectString(givenString, operatorPos, 1);
auto iterator = storedOperator.find(objectString);
if(iterator != storedOperator.end())
{
iterator->second->stringProcess(givenString, this);
}
}
}
void abstractExpression::bindSymbolOperator(abstractSymbolOperator *OperatorNode)
{
std::cout<<"pan "<<OperatorNode->getOperatorSymbol().c_str()<<std::endl;
auto it = storedOperator.find(OperatorNode->getOperatorSymbol());
if(it == storedOperator.end())
{
storedOperator.insert(std::make_pair(OperatorNode->getOperatorSymbol(), OperatorNode));
operatorSymbolString.append(OperatorNode->getOperatorSymbol());
}
}
int main()
{
terminalExpression *p_expression = new terminalExpression();
add_symbolOperator *p_addOperator = new add_symbolOperator(std::string("+"));
equal_symbolOperator *p_equalOperator = new equal_symbolOperator(std::string("="));
p_expression->bindSymbolOperator(p_addOperator);
p_expression->bindSymbolOperator(p_equalOperator);
p_expression->translateExpression("a=1");
p_expression->translateExpression("a+a");
}