English 中文(简体)
分离逻辑/准则和用户互动
原标题:Separating Logic/GUI and user interaction
  • 时间:2009-10-21 09:03:42
  •  标签:

想象你能够产生/复制/移动档案。 [图表]

如果已经存在一个应当复制/复制的档案,请用户填写档案。

如果(G) 事实和逻辑完全分离,那么你采取什么办法加以落实?

我所想的第一件事是MVC-pattern,但这意味着,只要我需要用户的互动,我就不得不加以利用。

任何其他建议?

BTW:你如何用非办事处语言执行?

最佳回答

我可以看到两种方式:

  • You have two functions, file_exists(...) and copy_file(...). The UI side always calls file_exists first and asks the user whether to copy the file is it already exists.
  • You have only one function copy_file(bool force, ...), that by default fails if the file exists. So UI side calls the default version of the function, check if it failed and why, if it was because the file already exists, ask the user and try again with force=true.
问题回答

如果《全球倡议》和逻辑确实分开,那么这一问题就永远不会出现。 通过设计,方案应当根据具有违约价值的备选办法,进行超婚或非超婚。 如果《全球倡议》能够提供,可以确定这一选择。

事实上,虽然显而易见的做法是,在它上才行,并开始复制,但你可以首先看看冲突,并检查目标装置有足够的免费储存。 那么,如果存在问题,除非有《全球倡议》,否则就不做任何事情,否则你可以报告问题,并询问是否继续工作。

如果你想有一个能够以档案方式在档案中援引全球协调倡议的设计,那么就把这个逻辑设计成一套单一的程序,每套复制一份档案,并在错误报告部分提供可选择的全球倡议。 然后,德国金融情报局可以重新确认复印单的逻辑。

在一种非联络处语言中,我会实施某种事件,即父母(或儿童,视你的设计而定)在一只繁忙的旗帜真实的情况下对事件进行了投票。 这一事件使对方在等待其解答的旗帜时做其他工作。 当然,在这两种方向上,都必须遵守一些时限以及相互排斥。 基本上,这意味着不锁定I/O的原则,或你关于在此设置实际免费节目的有利理论。

程序可以进行交流。 根据你们选择的语言,你通过有原始信号的银,交流了记忆、ema光。 它很难更具体地处理这种一般性问题。

参看我的意见,需要更多的资料,以便用你所选择的语言拟定答案。

我所想的第一件事是MVC-pattern,但这意味着,在我需要用户互动的地方,我必须使用它。

而这正是为什么? 分离“全球倡议”和逻辑是实际上MVC型号。 仅仅因为其名称很长——一旦你把“观点”和逻辑分开,你至少会有一个“控制者”——如果不是一个“模范”——而如果你的申请已经表明,你也会效仿。 你也许没有承认这一点。

从我可以看到的角度来看,确实存在两个问题:

  1. We have an algorithm (logic) in which we would like to defer some operations and decisions to something else (e.g. user via UI).
  2. We would like to avoid tight coupling between the algorithm and that something else.

如果我们使用联络处的语文,就有几个设计赞助人来应对这两个具体问题。

  • Template Method pattern can solve #1. It does not solve #2 very well because the typical implementation is via inheritence.
  • Observer pattern looks promising too.

因此,它实际上选择和混合最简单、最适合语言的需要。

在实践中,如果谈论C#,我们就可采用模版方法和观察员混合办法,例如:

// This will handle extensions to the FileCopy algorithm
abstract class FileCopyExtention
{
 public abstract Response WhatToDoWhenFileExists();
}


// the copy function, pure logic
public static void Copy(string source, string destination, FileCopyExtention extension) 
{
 if (File.Exists(destination))
 {
  var response = _extension.WhatToDoWhenFileExists();
  if (response == overwrite)
   // overwrite the file
  else
   // error
 }
}

// This is our user-interactive UI extension
class FileCopyUI : FileCopyExtention
{
 public override Response WhatToDoWhenFileExists()
 {
  // show some UI, return user s response to the caller
 }
}

// the program itself
void Main()
{
 Copy("/tmp/foo", "/tmp/bar", new FileCopyUI());
}

作为主题的一个变式,你可以利用你所选择的任何语言的活动、代表。

在C,这可能是一个功能点,在C++中,可以提到一类的猜测。

这种做法[假编码]:

UIClass
{
    //
    // Some code
    //

    bool fileCopied = false;

    do {
        try {
            fileCopied = CopyFile(fileName);
        } catch (FileExists) {
            //
            // Ask "File exists! Overwrite?" If "No", exit do-loop
            //
        } catch (FileLocked) {
            //
            // Ask "File Locked! Repeat?",   If "No", exit do-loop
           //
        } catch (etc...) {
            //
            // etc.
            //    
        }
    } while (!fileCopied);

    //
    // Some code
    //
}

LogicClass
{
    //
    // Some code
    //

    bool CopyFile(string fileName)
    {
        //
        // copy file
        //
    }

    //
    // Some code
    //

}




相关问题
热门标签