Home c++ What is the difference between templates, patterns and generics?

What is the difference between templates, patterns and generics?

Author

Date

Category

What is the difference between templates, patterns and generic? Are all three concepts are the same in meaning and the difference in the translation? Or the difference is how to implement the same in different languages ​​within the meaning of the functional type of List?

In particular, the question arose while reading comparisons of programming languages ​​and frequent thesis “In C++, there are templates and anywhere else there is no such”


Answer 1, Authority 100%

Templates (templates) in C++ is a special case of generalized types (generic types). Generalized types in C++ is very powerful, Turing-complete. For example, you can calculate the Fibonacci numbers at compile time using C++ templates.

In other languages ​​(Java, C #) generalized types much easier. Accordingly, the easier and the compiler and catching errors difficult to understand the strange (see. Brain Alexandrescu ).

Generalized types – a fairly old concept invented one of the first implementations of ML. It avoids duplication of code that is independent of a particular type, or superimposed on the type is not very restrictive.

For example, a generic algorithm, considering the arithmetic mean of the collection facilities imposed on the type of elements are two limitations: the element can be combined with the element and the element can be divided into an integer (a type of restrictions implemented not all programming languages)

.

The generalized data structure “list” does not impose on the type of elements to any restrictions except those that are already inherent in any object. In older versions of languages ​​such as C++, Java, C # without the support of the generalized type, I had to write a separate implementation of the list for each hierarchy, either permanently lead type Up and Down .

As for patterns (patterns) it is usually under them understand the larger units design of object-oriented programs. For example, such solutions access to databases as repository , unit of work , active record are patterns (which are described, inter alia, in Fowler’s books). DDD describes patterns of aggregate root and value-object , Dependency Injection is realized with the help of patterns composite root , constructor injection and etc.

There are also main book on patterns , which is called – patterns of object-oriented design

.


Answer 2, AUTHORITY 60%

Each area of ​​information technology or programming language has its own terms, the semantics of which are defined in the relevant regulations, such as in the respective standards.

For example, in the C++ programming language used templates term – templates . In programming languages, such as, for example, C #, or Eiffel, the term dzheneriks – generics

.

The exact definition and behavior depends on the order in which programming language or technology are used.

The meaning of pattern also depends on the technology or the context in which it is used. For example, there are problems of design patterns, or patterns of the search substring, etc.

If you’re talking about in the context of any technology or programming language, you should use the correct term, as they are defined in a particular technology or programming language.

On the other hand, these terms may be used in a broader sense, regardless of the specific technology or programming language.

That is, these terms may be used in a narrow specialized value, determined in accordance with a specific technology or programming language, and in a broader sense, derived from the context.

As for your example, with a list, then there are more suitable terms interface and implementation .

and first, and the second can differ from the description to the description, or from the project to the project. There are some general concepts that allow you to classify lists, such as one-connected lists, double-link lists, ring lists, etc. Even for the same class of lists, a different interface can be defined and different implementations. For example, in a standard single-connected list in C++ STD :: FORWARD_LIST there is no such list property as the number of items in the list. In other programming languages, a single-sensitive list may not have those properties that are defined for a single-connected list in C++.
In addition, lists can be implemented as template or generic lists.
As you can see, everything is resting again to accurate definition and more generalized definitions depending on the context.


Answer 3, Authority 20%

These concepts have a bunch of different meanings.

Perhaps the generics the most unambiguous thing is exactly what is described in comparison of generics in C # with C++ templates . It is just the ability to apply the algorithm to something in a generalized form.

Template can mean template or pattern. In the case of template, it may be C++ templates or simply a certain layout of anything. That is, this is where you can substitute something and get a specific implementation specifically for substituted values.

Patterns are Pattern, but the meanings are still somewhat.

Design patterns (or design templates) are high-level ways to implement the interaction between components. Not quite right, but the general meaning reflects. For more detailed, you can look at Wikipedia.

Pattern may designate a sample of something. Pattern Matching – mapping with sample. Many languages ​​are introduced to the object decomposition or compliance check in this way. For example, in ES6, the possibility of decomposing an object is entered in this way: var {x, y: z} = obj; – now two variables X and z , and the assignment of X = OBJ.X, Y = OBJ.Z . Comable example – exchange of values ​​in Python. Considered adding to C #.

In essence, all these concepts (in addition to the design patterns that are even higher-level) refer to metaprogramming, which is very trying to add to modern languages.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions