How to use the Template Method design pattern in PHP for reusable code?

Member

by addison , in category: PHP General , 2 years ago

How to use the Template Method design pattern in PHP for reusable code?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by cortez.connelly , 2 years ago

@addison The Template Method design pattern is a behavioral pattern that defines the skeleton of an algorithm in a base class but delegates some of the steps to its subclasses. The pattern allows for reuse of code by providing a common template that can be modified in specific ways by each subclass. In PHP, you can implement the Template Method pattern in the following way:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Create an abstract base class that defines 
// the common template for the algorithm:
abstract class AbstractTemplate {
    public function templateMethod() {
        $this->operation1();
        $this->operation2();
        $this->operation3();
    }
    
    public abstract function operation1();
    public abstract function operation2();
    public abstract function operation3();
}

// Create concrete subclasses that implement the abstract methods 
// to customize the algorithm:
class ConcreteClass1 extends AbstractTemplate {
    public function operation1() {
        // implementation specific to ConcreteClass1
    }
    
    public function operation2() {
        // implementation specific to ConcreteClass1
    }
    
    public function operation3() {
        // implementation specific to ConcreteClass1
    }
}

class ConcreteClass2 extends AbstractTemplate {
    public function operation1() {
        // implementation specific to ConcreteClass2
    }
    
    public function operation2() {
        // implementation specific to ConcreteClass2
    }
    
    public function operation3() {
        // implementation specific to ConcreteClass2
    }
}

// Use the template method by calling it on an instance of the concrete subclass:
$object = new ConcreteClass1();
$object->templateMethod();

$object = new ConcreteClass2();
$object->templateMethod();


The Template Method pattern can be useful in situations where you have a common algorithm that is used across multiple classes but needs to be customized in specific ways for each class. By defining the algorithm in a base class and allowing subclasses to implement specific steps, you can avoid code duplication and make your code more reusable.

Member

by darion , 2 years ago

@addison 

The Template Method design pattern is a behavioral pattern that allows you to define a skeleton of an algorithm in a base class and let the subclasses implement the details without changing the overall structure. This pattern is useful for creating reusable code because it helps to avoid code duplication and makes it easy to maintain and update the code.


Here's an example of how to use the Template Method design pattern in PHP for reusable code:

  1. Create an abstract base class that defines the skeleton of the algorithm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
abstract class BaseAlgorithm {
    public function execute() {
        $this->step1();
        $this->step2();
        $this->step3();
    }
    abstract protected function step1();
    abstract protected function step2();
    abstract protected function step3();
}


  1. Create subclasses that implement the details of the algorithm:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ConcreteAlgorithmA extends BaseAlgorithm {
    protected function step1() {
        // implementation for step 1 of algorithm A
    }
    protected function step2() {
        // implementation for step 2 of algorithm A
    }
    protected function step3() {
        // implementation for step 3 of algorithm A
    }
}

class ConcreteAlgorithmB extends BaseAlgorithm {
    protected function step1() {
        // implementation for step 1 of algorithm B
    }
    protected function step2() {
        // implementation for step 2 of algorithm B
    }
    protected function step3() {
        // implementation for step 3 of algorithm B
    }
}


  1. Use the subclasses to execute the algorithm:
1
2
3
4
5
$algorithmA = new ConcreteAlgorithmA();
$algorithmA->execute();

$algorithmB = new ConcreteAlgorithmB();
$algorithmB->execute();


In this example, the BaseAlgorithm class defines the skeleton of the algorithm by providing the execute() method that calls the abstract methods step1(), step2(), and step3(). The ConcreteAlgorithmA and ConcreteAlgorithmB classes implement the details of the algorithm by providing their own implementation for these abstract methods.


By using the Template Method design pattern, you can create reusable code that defines the structure of an algorithm once and allows you to provide different implementations of the details in subclasses. This can help you to reduce code duplication and make your code more maintainable and extensible.