使用和創建一只Dog,我經常這樣使用,或者組合其中的幾個,當然其中有單例模式,也不知道別人一般怎樣創建。
單例模式一般用於像數據庫訪問類這樣的東西,因為多也沒用,只是不知道只有一個夠不夠用,哈哈,這問題,就是什么時候用單例,什么時候需要多New幾個,取決於性能還是需求呢?倒想請大牛指點指點
public class Dog { private static Dog _instance = null; public static Dog Instance { get { if (_instance == null) _instance = new Dog(); return _instance; } } public static Dog CreateOne() { return new Dog(); } public static Dog CreateTwo() { return Dog.Instance; } public string Name { get; set; } public int Age { get; set; } } public class Execute { //創建Dog方式一 Dog dog = Dog.Instance; //創建Dog方式二 Dog dog2 = new Dog(); //創建Dog方式三,四 Dog dog3 = Dog.CreateOne(); Dog dog4 = Dog.CreateTwo(); //創建方式五 public Dog GetADog() { return this.CreateDog(); } public Dog CreateDog() { return new Dog(); } }