- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 195字
- 2021-06-25 20:52:38
The observer pattern
As we advance in this book, we keep mentioning how important decoupling is. When we reduce dependencies, we can extend, develop, and test different modules without having to know the implementation details of other modules. All we have to know is the abstraction they implement.
However, modules should work together in practice. And it's not uncommon that changes in one object are known by another object. For example, if we implement a car class in a game, the engine of the car should know when the accelerator changes its position. The naive solution would be to have an engine class that checks from time to time the accelerator position to see whether it has changed. A smarter approach would be to make the accelerator call the engine to inform it about the changes. But this is not enough if we want to have well-designed code.
If the Accelerator class keeps a reference to the Engine class, what happens when we need to display on screen the position of Accelerator? This is the best solution: instead of making the accelerator dependent on the engine, we should make both of them rely on abstractions.
- Advanced Machine Learning with Python
- Web前端開發技術:HTML、CSS、JavaScript(第3版)
- OpenDaylight Cookbook
- Python科學計算(第2版)
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- 算法訓練營:入門篇(全彩版)
- Wireshark Network Security
- Learn Programming in Python with Cody Jackson
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- Backbone.js Blueprints
- Oracle JDeveloper 11gR2 Cookbook
- 零基礎學單片機C語言程序設計
- Python+Tableau數據可視化之美
- iPhone應用開發從入門到精通
- Python 3.7從入門到精通(視頻教學版)