टेम्पलेट पैटर्न क्या है?
💻 coding

टेम्पलेट पैटर्न क्या है?

2 min read 418 words
2 min read
ShareWhatsAppPost on X
  • 1The template method pattern defines an algorithm's skeleton, allowing subclasses to redefine specific steps without altering the overall structure.
  • 2It consists of a base class with shared code and abstract methods that subclasses implement, providing flexibility in algorithm execution.
  • 3This pattern exemplifies inversion of control, where high-level code does not dictate algorithms, allowing for dynamic selection at run-time.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The template method pattern defines an algorithm's skeleton, allowing subclasses to redefine specific steps without altering the overall structure."

टेम्पलेट पैटर्न क्या है?

In software engineering, the template method pattern is a behavioural design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. The template method is one of the twenty-three well-known patterns described in the "Gang of Four" book Design Patterns.

This pattern has two main parts, and typically uses object-oriented programming:

The "template method", generally implemented as a base class (possibly an abstract class), which contains shared code and parts of the overall algorithm which is invariant. The template ensures that the overarching algorithm is always followed. In this class, "variant" portions are given a default implementation or none at all.

Concrete implementations of the abstract class, which fill in the empty or "variant" parts of the "template" with specific algorithms that vary from implementation to implementation.

At run-time, a concrete class is instantiated. The main method inherited from the base class is called, which they may call other methods defined by both the base class and subclasses. This performs the overall algorithm in the same steps every time, but the details of some steps depend on which subclass was instantiated.

This pattern is an example of inversion of control because the high-level code no longer determines what algorithms to run; a lower-level algorithm is instead selected at run-time.

The template method pattern thus manages the larger picture of task semantics and more refined implementation details of selection and sequence of methods. This larger picture calls abstract and non-abstract methods for the task at hand. The non-abstract methods are completely controlled by the template method, but the abstract methods, implemented in subclasses, provide the pattern's expressive power and degree of freedom. Template method's abstract class may also define hook methods that may be overridden by subclasses. These have a no-op implementation in the abstract class but provide a "hook" on which to "hang" implementations.

The template method pattern occurs frequently, at least in its simplest case, where a method calls only one abstract method when using object-oriented languages. If a software writer uses a polymorphic method at all, this design pattern may be a rather natural consequence. This is because a method of calling an abstract or polymorphic function is simply the reason for being of the abstract or polymorphic method. The template method pattern may be used to add immediate present value to the software or with a vision for enhancements in the future.

source: wiki

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 1 August 2018 · 2 min read · 418 words

Part of AskGif Blog · coding

You might also like

टेम्पलेट पैटर्न क्या है? | AskGif Blog