可变状态和观察者模式

时间:2013-09-15 16:28:22

标签: f# observer-pattern mutable

我目前正在重新开发F#中的应用程序,虽然经验非常出色,但在控制可变性时,我发现自己有点困惑。

以前,我的C#程序使用的文档模型是高度可变的,并且实现了ObservableCollections和INotifyPropertyChanged,它们之间的共享状态不会出错。显然,这不是一个理想的选择,特别是如果我想要一个完全不可改变的设计方法。

考虑到这一点,我为我的底层应用程序内核创建了一个不可观察的,不可变的文档模型,但是,因为我希望UI订阅者看到更改,我立即发现自己实现了事件驱动模式:

// Raw data.
type KernelData = { DocumentContent : List<string> }

// Commands that act on the data.
type KernelCommands = { AddString : string -> () }

// A command implementation. Performs a state change, echos the new state through the event.
let addStringCommand (kernelState : KernelData) (kernelChanged : Event<KernelData>) (newString : string) =
    kernelState with { DocumentContent=oldList |> List.add newString }
    |> kernelChanged.Trigger

// Time to wire this up.
do
    // Create some starting state.
    let kernelData = { DocumentContent=List.Empty }

    // Create a shared event that commands may use to inform observers (UI).
    let kernelChangedEvent = new Event<KernelData>()

    // Create the command, it uses the event to inform observers.
    let kernelCommands = { AddString=addString kernelData kernelChangedEvent }

    // Create a UI element that uses the commands to initialize data transformations. UI elements subscribed to the data use the event to listen.
    let myUI = new UiObject(kernelData, kernelChangedEvent.Publish, kernelCommands)
    myUI.Show()

所以这是我将新状态传递给相关听众的解决方案。然而,更理想的是一个&#34;框&#34;我可以&#34;勾解&#34;转换功能。当框变异时,调用函数来处理新状态并在UI组件中产生相应的更改状态。

do
    // Lambda called whenever the box changes.
    idealBox >>= (fun newModel -> new UIComponent(newModel))

所以我想我是否有一个可观察的模式来处理这些情况。可变状态通常使用monad处理,但我只看到涉及执行操作的示例(例如管道控制台IO monad,加载文件等),而不是实际处理持续变异状态。

1 个答案:

答案 0 :(得分:4)

我对这些场景的一般解决方案是在纯功能设置中构建所有业务逻辑,然后提供瘦服务层,其中包含用于同步和传播更改的必要功能。以下是KernelData类型的纯接口示例:

type KernelData = { DocumentContent : List<string> }
let emptyKernelData = {DocumentContent = []}
let addDocument c kData = {kData with DocumentContent = c :: kData.DocumentContent}

然后我将定义一个服务层接口,其中包含用于修改和订阅更改的功能:

type UpdateResult = 
    | Ok
    | Error of string

/// Service interface
type KernelService =
{
    /// Gets the current kernel state.
    Current : unit -> KernelData

    /// Subscribes to state changes.
    Subscribe : (KernelData -> unit) -> IDisposable

    /// Modifies the current kernel state.
    Modify : (KernelData -> KernelData) -> Async<UpdateResult>
}

Async响应启用非阻止更新。 UpdateResult类型用于表示更新操作是否成功。为了构建一个健全的KernelService对象,重要的是要意识到修改请求需要通过同步来避免并行更新导致数据丢失。为此,MailboxProcessor派上用场。这是一个buildKernelService函数,它在给定初始KernelData对象的情况下构造服务接口。

// Builds a service given an initial kernel data value.
let builKernelService (def: KernelData) =

    // Keeps track of the current kernel data state.
    let current = ref def

    // Keeps track of update events.
    let changes = new Event<KernelData>()

    // Serves incoming requests for getting the current state.
    let currentProc :  MailboxProcessor<AsyncReplyChannel<KernelData>> =
        MailboxProcessor.Start <| fun inbox ->
            let rec loop () =
                async {
                    let! chn = inbox.Receive ()
                    chn.Reply current.Value
                    return! loop ()
                }
            loop ()

    // Serves incoming 'modify requests'.
    let modifyProc : MailboxProcessor<(KernelData -> KernelData) * AsyncReplyChannel<UpdateResult>> =
        MailboxProcessor.Start <| fun inbox ->
            let rec loop () =
                async {
                    let! f, chn = inbox.Receive ()
                    let v = current.Value
                    try
                        current := f v
                        changes.Trigger current.Value
                        chn.Reply UpdateResult.Ok
                    with
                    | e ->
                        chn.Reply (UpdateResult.Error e.Message)
                    return! loop ()
                }
            loop ()
    {
        Current = fun () -> currentProc.PostAndReply id
        Subscribe = changes.Publish.Subscribe
        Modify = fun f -> modifyProc.PostAndAsyncReply (fun chn -> f, chn)
    }

请注意,上面的实现中没有任何内容对KernelData是唯一的,因此服务接口和 build 函数可以推广到任意类型的内部状态。

最后,使用KernelService对象进行编程的一些示例:

// Build service object.
let service = builKernelService emptyKernelData

// Print current value.
let curr = printfn "Current state: %A" service.Current

// Subscribe 
let dispose = service.Subscribe (printfn "New State: %A")


// Non blocking update adding a document
service.Modify <| addDocument "New Document 1"

// Non blocking update removing all existing documents.
service.Modify (fun _ -> emptyKernelData)

// Blocking update operation adding a document.
async {
    let! res = service.Modify (addDocument "New Document 2")
    printfn "Update Result: %A" res
    return ()
}
|> Async.RunSynchronously

// Blocking update operation eventually failing.
async {
    let! res = 
        service.Modify (fun kernelState ->
            System.Threading.Thread.Sleep 10000
            failwith "Something terrible happened"
        )
    printfn "Update Result: %A" res
    return ()
}
|> Async.RunSynchronously

除了更多技术细节之外,我认为与原始解决方案最重要的区别在于不需要特殊的命令功能。使用服务层,可以使用KernelData函数将在Modify上运行的任何纯函数(例如addDocument)提升为有状态计算。

相关问题