Design Pattern Interpreter 解析者模式

解析者本身是一个很大的设计模式,重点在于设计这个解析者本身,但是由于解析者本身很难设计,故此完善的解析者模式比较少应用,但是这个设计模式本身的思想却不难。

下面简单实用C++实现一下解析者模式,使用不同的解析者,那么就会对于同样的内容解析出不同的结果。


#include <stdio.h>
#include <string>
using namespace std;

class Context
{
public:
	string cmd;
	void setContext(string s)
	{
		cmd = s;
	}
};


class InterpreterBase
{
protected:
	Context *context;
public:
	InterpreterBase(Context *c) : context(c) {}
	//不在后面写=0会出现无法解析外部命令错误的virtual void interpret();
	virtual void interpret() = 0;
};

class InterpreterIncre:public InterpreterBase
{
public:
	InterpreterIncre(Context *c) : InterpreterBase(c) {}
	void interpret()
	{
		string cmd = context->cmd;
		for (int i = 0; i < (int)cmd.size(); i++)
		{
			putchar(cmd[i]+1);
		}
		putchar('\n');
	}
};

class InterpreterDecre:public InterpreterBase
{
public:
	InterpreterDecre(Context *c) : InterpreterBase(c) {}
	void interpret()
	{
		string cmd = context->cmd;
		for (int i = 0; i < (int)cmd.size(); i++)
		{
			putchar(cmd[i]-1);
		}
		putchar('\n');
	}
};

int main()
{
	Context context;
	context.setContext("ABCDEFG");
	InterpreterDecre decre(&context);
	InterpreterIncre incre(&context);

	decre.interpret();
	incre.interpret();

	return 0;
}

运行结果对同一个字符串ABCDEFG,使用不同的解析者得到不同的结果:



可以说C\C++编译器就是利用这个设计模式写出来的。完善实现这样的解析器当然是很难的。


Mastering Python Design Patterns: A guide to creating smart, efficient, and reusable software, 2nd Edition Exploit various design patterns to master the art of solving problems using Python Python is an object-oriented scripting language that is used in a wide range of categories. In software engineering, a design pattern is an elected solution for solving software design problems. Although they have been around for a while, design patterns remain one of the top topics in software engineering, and are a ready source for software developers to solve the problems they face on a regular basis. This book takes you through a variety of design patterns and explains them with real-world examples. You will get to grips with low-level details and concepts that show you how to write Python code, without focusing on common solutions as enabled in Java and C++. You’ll also fnd sections on corrections, best practices, system architecture, and its designing aspects. This book will help you learn the core concepts of design patterns and the way they can be used to resolve software design problems. You’ll focus on most of the Gang of Four (GoF) design patterns, which are used to solve everyday problems, and take your skills to the next level with reactive and functional patterns that help you build resilient, scalable, and robust applications. By the end of the book, you’ll be able to effciently address commonly faced problems and develop applications, and also be comfortable working on scalable and maintainable projects of any size.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值