Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Single Responsibilty Principle
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 = '' = ''

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' = f'{newuser.first_name}.{newuser.last_name}'

# Display the information. Can be changed without affecting registration
Display(newuser.first_name, newuser.last_name,

# Sending the confimation email

Sorry, I should have placed this in the news and discussions.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Johnny's CookBook - Powered By: Python3.8 | Bootstrap4 | Apache2 | Django
I would say Register would actually be Person
Register would contain Person's
Email 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
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

    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

    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):

class ReverseDisplay(Display):

    def output(self, text):

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.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)

controller = Controller()
person1 = Person('John', 'Doe', '')
person2 = Person('Joe', 'Bloggs', '')
controller.email_everyone('Alert!', 'You have been alerted')
controller.display = ReverseDisplay()
person3 = Person('Mac', 'Dee', '')
controller.email_everyone('Alert!', 'You have been alerted')
Display initiaited John Doe added to register Joe Bloggs added to register sending eveyone an mail Email Alert! sent to Email Alert! sent to ReverseDisplay initiaited retsiger ot dedda eeD caM liam na enoyeve gnidnes moc.niamod@eodnhoj ot tnes !trelA liamE moc.niamod@sggolbeoj ot tnes !trelA liamE moc.niamod@eedcam ot tnes !trelA liamE
menator01 likes this post
(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?

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)