jeudi 14 janvier 2021

How can I handle default email from a list of emails?

I have the following class

class ContactCard:
  def __init__(self):
    self._default_email = None

  def add_email(self, email: Email):
    if len(self._emails) == 0: # first email becomes the default
      self.default_email = email
    self._emails.append(email)
  
  def default_email(self):
    return self._default_email

  def set_default_email(email: Email):
    if email not in self._emails:
      raise EmailDoesntExistsInThisCardError()
    self._default_email = email # replace the default

Then I want to do something with Email

vcard = VCard()
vcard.default_email().something() # null pointer

Then I think, maybe I can create a NullEmail?

class ContactCard:
  def __init__(self):
    self._default_email = NullEmail()
  ...

So now I can do it safely

vcard = VCard()
vcard.default_email().something() # it does nothing but without error

I need to do the same with phone numbers, so I've thought instead of having a default_phone and default_email, having a set/array of unique instances like defaults which can contain just one email and one phone, if you then set a new phone, it replaces the current one. So defaults always have just one reference to an email or phone. The thing is that if there is

Do you have some recommendation to handle defaults?

Aucun commentaire:

Enregistrer un commentaire