chain-of-responsibility

What is the difference between Chain Of Responsibility design pattern and using a simple if-elseif-else block?

大兔子大兔子 提交于 2019-11-30 13:48:31
I was just looking up Chain Of Responsibility the other day, and I came across this example. Basically, there is an abstract handler, and then are concrete handlers, each of which implement the handle method of the parent abstract handler. The implementation is such that at first there is a check to see if this particular handler can process the current request, and if not, then it passes on the request to its successor. Now, I could also do the same thing using a simple if-else conditional block. To take the first example from the link above, here is how I would change it: class SingleHandler

What is the difference between Chain Of Responsibility design pattern and using a simple if-elseif-else block?

假如想象 提交于 2019-11-29 19:14:15
问题 I was just looking up Chain Of Responsibility the other day, and I came across this example. Basically, there is an abstract handler, and then are concrete handlers, each of which implement the handle method of the parent abstract handler. The implementation is such that at first there is a check to see if this particular handler can process the current request, and if not, then it passes on the request to its successor. Now, I could also do the same thing using a simple if-else conditional

Why would I ever use a Chain of Responsibility over a Decorator?

我只是一个虾纸丫 提交于 2019-11-28 03:38:30
I'm just reading up on the Chain of Responsibility pattern and I'm having trouble imagining a scenario when I would prefer its use over that of decorator . What do you think? Does CoR have a niche use? The fact that you can break the chain at any point differentiates the Chain of Responsibility pattern from the Decorator pattern . Decorators can be thought of as executing all at once without any interaction with the other decorators. Links in a chain can be thought of as executing one at a time, because they each depend on the previous link. Use the Chain of Responsibility pattern when you can

Why would I ever use a Chain of Responsibility over a Decorator?

穿精又带淫゛_ 提交于 2019-11-27 19:15:42
问题 I'm just reading up on the Chain of Responsibility pattern and I'm having trouble imagining a scenario when I would prefer its use over that of decorator. What do you think? Does CoR have a niche use? 回答1: The fact that you can break the chain at any point differentiates the Chain of Responsibility pattern from the Decorator pattern . Decorators can be thought of as executing all at once without any interaction with the other decorators. Links in a chain can be thought of as executing one at

Replace giant switch statement with what?

巧了我就是萌 提交于 2019-11-27 15:52:51
问题 I have a code that parses some template files and when it finds a placeholder, it replaces it with a value. Something like: <html> <head> <title>%title%</title> </head> <body bgcolor="%color%"> ...etc. In code, the parser finds those, calls this function: string getContent(const string& name) { if (name == "title") return page->getTitle(); else if (name == "color") return getBodyColor(); ...etc. } and then replaces the original placeholder with returned value. In real case, it is not a dummy

For a large validation task is chain of responsibility pattern a good bet?

会有一股神秘感。 提交于 2019-11-27 14:00:09
问题 I need to build a process which will validate a record against ~200 validation rules. A record can be one of ~10 types. There is some segmentation from validation rules to record types but there exists a lot of overlap which prevents me from cleanly binning the validation rules. During my design I'm considering a chain of responsibility pattern for all of the validation rules. Is this a good idea or is there a better design pattern? 回答1: Validation is frequently a Composite pattern. When you

Avoiding instanceof in Java

纵然是瞬间 提交于 2019-11-26 03:17:36
Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous situation would be with the Java classes Integer, Double, BigDecimal etc. if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);} else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);} else if (obj instanceof Double) {DoubleStuff.handle((Double)obj);} I do have control over NumberStuff and so on. I don't want to use many

Avoiding instanceof in Java

北城以北 提交于 2019-11-26 01:13:12
问题 Having a chain of \"instanceof\" operations is considered a \"code smell\". The standard answer is \"use polymorphism\". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous situation would be with the Java classes Integer, Double, BigDecimal etc. if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);} else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);} else if (obj instanceof