Skip to content

Commit

Permalink
Add Template Method
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusml authored and ochococo committed Aug 25, 2020
1 parent 10114a6 commit c0daff5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
Binary file modified Design-Patterns.playground.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,43 @@ let isRachelAndroid = deckard.testIfAndroid(rachel)
let gaff = BladeRunner(test: GeneticTest())
let isDeckardAndroid = gaff.testIfAndroid(rachel)
/*:
📝 Template Method
-----------

The template method patter defines the steps of an algorithm and allows the redefinition of one or more of these steps. In this way, the template method protects the algorithm, the order of execution and provides abstract methods that can be implemented by concrete types.

### Example
*/
protocol Garden {
func prepareSoil()
func plantSeeds()
func waterPlants()
func prepareGarden()
}

extension Garden {

func prepareGarden() {
func prepareSoil()
func plantSeeds()
func waterPlants()
}
}

final class RoseGarden: Garden {

func prepare() {
prepareGarden()
}
}

/*:
### Usage
*/

let roseGarden = RoseGarden()
roseGarden.prepare()
/*:
🏃 Visitor
----------

Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,47 @@ let gaff = BladeRunner(test: GeneticTest())
let isDeckardAndroid = gaff.testIfAndroid(rachel)
```

📝 Template Method
-----------

The template method patter defines the steps of an algorithm and allows the redefinition of one or more of these steps. In this way, the template method protects the algorithm, the order of execution and provides abstract methods that can be implemented by concrete types.

### Example

```swift
protocol Garden {
func prepareSoil()
func plantSeeds()
func waterPlants()
func prepareGarden()
}

extension Garden {

func prepareGarden() {
func prepareSoil()
func plantSeeds()
func waterPlants()
}
}

final class RoseGarden: Garden {

func prepare() {
prepareGarden()
}
}

```

### Usage

```swift

let roseGarden = RoseGarden()
roseGarden.prepare()
```

🏃 Visitor
----------

Expand Down
37 changes: 37 additions & 0 deletions source/behavioral/template_method.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*:
📝 Template Method
-----------

The template method patter defines the steps of an algorithm and allows the redefinition of one or more of these steps. In this way, the template method protects the algorithm, the order of execution and provides abstract methods that can be implemented by concrete types.

### Example
*/
protocol Garden {
func prepareSoil()
func plantSeeds()
func waterPlants()
func prepareGarden()
}

extension Garden {

func prepareGarden() {
func prepareSoil()
func plantSeeds()
func waterPlants()
}
}

final class RoseGarden: Garden {

func prepare() {
prepareGarden()
}
}

/*:
### Usage
*/

let roseGarden = RoseGarden()
roseGarden.prepare()

0 comments on commit c0daff5

Please sign in to comment.