当前位置:网站首页>[designmode] template method pattern

[designmode] template method pattern

2022-07-07 16:36:00 weixin_ forty-three million two hundred and twenty-four thousan

Basic introduction

  1. Template method pattern (Template Method Pattern), Also called template mode (Template Pattern),z In an abstract class, the template that defines the method to execute it is exposed . Its subclass can override method implementation as needed , But the call will take place in the way defined in the abstract class .

  2. In short , Template method pattern Defines the skeleton of an algorithm in an operation , Instead, defer some steps to subclasses , So that the subclass can not change the structure of an algorithm , You can redefine some specific steps of the algorithm

  3. This type of design pattern belongs to behavioral pattern .

1. Problems in soybean milk production

Write procedures for making soybean milk , The explanation is as follows :

  1. The process of making soybean milk Material selection —> Add ingredients —> soak —> Break it in the soymilk machine

  2. By adding different ingredients , You can make soymilk with different flavors

  3. Material selection 、 Soaking and smashing in a soymilk machine are the same for every flavor of soymilk

  4. Please use Template method pattern complete ( explain : Because the template method pattern , Relatively simple , It's easy to come up with this plan , So use it directly , No longer use the traditional scheme to lead out the template method pattern )

2. Basic introduction to template method pattern

Basic introduction

  1. Template method pattern (Template Method Pattern), Also called template mode (Template Pattern),z In an abstract class, the template that defines the method to execute it is exposed . Its subclass can override method implementation as needed , But the call will take place in the way defined in the abstract class .

  2. In short , Template method pattern Defines the skeleton of an algorithm in an operation , Instead, defer some steps to subclasses , So that subclasses can not

原网站

版权声明
本文为[weixin_ forty-three million two hundred and twenty-four thousan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071416475023.html