python中的API状态设计模式
据我所知,以下代码是否足以回答所附问题?请您帮助澄清缺失的点或部分,如果我添加一些异常捕获,如果编辑_title和编辑_description仅在新的类()或任务API类()中的摘要中,并在子类中重写它,我是否应该使用Django rest framework API,下面的“进行中”状态写为“链接两个任务,您应该能够使用任何id调用这两个任务”,这是什么意思python中的API状态设计模式,python,django,design-patterns,Python,Django,Design Patterns,据我所知,以下代码是否足以回答所附问题?请您帮助澄清缺失的点或部分,如果我添加一些异常捕获,如果编辑_title和编辑_description仅在新的类()或任务API类()中的摘要中,并在子类中重写它,我是否应该使用Django rest framework API,下面的“进行中”状态写为“链接两个任务,您应该能够使用任何id调用这两个任务”,这是什么意思 # States of a Task class TaskAPI(object): """ Abstract base
# 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')
Django Rest框架在api方面做得非常好,他们在文档中有很多示例。这个问题是否需要使用Django Rest框架?您不需要使用Rest框架,它只是让编写api变得更容易。您可以与我共享一个编写api的链接吗?这是一个很好的示例