【发布时间】:2011-08-26 05:04:13
【问题描述】:
好的,我正在制作分布式系统的模型。 mClass(对象)的每个实例由 1 个线程使用。可变数据不能在 mClass 对象之间传递 这是一个类似于我所拥有的代码示例:
type mClass (mID:id)=
member this.ID=mID
let resolve : id->mClass =... //This function turns IDs into the objects they represnt
...
let managementInfo = ref Some mutableData
//this should only ever be called by the GiveControl method.
private member this.TakeControl (frozenData) =
let defrostedData = ... //a variable that works out the unfrozen version of the frozenData
managementInfo := Some defrostedData
///This is a helper that ensure that when ever someone else is told to take control
///that all my internal data and any functions i need to call to tell others are done
///And convert the data a nonmutable form
///(Mutable data can not be tranfered between different instances of of mClass (objects),
///as this is a mockup of a distributed system, where the instances of mClass can't
///@param: nID this ID of the object we are giving control to
///@param: callbacks: a list of functions to call to tell other objects i've given control
private member this.GiveControl nID (callbacks: list<id->unit>) =
let updateInternalData = ... //A function to upate my internal state
updateInternalData;
ignore (List.map (fun f-> f nID) callbacks);
let freezeData data= ... //a function make our mutableData into nonmutable data
let frozenData = (!managemenInfo).Value|>freezeData
managemenInfo:=None;
(nID|>resolve).TakeControl (frozenData)
...
私有成员 this.GiveControl 最初被声明为: 让给予控制 但它让编译器抱怨错误声明了一个新类型,直到我将它改为私有方法。
问题是,我怎样才能重新分解这段代码, 将两种方法合二为一。 (或至少使 GiveControl 只能从该对象内调用(实际上是私有的),而 TakeControl 只能从 GiveControl 调用)
【问题讨论】:
标签: f# refactoring