解释器模式
C++
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <regex>
#include <set>
using namespace std;
class Expression
{
public:
virtual bool Interpret(const std::string &info) = 0;
};
class TerminalExpressin : public Expression
{
private:
std::set<std::string> infos;
public:
TerminalExpressin(const std::vector<std::string> datas)
{
infos.insert(datas.begin(), datas.end());
}
bool Interpret(const std::string &info)
{
if (infos.find(info) != infos.end())
return true;
return false;
}
};
class AndExpression : public Expression
{
private:
std::shared_ptr<Expression> smartCity;
std::shared_ptr<Expression> smartPerson;
public:
AndExpression(std::shared_ptr<Expression> city, std::shared_ptr<Expression> person) : smartCity(city), smartPerson(person) {}
bool Interpret(const std::string &info)
{
std::regex pattern("的");
std::vector<std::string> results(std::sregex_token_iterator(info.begin(), info.end(), pattern, -1), std::sregex_token_iterator());
if (results.size() != 2)
{
std::cout << "输入解释信息有误,无法解析!" << std::endl;
return false;
}
return smartCity->Interpret(results[0]) && smartPerson->Interpret(results[1]);
}
};
class Context
{
private:
std::vector<std::string> citys;
std::vector<std::string> persons;
std::shared_ptr<Expression> smartAndExpr;
public:
Context()
{
citys.push_back("成都");
citys.push_back("临沂");
persons.push_back("老人");
persons.push_back("儿童");
smartAndExpr = std::make_shared<AndExpression>(std::make_shared<TerminalExpressin>(citys), std::make_shared<TerminalExpressin>(persons));
}
void IsFree(const std::string &info)
{
if (smartAndExpr->Interpret(info))
{
std::cout << info << " , 您本次乘车免费" << std::endl;
}
else
{
std::cout << info << ", 您本次乘车扣费2¥" << std::endl;
}
}
};
int main()
{
std::shared_ptr<Context> bus = std::make_shared<Context>();
std::vector<std::string> passengers = {"成都的老人", "成都的年轻人", "成都的儿童", "临沂的老人", "临沂的年轻人", "临沂的儿童"};
for (std::string passenger : passengers)
{
bus->IsFree(passenger);
}
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
typedef struct Expression Expression;
typedef struct TerminalExpression TerminalExpression;
typedef struct AndExpression AndExpression;
typedef struct Context Context;
struct Expression
{
bool (*interpret)(struct Expression *, const char *);
};
struct TerminalExpression
{
struct Expression base;
char **infos;
size_t num_infos;
};
struct AndExpression
{
struct Expression base;
struct Expression *smartCity;
struct Expression *smartPerson;
};
struct Context
{
struct Expression *smartAndExpr;
};
bool and_interpret(struct Expression *self, const char *info);
bool terminal_interpret(struct Expression *self, const char *info);
struct TerminalExpression *create_terminal_expression(char **datas, size_t num_datas)
{
struct TerminalExpression *expr = malloc(sizeof(struct TerminalExpression));
expr->base.interpret = terminal_interpret;
expr->infos = malloc(num_datas * sizeof(char *));
expr->num_infos = num_datas;
size_t i;
for (i = 0; i < num_datas; i++)
{
expr->infos[i] = strdup(datas[i]);
}
return expr;
}
struct AndExpression *create_and_expression(struct Expression *city, struct Expression *person)
{
struct AndExpression *expr = malloc(sizeof(struct AndExpression));
expr->base.interpret = and_interpret;
expr->smartCity = city;
expr->smartPerson = person;
return expr;
}
bool terminal_interpret(struct Expression *self, const char *info)
{
struct TerminalExpression *expr = (struct TerminalExpression *)self;
size_t i;
for (i = 0; i < expr->num_infos; i++)
{
if (strcmp(info, expr->infos[i]) == 0)
{
return true;
}
}
return false;
}
bool and_interpret(struct Expression *self, const char *info)
{
struct AndExpression *expr = (struct AndExpression *)self;
char *buffer = strdup(info);
char *token = strtok((char *)buffer, "的");
bool cityMatched = false;
bool personMatched = false;
while (token != NULL)
{
if (!cityMatched && expr->smartCity->interpret(expr->smartCity, token))
{
cityMatched = true;
}
else if (!personMatched && expr->smartPerson->interpret(expr->smartPerson, token))
{
personMatched = true;
}
else
{
if (buffer)
free(buffer);
return false;
}
token = strtok(NULL, "的");
}
if (buffer)
free(buffer);
return cityMatched && personMatched;
}
struct Context *create_context()
{
struct Context *ctx = malloc(sizeof(struct Context));
char *citys[] = {"成都", "临沂"};
char *persons[] = {"老人", "儿童"};
struct TerminalExpression *cityExpr = create_terminal_expression(citys, sizeof(citys) / sizeof(citys[0]));
struct TerminalExpression *personExpr = create_terminal_expression(persons, sizeof(persons) / sizeof(persons[0]));
struct AndExpression *andExpr = create_and_expression(&cityExpr->base, &personExpr->base);
ctx->smartAndExpr = &andExpr->base;
return ctx;
}
void context_is_free(struct Context *ctx, const char *info)
{
if (ctx->smartAndExpr->interpret(ctx->smartAndExpr, info))
{
printf("%s , 您本次乘车免费\n", info);
}
else
{
printf("%s , 您本次乘车扣费2¥\n", info);
}
}
void destroy_terminal_expression(struct TerminalExpression *expr)
{
size_t i;
for (i = 0; i < expr->num_infos; i++)
{
free(expr->infos[i]);
}
free(expr->infos);
free(expr);
}
void destroy_and_expression(struct AndExpression *expr)
{
destroy_terminal_expression((struct TerminalExpression *)expr->smartCity);
destroy_terminal_expression((struct TerminalExpression *)expr->smartPerson);
free(expr);
}
void destroy_context(struct Context *ctx)
{
struct AndExpression *andExpr = (struct AndExpression *)ctx->smartAndExpr;
destroy_and_expression(andExpr);
free(ctx);
}
int main()
{
struct Context *bus = create_context();
char *passengers[] = {"成都的老人", "成都的年轻人", "成都的儿童", "临沂的老人", "临沂的年轻人", "临沂的儿童"};
size_t i;
for (i = 0; i < sizeof(passengers) / sizeof(passengers[0]); i++)
{
context_is_free(bus, passengers[i]);
}
destroy_context(bus);
return 0;
}