Skip to content

Commit 7db3a67

Browse files
committed
Template method definition and applicability updated
1 parent 9d63242 commit 7db3a67

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ Covert the interface of a class into another interface clients expects. Adapter
2121
* You want to use and existing class, and its interface does not match the one your need.
2222
* You want to create a reusable class the cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
2323
* You need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class. (object adapter only)
24-
24+
25+
#### 3.Template Method
26+
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. [example](https://github.com/hbrawnak/php-design-pattern/blob/master/TemplateMethod/index.php#L4)
27+
28+
##### Applicability:
29+
* To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.
30+
* When the commin behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a good example of "refactoring to generalize" as described by Opdyke and Johnson. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one these new operations.
31+
* To control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points.
32+
33+

TemplateMethod/index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
/*
4+
* Template methods are a fundamental technique for code reuse. They are particularly important in
5+
* class libraries, because they are the means for factoring out common behavior in library classes.
6+
* */
7+
38
use Template\Bike;
49
use Template\Car;
510

0 commit comments

Comments
 (0)