- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 454字
- 2021-07-02 23:43:42
The advantages of compartmentalizing code
One important difference between procedural programming (think C-style) and object-oriented programming is the ability to encapsulate or compartmentalize code. Oftentimes we think of this as just data hiding: making variables private. In a C-style program, the functions and data are separate, but it is hard to reuse any one function because it might depend on other functions or other pieces of data in the program. In object-oriented programming, we are allowed to group the data and function together into reusable pieces. That means we can (hopefully) take a class or module and place it in a new project. This also means that since the data is private, a variable can be easily changed as long as the interface or public methods don't change. These concepts of encapsulation are important, but they aren't showing us all of the power that this provides us.
The goal of writing object-oriented code is to create objects that are responsible for themselves. Using a lot of if/else or switch statements within your code can be a symptom of bad design. For example, if I have three classes that need to read data from a text file, I have the choice of using a switch statement to read the data differently for each class type, or passing the text file to a class method and letting the class read the data itself. This is even more powerful when combined with the power of inheritance and polymorphism.
By making the classes responsible for themselves, the classes can change without breaking other code, and the other code can change without breaking the classes. We can all imagine how fragile the code would be if a game was written entirely in the main function. Anything that is added or removed is likely to break other code. Anytime a new member joined the team, they would need to understand absolutely every line and every variable in the game before they could be trusted to write anything.
By separating code into functions or classes, we are making the code easier to read, test, debug, and maintain. Anyone joining the team would of course need to understand some pieces of the code, but it might not be necessary to understand all of graphics if they are working on game logic or file loading.
Design patterns are solutions to common programming problems flexible enough to handle change. They do this by compartmentalizing sections of code. This isn't by accident. For the purposes of this book, the definition of good design is encapsulated, flexible, reusable code. So it should come as no surprise that these solutions are organized into classes or groups of classes that encapsulate the changing sections of your code.
- 企業級Java EE架構設計精深實踐
- Raspberry Pi Networking Cookbook(Second Edition)
- Python自動化運維快速入門
- Scala Design Patterns
- 你必須知道的204個Visual C++開發問題
- Java 11 Cookbook
- Web程序設計(第二版)
- Mastering Linux Network Administration
- 學習正則表達式
- Selenium Testing Tools Cookbook(Second Edition)
- PySide 6/PyQt 6快速開發與實戰
- Java零基礎實戰
- Distributed Computing in Java 9
- WordPress Search Engine Optimization(Second Edition)
- Solr權威指南(下卷)