设计模式问题 [英] Design pattern question

查看:52
本文介绍了设计模式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在努力解决以下困扰我的问题。

我有一个具体的课程,Parser说,提供一些基本的解析

功能(比如逐字逐行阅读等)

然后我有第二个Parser派生类,比如说HtmlParser,<除了提供解析器功能外,还提供额外的功能,例如按标签读取标签,属性等等。


什么我想要实现的是工厂。根据收到的内容类型,它会返回正确的

类,所以我称之为工厂

,如factory.create(" html");等等


我面临的问题是派生的解析器将包含更多

方法,这些方法在基类中不可用。

那么如何在不依赖if / else /

切换结构的情况下实现通用行为?到目前为止,我看到的唯一方法是动态投射工厂返回的

解析器指针,但我觉得这打败了工厂的整个目的



另一个明显的方法是将所有方法放在基础

类中,但其中一些没有任何意义..比如阅读

来自通用文本的标签..


任何提示,我可以找到一些指针?谢谢。

Hello,

I''m trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I''d like to achieve is a "factory" which would return the proper
class depending on the content type received, so I''d call this factory
like factory.create("html");, etc

The problem I''m facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn''t have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.

推荐答案

al * *****@gmail.com 写道:
al******@gmail.com wrote:

我正在努力解决以下困扰我的问题。

我有一个具体的类,比如Parser,它提供了一些基本的解析

功能(比如逐字逐行读,等等)

然后我有第二个Parser派生类,比如HtmlParser,除了提供Parser功能外,它还提供额外的功能,

,例如按标签读取标签,属性等。 。


我想要达到的是工厂。根据收到的内容类型,它会返回正确的

类,所以我称之为工厂

,如factory.create(" html");等等


我面临的问题是派生的解析器将包含更多

方法,这些方法在基类中不可用。

那么如何在不依赖if / else /

切换结构的情况下实现通用行为?到目前为止,我看到的唯一方法是动态投射工厂返回的

解析器指针,但我觉得这打败了工厂的整个目的



另一个明显的方法是将所有方法放在基础

类中,但其中一些没有任何意义..比如阅读

来自通用文本的标签..


任何提示,我可以找到一些指针?谢谢。
I''m trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I''d like to achieve is a "factory" which would return the proper
class depending on the content type received, so I''d call this factory
like factory.create("html");, etc

The problem I''m facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn''t have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.



首先编写使用解析器的类。以这样的方式写它

无论文本的格式是什么它都会起作用。然后你的问题

将被解决。


更抽象地思考。

Write the class that uses the parser first. Write it in such a way that
it will work no matter what format the text is in. Then your problem
will be solved.

Think more abstractly.


al ****** @ gmail.com 写道:

您好,


我正在努力解决以下问题令我困惑的是。

我有一个具体的类,比如Parser,它提供了一些基本的解析

功能(比如逐字逐行读,等等)

然后我有第二个Parser派生类,比如说HtmlParser,除了提供Parser功能之外还有
,提供额外的功能,

如按标签,属性等读取标签。


我想要实现的是工厂。根据收到的内容类型,它会返回正确的

类,所以我称之为工厂

,如factory.create(" html");等等


我面临的问题是派生的解析器将包含更多

方法,这些方法在基类中不可用。

那么如何在不依赖if / else /

切换结构的情况下实现通用行为?到目前为止,我看到的唯一方法是动态投射工厂返回的

解析器指针,但我觉得这打败了工厂的整个目的



另一个明显的方法是将所有方法放在基础

类中,但其中一些没有任何意义..比如阅读

来自通用文本的标签..


任何提示,我可以找到一些指针?谢谢。
Hello,

I''m trying to solve the following issue which is puzzling me.
I have a concrete class, say Parser, which provides some basic parsing
functionality (such as reading word by word, line by line, etc)
Then I have a second Parser-derived class, say HtmlParser, which
besides providing the Parser functionality, provides extra features,
such as read tag by tag, attributes, etc..

What I''d like to achieve is a "factory" which would return the proper
class depending on the content type received, so I''d call this factory
like factory.create("html");, etc

The problem I''m facing is that the derived parser will contain more
methods which are not available in the base class.
So how can I achieve a generic behavior without relying on if/else/
switch constructs? So far the only way I see is dynamic casting the
Parser pointer returned by the factory, but I feel this defeats the
whole purpose of a factory.
The other obvious approach would be putting all methods in the base
class, but some of them wouldn''t have any sense.. such as reading a
tag from a generic text..

Any hint to where I can find some pointers? Thank you.



好​​了这里有几个问题:

1)根据你的描述,你正在编写一个词法分析器(扫描仪)

和tokenizer),不要将解析器与词法分析器混淆。从这里开始


http://en.wikipedia.org/wiki/Semanti...ter_science%29

确切了解您的需求并定义您的问题。你想要一个词法分析器

还是一个解析器?


2)当并行层次结构存在时,因子方法效果最好,你可以

将您的层次结构表示为词法分析器层次结构和令牌层次结构。

然后lexer类可以通过协方差创建一个令牌类和

虚拟方法:

class lexer {

public:

虚拟标记* create_token(const attr& at){

返回新标记(at);

}

virtual~lexer(){};

};

class html:virtual public lexer {

public:

html_token * create_token(const attr& at){

返回新的html_token(at);

}

}

};


班级代币{

public:

令牌(const attr& at){//构造一个令牌

}

};

