Design patterns 当用户选择选项时,选项列表的设计模式是什么
我有一个应用程序,用户选择初始选择,这个选择将呈现另一组选择,每个选择都可以调用呈现另一组选择的应用程序流 例如:Design patterns 当用户选择选项时,选项列表的设计模式是什么,design-patterns,Design Patterns,我有一个应用程序,用户选择初始选择,这个选择将呈现另一组选择,每个选择都可以调用呈现另一组选择的应用程序流 例如: app: what is you name ? user: poko app: what is your profession? 1.programmer 2.Lawyer 3.blacksmith user: 2 app: what total income per year 1. above 100$ 2. above 200$ user: 2 in
app: what is you name ?
user: poko
app: what is your profession?
1.programmer
2.Lawyer
3.blacksmith
user: 2
app: what total income per year
1. above 100$
2. above 200$
user: 2
in the application:
app invokes layer handler with handling income above 200 $
这种流程的最佳设计模式是什么?设计模式
这种流程的最佳设计模式是什么
一个问题没有单一的模式
附加的设计模式并不能解决问题。相反,它帮助您构建代码库。换句话说:您不需要设计模式来解决这个问题,但可以使用一个或多个模式来构造代码
数据结构
与设计模式不同,您可以使用数据结构来增强算法。对于算法问题,总是有一个适用的数据结构
你的问题是一个流程。这听起来像是一个你可以穿越的女巫。顶点可以是一个包含有关问题和选择的信息的对象,边就是答案。正如@Roman所说,这是一个数据结构。您应该为流选择一个数据结构,然后选择一个方法将此数据结构表示到应用程序中。例如,如果此数据结构是一棵树,则可以使用它来表示它。例如(我不知道您使用的是什么编程语言,因此我使用了PHP):
下一个问题是否取决于当前的具体答案?我的意思是,如果用户选择“高于100$”或“高于200$”,那么他将看到各种各样的问题?这也可以是选项,这是一个高级示例,我需要看看我可以设置哪种模式。这里的每个选项都是一个分支。这听起来像是一个决策树的例子。正如下面的答案所指出的,这就像有一个由选项列表组成的类选项。因此,设计可以基于组合模式。
class Answer
{
/**
* @var string The text of the answer which will be shown to the user
*/
public $text;
/**
* @var Question The question which will be shown to the user if the current answer was chosen
*/
public $nextQuestion;
}
class Question
{
/**
* @var string The text of the question which will be shown to the user
*/
public $text;
/**
* @var Answer[] List of answers by the question
*/
public $answers = [];
/**
* Choice of answer and getting the next question
*
* @param string $value The text of the answer
* @return Questio The next question
*/
public function chooseAnswer(string $value) : Question
{
foreach($this->answers as $answer) {
if ($answer === $value) {
return $answer->nextQuestion;
}
}
}
}