as I understood, Is the following code sufficient for the attached question? please need your help to clarify the missing points or parts, should I add some exceptions catching, should edit_title & edit_description be only in the New class() or abstract in the TaskAPI class() and override it inside child classes , should i use the Django rest framework API, below the in-progress state is written 'link two tasks you should be able to call both of them using any id' what does that mean.
# States of a Task
class TaskAPI(object):
""" Abstract base class of state of a task """
name = "state"
allowed = []
def switch(self, state):
""" Switch to new state """
if state.name in self.allowed:
print('Current:', self, ' => switched to new state', state.name)
self.__class__ = state
# print( self.__class__)
else:
print('Current:', self, ' => switching to', state.name, 'not possible.')
def __str__(self):
return self.name
class New(TaskAPI):
""" New State being in progress """
name = "New"
allowed = ['InProgress']
def edit_title(self,new_title):
self.title = new_title
def edit_description(self,new_description):
self.new_description = new_description
class InProgress(TaskAPI):
""" State of being in progress after New state and need to switched to be done """
name = "InProgress"
allowed = ['Done']
class Done(TaskAPI):
""" Done State of task """
name = "Done"
allowed = []
class Task(object):
""" A class representing a Task """
def __init__(self, id,title,description):
self.id = id
self.title = title
self.description = description
# State of the computer - default is New.
self.state = New()
def change(self, state):
""" Change state """
self.state.switch(state)
if __name__ == "__main__":
t1 = Task(1,'task1','test1')
t1.change(InProgress)
t1.change(Done)
print(t1.state.__class__)
t1.change(InProgress)
print(t1.title)
try:
t1.state.edit_title
except:
print('Edit title process not allowed! for {}'.format(t1.state.name),'state')
Aucun commentaire:
Enregistrer un commentaire