class html_token:virtual公共令牌{

pubilc:

html_token(const attr& at){//构建一个html toke n $ / b $ b}

};

3)if / else / switch构造没有任何问题。事实上,IMO

是唯一可以在领域内动态初始化对象的方法

的C ++。您可以使用特定于平台的功能,即动态库和

名称解析,以促进更通用的解决方案,但这不会与C ++有任何关系。

Ok there are a couple issues revealed here:
1) From your description, you are writing a lexical analyzer (scanner
and tokenizer), do not confuse a parser with a lexical analyzer. Start
from here:
http://en.wikipedia.org/wiki/Semanti...ter_science%29
Know exactly what you want and define your problem. Do you want a lexer
or a parser?

2) A factor method works best when parallel hierarchies exists, you can
formulate your hierarchies into a lexer hierarchy and a token hierarchy.
Then a lexer class can create a token class through covariance and
virtual methods:
class lexer{
public:
virtual token * create_token(const attr & at){
return new token(at);
}
virtual ~lexer(){};
};
class html : virtual public lexer {
public:
html_token * create_token(const attr & at){
return new html_token(at);
}
}
};

class token {
public:
token(const attr & at){ // construct a token
}
};
class html_token : virtual public token{
pubilc:
html_token(const attr & at){ // construct a html token
}
};
3) There is nothing wrong with if/else/switch constructs. In fact IMO
that''s only way to can initialize objects dynamically within the realm
of C++. You can use platform specific feature, i.e. dynamic library and
name resolution, to facilitate a more generic solution but that does not
have anything to do with C++.


非常感谢你的回答..
Hi, thanks a lot for your answer..

好​​的,这里有几个问题:

1)从您的描述中,您正在编写词法分析器(扫描仪

和tokenizer),不要将解析器与词法分析器混淆。从这里开始

http:// en.wikipedia.org/wiki/Semanti...ter_science%29

确切了解您的需求并定义您的问题。你想要一个词法分析器

还是一个解析器?
Ok there are a couple issues revealed here:
1) From your description, you are writing a lexical analyzer (scanner
and tokenizer), do not confuse a parser with a lexical analyzer. Start
from here:http://en.wikipedia.org/wiki/Semanti...ter_science%29
Know exactly what you want and define your problem. Do you want a lexer
or a parser?



是的,谢谢你让我知道。我对这些过程的分离没有足够的了解。

实际上,似乎我需要它们两个......


这个想法基本上能够解释一个http头,还有另外一些协议,比如说rtsp举个例子。

两个都有相似之处,但那里有是每个

协议特有的东西。

如果我说在大多数情况下分析和解析是在b / b中完成的,我错了同一个地方?


我的想法就是能够获得一个数据流,然后构建一个包含不同标题的

数据结构,

及其字段和属性..从我的想法来看,我不认为我需要制作一个lex分析器,然后是一个解析器..我能做到

obth当时的东西?

Yes, thanks for letting me know. I didn''t have enough knowledge on the
separation of these processes.
Actually, it seems I need both of them..

The idea is basically being able to interpret a http header, and also
other protocols, say rtsp to put an example.
Both have similarities, but there are some things specific to each
protocol.
Am I wrong if I say that in most cases the analyzing and parsing is
done in the same place?

My idea is simply being able to get a stream of data and then build a
data structure holding the different headers,
with its fields and attributes.. From what I think, I don''t think I
need to make a lex analyzer and then a parser.. I could do
obth things at the time?


2)当并行层次结构存在时,因子方法效果最好,你可以用b $ b来表示你的层次结构成词法分析器层次结构和令牌层次结构。

然后lexer类可以通过协方差创建一个令牌类和

虚拟方法:

class lexer {

public:

虚拟标记* create_token(const attr& at){

返回新令牌(at);

}

virtual~lexer(){};};


class html:虚拟公共词法分析器{

public:

html_token * create_token(const attr& at){

返回新的html_token(at);

}

}


};


class token {

public:

token(const attr& at){//构造一个令牌

}};


class html_token:虚拟公共令牌{

pubilc:

html_token(const attr& at){//构造一个html令牌

}};


3)if / else / switch构造没有任何问题。事实上,IMO

是唯一可以在领域内动态初始化对象的方法

的C ++。您可以使用特定于平台的功能,即动态库和

名称解析,以促进更通用的解决方案,但这不会与C ++有任何关系。
2) A factor method works best when parallel hierarchies exists, you can
formulate your hierarchies into a lexer hierarchy and a token hierarchy.
Then a lexer class can create a token class through covariance and
virtual methods:
class lexer{
public:
virtual token * create_token(const attr & at){
return new token(at);
}
virtual ~lexer(){};};

class html : virtual public lexer {
public:
html_token * create_token(const attr & at){
return new html_token(at);
}
}

};

class token {
public:
token(const attr & at){ // construct a token
}};

class html_token : virtual public token{
pubilc:
html_token(const attr & at){ // construct a html token
}};

3) There is nothing wrong with if/else/switch constructs. In fact IMO
that''s only way to can initialize objects dynamically within the realm
of C++. You can use platform specific feature, i.e. dynamic library and
name resolution, to facilitate a more generic solution but that does not
have anything to do with C++.



谢谢。我绝对需要更多地考虑这个问题,或者学习现有的

示例,看看他们是如何做到的。

Thanks. I definetly need to give this more thought, or learn existing
examples to see how they do this.


这篇关于设计模式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