Single Responsibilty Principle - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: Single Responsibilty Principle (/thread-27946.html) |
Single Responsibilty Principle - menator01 - Jun-28-2020 I got to thinking about what ndc85430 said in one of my post, and was wondering if this would roughly fall along those lines. Classes could be added or removed without affecting the others. All classes could have a relation with the Register class or other class within the program. Just trying to get a basic understanding. Thanks for any input. #! /usr/bin/env python3 class Register: def __init__(self): self.first_name = '' self.last_name = '' self.email = '' class Email(Register): def __init__(self, email): print(f'Sending confirmation email to {email}') class Display(Register): def __init__(self, first_name, last_name, email): print(f'Hello {first_name.title()} {last_name.title()}\nYour email is {email}') # Initial registration newuser = Register() newuser.first_name = 'John' newuser.last_name = 'Doe' newuser.email = f'{newuser.first_name}.{newuser.last_name}@domain.com' # Display the information. Can be changed without affecting registration Display(newuser.first_name, newuser.last_name, newuser.email) # Sending the confimation email Email(newuser.email) Sorry, I should have placed this in the news and discussions. RE: Single Responsibilty Principle - Yoriz - Jun-28-2020 I would say Register would actually be Person Register would contain Person 'sEmail would be Emailer it would not be an subclass of Register Display would not be a subclass of Register Tyr to separate things as much as possible so they do their own thing. My Rock, Paper, Scissors is an example of separating responsibility https://python-forum.io/Thread-Another-Rock-Paper-Scissors A lot of the code could be reused to make a GUI version Here is a possible example of your code above, each class has its own responsibility to do something. classes can be changed without affecting the other classes. class Person: def __init__(self, first_name, last_name, email_address): self.first_name = first_name self.last_name = last_name self.email_address = email_address @property def fullname(self): return f'{self.first_name} {self.last_name}' class Register: def __init__(self, people_dict=None): self.people_dict = people_dict or {} def add_person(self, person): self.people_dict[person.fullname] = person class Emailer: def __init__(self): # code that sets up enabling the sending of emails pass def send_mail(self, email_address, subject, text): # code that sends the email return f'Email {subject} sent to {email_address}' class Display: def __init__(self): print(f'{self.__class__.__name__} initiaited') def output(self, text): print(text) class ReverseDisplay(Display): def output(self, text): print(text[::-1]) class Controller: def __init__(self, register=None, emailer=None, display=None): self.register = register or Register() self.emailer = emailer or Emailer() self.display = display or Display() def add_person(self, person): self.register.add_person(person) self.display.output(f'{person.fullname} added to register') def email_everyone(self, subject, text): self.display.output('sending eveyone an mail') for key, person in self.register.people_dict.items(): result = self.emailer.send_mail( person.email_address, subject, text) self.display.output(result) controller = Controller() person1 = Person('John', 'Doe', '[email protected]') person2 = Person('Joe', 'Bloggs', '[email protected]') controller.add_person(person1) controller.add_person(person2) controller.email_everyone('Alert!', 'You have been alerted') controller.display = ReverseDisplay() person3 = Person('Mac', 'Dee', '[email protected]') controller.add_person(person3) controller.email_everyone('Alert!', 'You have been alerted')
RE: Single Responsibilty Principle - ndc85430 - Jun-28-2020 (Jun-28-2020, 01:55 PM)menator01 Wrote:class Email(Register): def __init__(self, email): print(f'Sending confirmation email to {email}') class Display(Register): def __init__(self, first_name, last_name, email): print(f'Hello {first_name.title()} {last_name.title()}\nYour email is {email}') Two things here: 1. Why do these classes inherit from Register ? They aren't kinds of register (or person, as that seems to be what Register is modelling), so inheritance doesn't really make sense there.2. Why do you need the classes at all? They aren't encapsulating some data and providing methods to operate on that data (like say, how you might have a class to abstract away access to a database, with methods to find and add items, say). They aren't about bundling some data together either (like what Register does), so you aren't going to be creating multiple instances of them. Instead, each one seems to be about performing one specific action. Why then aren't you just using functions?
|