首先,這完全是C#的問題!請將您的知識限制在C#中。工廠方法實現的含義
在我正在開發的應用程序中,我們有一些類可以提供預設的「原型」。處於非常高的水平:
public class Foo
{
#region Variables/Properties
public bool Bar { get; set; }
public string Name { get; set; }
public int Fooby { get; set; }
#endregion Variables/Properties
#region Prototypes
public Foo SomePrototype = new Foo
{
Bar = false,
Name = "Generic False State",
Fooby = -1
};
public Foo SomeOtherPrototype = new Foo
{
Bar = true,
Name = "Generic True State",
Fooby = Int32.MaxValue
};
#endregion Prototypes
#region Methods
// ...Behaviors here.
#endregion Methods
}
請注意「原型」部分。我一直了解,在C#中,「正確」的方式做這樣的「原型」是:
public Foo SomePrototype
{
get
{
return new Foo
{
Bar = false,
Name = "Generic False State",
Fooby = -1
};
}
}
雖然我想無論是「原型」的實施可以工作,問:有一些什麼的在我們的代碼庫中看到的方式(即第一個示例?)的含義
對我來說,明顯的含義是,'原型'的所有用法都只是引用該公用的Foo
變量,這可以提供一些有趣的交互......我只是想知道是否還有更多需要注意的事情。
'Foo f = new Foo(); f.SomePrototype = new Foo {Name =「Baz」};' – I4V 2013-05-10 13:27:04
這應該是一個答案。我很困擾我的基於getter的思維模式,我甚至都沒有看到。 – 2013-05-10 13:28:32