开发者

Are some design patterns language dependent?

开发者 https://www.devze.com 2023-04-03 05:14 出处:网络
If yes, then are there any patterns that are specific to C++, and some that are not? Wh开发者_C百科ich are they, if any? Well I am referring to these design patterns: http://en.wikipedia.org/wiki/Desi

If yes, then are there any patterns that are specific to C++, and some that are not? Wh开发者_C百科ich are they, if any? Well I am referring to these design patterns: http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29


RAII is a powerful pattern that relies on C++'s scope cleanup mechanism. In garbage-collected environments this language feature is usually missing. Other languages (e.g. Ruby) have other ways of using user-provided control flow constructions.


I'm not sure if there is another answer other than "Yes".

If the design pattern depends on language-agnostic object behavior, the same DP can be applied across languages.

However if the design pattern is dependent on some language features, then translating it to another language would require some effort or be impossible. Some patterns might come into existence because of some characteristics of a specific language (but are not needed in other languages).

People come up with patterns all the time. So yes.


I think the best answer here is "it depends" because the notion of pattern has some fuzzy boundaries.

If you are willing to say "C++ virtual function" or "Java interface" is a "pattern" then, well, you have by default come up with a "pattern" that is applicable to exactly one language at the exclusion of all others. Given that a pattern is simply a way of solving a problem that can be applied in varying contexts, you could argue technically that single-language patters can in theory exist.

However the common use of the term "pattern" is something slightly more abstract than a specific language feature. What I think most people can agree on is that there are patterns that are specific to certain families of languages, where the family can be very small, e.g., languages supporting OO, supporting actors, supporting channel-based communication, etc.

When your pattern becomes so specific that the language family to which it applies winds all the way down to a single language, you get into the fuzzy area where people might quibble about whether something is a pattern or a language feature. The rationale here is that "well heck, can't someone just create another language based on this language so that the pattern will apply to the derived language too?"

However, if you can reasonably argue that your alleged pattern is indeed a way of solving a problem that can be used in different contexts, and that no other programming language in existence can reasonably implement this solution, then you can say yes.

.... Until someone creates that dialect! :-)


Yes.

The most notable way to see this is to experiment with a paradigm shift. For example, compare Java (OO) to Haskell (Functional).

Let's take the Flyweight pattern, in Java. In Haskell, it's a no-brainer, data (which is immutable) is implicitly shared.

Other example: the Command pattern. Haskell supports first-class functions (and curryism), commands are built-in.

Some will argue that DP exist to cover what a language does not provide in a built-in way. In this sense, the higher level a language, the more DP are embedded.


The patterns which uses the specific language functions like Multiple Inhertance in C++, Dynamic behaviors like Reflections are quite language dependent. GoF design patterns are considered as the base for most of the patterns but as the years went, systems became more complex and distributed. Hence the fundamental design patterns and principles were evolved and extended. Web frameworks and libraries are one of the best examples for implementing real-world scenarios to meet the developers/users need. Like dynamic page generation, AJAX etc. But I seriously believe that, the patterns must be generic and should not rely on languages.

For e.g if you're is using too much patterns which depends on PHP, may not help them to switch to some other dynamic languages like Python. The general/generic implementations can be realized easily across most of the languages.

0

精彩评论

暂无评论...
验证码 换一张
取 消