Refactoring using the MVC pattern
Now that we have developed the complete functionality of our application, we can spot some problems in our current design. For instance, the App
class has several responsibilities, from instantiating Tkinter widgets to executing SQL statements.
Although it seems easy and straightforward to write methods that perform an operation from end to end, this approach leads to code bases that are harder to maintain. We can detect this flaw by anticipating possible architectural changes, such as replacing our relational database with a REST backend accessed via HTTP.
Getting ready
Let's start by defining the MVC pattern and how it maps to the different parts of the application we built in our previous recipe.
This pattern divides our application into three components that encapsulate a single responsibility, forming the MVC triad:
- The model represents the domain data and contains the business rules to interact with it. In our example, it is the
Contact
class and the SQLite...