学习之前,先喊一下口号:每天进步一点,生活更好一点

首先声明一点,我也是新新新手一枚,崭新的新哦。如果文章有不合理的地方,也请各位博友多多指点,不要乱喷哦

我的文采很低调,低调到语文老师对我的期望是你什么时候能及格啊!!!▄█▀█●给跪了@@@ 所以我的文章都是直奔主题,没有华丽的装饰,没准可以美而言之『通俗易懂』呢ヾ(=゚・゚=)ノ喵♪

好了,可以开始了

我们声明两个类  Customer  和  Order

  1. public class Customer
  2. {
  3. public string CustomerID { get; set; }
  4. public string CompanyName { get; set; }
  5. public string ContactName { get; set; }
  6. public string ContactTitle { get; set; }
  7. }
  1. public class Order
  2. {
  3. public int OrderID { get; set; }
  4. public string CustomerID { get; set; }
  5. public int? EmployeeId { get; set; }
  6. public DateTime? OrderDate { get; set; }
  7. }

如果是刚开始学习编程,如果要对这两个类进行操作,就需要建立两个操作类  CustomerOperation  和 OrderOperation

  1. public class CustomerOperation
  2. {
  3. public void Creat(Customer item)
  4. {
  5. }
  6. public void Delete(Customer item)
  7. {
  8. }
  9. public IEnumerable<Customer> Get()
  10. {
  11. }
  12. public void Update(Customer item)
  13. {
  14. }
  15. }
  1. public class OrderOperation
  2. {
  3. public void Creat(Order item)
  4. {
  5. }
  6. public void Delete(Order item)
  7. {
  8. }
  9. public IEnumerable<Order> Get()
  10. {
  11. }
  12. public void Update(Order item)
  13. {
  14. }
  15. }

好了,关于两个类的CRUD(增查改删)就写好了,下面我们就可以使用了

  1. static void Main(string[] args)
  2. {
  3. CustomerOperation customer = new CustomerOperation();
  4. OrderOperation order = new OrderOperation();
  5. customer.Get();
  6. order.Get();
  7. Console.ReadLine();
  8. }

这样一来,我们就new了两个实例,如果我们有一百个类,岂不是要new一百遍!!o(>﹏<)o   

那怎么样new一个实例,可以同时操作两个类呢,那还不简单,我们把两个操作类,写到一个里边

  1. public class Factory
  2. {
  3. public void CustomerCreat(Customer item)
  4. {
  5. }
  6. public void CustomerDelete(Customer item)
  7. {
  8. }
  9. public IEnumerable<Customer> CustomerGet()
  10. {
  11. }
  12. public void CustomerUpdate(Customer item)
  13. {
  14. }
  15. public void OrderCreat(Order item)
  16. {
  17. }
  18. public void OrderDelete(Order item)
  19. {
  20. }
  21. public IEnumerable<Order> OrderGet()
  22. {
  23. }
  24. public void OrderUpdate(Order item)
  25. {
  26. }
  27. }

这时,我们再使用的时候,不就直接实例化一个就可以了

  1. static void Main(string[] args)
  2. {
  3. Factory factory = new Factory();
  4. Factory.CustomerGet();
  5. Factory.OrderGet();
  6. Console.ReadLine();
  7. }

果然聪明,这就基本上可以称作是单一实例模式了

但是,如果将来有一百个类,你的Factory岂不是要爆掉,那怎么办呢,我已经修炼了第一层功力,我是这样做的

原来的两个操作类不变,在Factory里边进行实例化

  1. public class Factory
  2. {
  3. private CustomerOperation _customerOperation = null;
  4. private OrderOperation _orderOperation = null;
  5. public Customer Customers
  6. {
  7. get
  8. {
  9. if (_customerOperation == null)
  10. {
  11. _customerOperation = new CustomerOperation();
  12. }
  13. return this._customerOperation;
  14. }
  15. }
  16. public Order Orders
  17. {
  18. get
  19. {
  20. if (_orderOperation == null)
  21. {
  22. _orderOperation = new OrderOperation();
  23. }
  24. return this._orderOperation;
  25. }
  26. }
  27. }

然后我们再去使用的时候

  1. static void Main(string[] args)
  2. {
  3. Factory factory = new Factory();
  4. factory .Orders.Get();
  5. factory .Customers.Get();
  6. Console.ReadLine();
  7. }

这样操作起来是不是就直观很多了,代码量也减少了很多,这就是我理解的单一实例模式 

 

版权声明:本文为xu3784原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xu3784/p/9717206.html