当前位置:网站首页>Teacher Liang's small class
Teacher Liang's small class
2020-11-08 23:53:00 【Official account Song Hua said】

Among many design patterns , I think the template method pattern is easy to understand , It's also a skill worth studying in depth . The definition is as follows , In a multi-step business framework , Most of the steps are fixed , And it can be used in a variety of business scenarios , Variable steps are left to subclasses to implement independently , So we separate stability from change .
Using this model is a little bit careless , There will be some strange problems . The first is that multiple abstract methods modify the same variable , There is a strong correlation between methods , The second problem is that there are too many abstract methods defined . The latter is exactly what I want to talk to you about today , Follow my step and look down .
One 、 Why does this happen ?
I think so , The product manager told us from a business point of view , What steps does this function involve , What needs to be done at each step , Then we copy the concept and turn it into code , Considering that some steps involve multiple implementations , So there are a lot of extension methods left .
soon , New businesses need to reuse these processes , Steps required 1\2\3, But it doesn't take steps 4\5, I can't even understand the steps 4\5 What does it do , I don't know what conditions will be called , The original design is not simple enough .
How to understand the simple here ? Simplicity is from a human point of view . If , The definer and implementer come from two different teams , After the implementer has read the document, the definer needs to explain , To know how to implement abstract methods is right , So it's complex . This is my real feeling , Before that, I was doing capacity building for China Taiwan general purpose , When external empowerment is performed through extension points , The most important thing to think about is how to make the third party understand and start quickly .
To sum up , Copy requirements documents mechanically , Let the execution order be fully reflected in the code structure , It will lead to too many abstract methods in the template method pattern .
Two 、 What to do if something happens ?
What to do if there are too many abstract methods ? In fact, we can use the interface adapter mode to patch .
The actual practice is like this , Create an abstract class Wrapper, Implement all the methods , Method implementation does not require specific business implications . When we write a concrete implementation class , Inherit Wrapper class , Rewrite the methods it cares about , So you don't have to implement steps that you don't understand .
Using interface adapters for compatibility may not be enough , Consider the situation , Abstract methods in templates explicitly need to have return values , How to deal with it ?
It seems that we have to continue patching . such as , Define some default values , Discard these values in the business process . perhaps , Method default implementation runtime throw exception , Indicates that the operation is not supported , It has to be implemented by a third party . Neither of them is good . The first one has special logic , It's expected to be despised by many people . The second is a little more elegant , The disadvantage is that the debugging process can only find the exception .
To sum up , Using the interface adapter pattern , Let the subclass only care about the methods it needs , So the old code 、 Dirty code enlivens .
3、 ... and 、 How to avoid too many abstract methods ?
There are too many abstract methods in the template method pattern , The side shows that the process steps are complicated , Not simple enough . therefore , Another description of the problem is , How to avoid exposing too much detail when designing template method .
My advice is , Organize the business logic , Extract the same kind of behavior , Or merge methods that share a lot of information , Use a general term to summarize the link , Hide the details , Then load it in combination , Don't tile the code directly .
For example . In a request link , The intermediate method directly controls the cache clearing through the cache client 、 Cache value settings , Other links read cache values . This coding style , It will cache Key、 Cache components are exposed to third parties , This leads to difficulties in subsequent modifications . Normally, a new business class should be created , Specifically with the cache Key Dealing with .
To sum up , The key to avoiding too many abstract methods is how to avoid exposing too many details .
This article is from WeChat official account. - Songhua said (songhuasay).
If there is any infringement , Please contact the [email protected] Delete .
Participation of this paper “OSC Source creation plan ”, You are welcome to join us , share .
版权声明
本文为[Official account Song Hua said]所创,转载请带上原文链接,感谢
边栏推荐
- VIM Introduction Manual, (vs Code)
- 你有没有想过为什么交易和退款要拆开不同的表
- 非阻塞的无界线程安全队列 —— ConcurrentLinkedQueue
- 写时复制集合 —— CopyOnWriteArrayList
- 数据库设计:范式与反范式
- APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
- Linked blocking queue based on linked list
- 使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?
- When we talk about data quality, what are we talking about?
- Realization of file copy
猜你喜欢

Mobile big data own website precise marketing and accurate customer acquisition

APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!

Esockettimeout solution in request in nodejs

How does semaphore, a thread synchronization tool that uses an up counter, look like?

你有没有想过为什么交易和退款要拆开不同的表

云计算之路-出海记-小目标:Hello World from .NET 5.0 on AWS

几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!

使用容器存储表格数据

构造函数和原型

教你如何 分析 Android ANR 问题
随机推荐
写时复制集合 —— CopyOnWriteArrayList
Pipedrive如何在每天部署50+次的情况下支持质量发布?
Leetcode-15: sum of three numbers
如何让脚本同时兼容Python2和Python3?
Octave基本语法
你有没有想过为什么交易和退款要拆开不同的表
程序员都应该知道的URI,一文帮你全面了解
Esockettimeout solution in request in nodejs
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!
Concurrent linked queue: a non blocking unbounded thread safe queue
VIM Introduction Manual, (vs Code)
C/C++编程笔记:指针篇!从内存理解指针,让你完全搞懂指针
APP 莫名崩溃,开始以为是 Header 中 name 大小写的锅,最后发现原来是容器的错!
使用递增计数器的线程同步工具 —— 信号量,它的原理是什么样子的?
Decorator (1)
Have you ever thought about why the transaction and refund have to be split into different tables
构造函数和原型
The road of cloud computing - going to sea - small goal: Hello world from. Net 5.0 on AWS
VIM 入门手册, (VS Code)
Mobile big data own website precise marketing and accurate customer acquisition