› Forums › Python › π Q&A: What Exactly Does the FileHandler Do, and Why Must PhoneBookApplication Still Add Numbers?
- This topic is empty.
-
AuthorPosts
-
December 5, 2025 at 1:53 pm #5855
π Context Code: FileHandler and PhoneBookApplication
class FileHandler: def __init__(self, filename): self.__filename = filename def load_file(self): names = {} with open(self.__filename) as f: for line in f: parts = line.strip().split(';') name, *numbers = parts names[name] = numbers return names class PhoneBookApplication: def __init__(self): self.__phonebook = PhoneBook() self.__filehandler = FileHandler("phonebook.txt") # add the names and numbers from the file to the phone book for name, numbers in self.__filehandler.load_file().items(): for number in numbers: self.__phonebook.add_number(name, number)
π Q&A: What Exactly Does the FileHandler Do, and Why Must PhoneBookApplication Still Add Numbers?
Q1: What is the purpose of the
FileHandlerclass?A:
The FileHandler class is designed solely to read the contents of a file. It takes a filename during initialization and, when theload_file()method is executed, it reads each line, splits the data, and returns a dictionary mapping each name to its list of phone numbers.For example, if the file contains:
Eric;02-1234567;045-4356713 Emily;040-324344The output of
load_file()will be:{ "Eric": ["02-1234567", "045-4356713"], "Emily": ["040-324344"] }This method provides structured data but does not interact with the PhoneBook object.
Q2: If FileHandler returns the data, why does
PhoneBookApplicationstill need to add numbers manually?A:
Even though FileHandler extracts and formats the data, it does not store anything inside the PhoneBook. Its job ends after returning the dictionary.The PhoneBookApplication must therefore loop through the returned data and call:
self.__phonebook.add_number(name, number)This is necessary because adding phone numbers to the PhoneBook is not the FileHandlerβs responsibility.
Q3: Wouldnβt it be simpler if FileHandler added the data directly to the phonebook?
A:
While it may seem convenient, it would violate a core OOP design guideline: the Single Responsibility Principle (SRP).- FileHandler should do one job only: reading files.
- PhoneBook should do one job only: storing and managing contact data.
- PhoneBookApplication should coordinate the workflow.
If FileHandler started modifying the PhoneBook, it would no longer be reusable or cleanly designed.
Q4: What is the role of
PhoneBookApplicationin this setup?A:
PhoneBookApplication acts as the controller of the entire program. It:- Creates a PhoneBook object
- Creates a FileHandler object
- Loads the raw data from the file
- Adds each name and phone number into the PhoneBook
This ensures that the program maintains clear boundaries between reading data, storing data, and executing the main logic.
Q5: How do all three components work together?
A:
Component Responsibility FileHandler Reads file β returns data as a dictionary PhoneBook Stores names and numbers; provides methods like add_number()PhoneBookApplication Reads data via FileHandler and inserts it into the PhoneBook Each component does exactly one focused task, making the program easier to maintain, debug, and expand.
If you’d like, a diagram illustrating the class interactions or a full working example with the PhoneBook class included can also be prepared for your forum audience.
-
AuthorPosts
- You must be logged in to reply to this topic.

