Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from abc import ABCMeta, abstractmethod
import json


class User:
  def __init__(self, name):
    self.name = name


class AdminUser(User):
  def be_represented_by(self, serializer):
    return serializer.represent_admin_user(self)


class TrialUser(User):
  def be_represented_by(self, serializer):
    return serializer.represent_trial_user(self)



class Representer(metaclass=ABCMeta):
  def represent(self, user):
    return user.be_represented_by(self)

  @abstractmethod
  def represent_admin_user(self, user):
    pass

  @abstractmethod
  def represent_trial_user(self, user):
    pass


class JsonRepresenter(Representer):
  def represent_admin_user(self, user):
    return json.dumps({
      "admin": True,
      "name": user.name,
    })

  def represent_trial_user(self, user):
    return json.dumps({
      "trial": True,
      "name": user.name,
    })


class StringRepresenter(Representer):
  def represent_admin_user(self, user):
    return '<<{}>>'.format(user.name)

  def represent_trial_user(self, user):
    return '[TRIAL] {}'.format(user.name)


user = AdminUser('Homer')
representer = JsonRepresenter()

for user in AdminUser('Homer'), TrialUser('Bart'):
  for representer in JsonRepresenter(), StringRepresenter():
    print(representer.represent(user))