zoukankan      html  css  js  c++  java
  • 代码重构之封装集合

    核心:用IEnumerable<T> 而不是用 IList<T>

               如果某个集合只是需要对外暴露查询的功能,那么就应该用IEnumerable<T> 而不是用 IList<T>来作为结果的返回。因为IList<T> 有对集合操作的所有功能(修改,删除等),而IEnumerable<T> 就只有对集合的遍历、取值的操作,不存在更改操作。

    代码演示:

    1、产品代码

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EncapsulateCollection
    {
        public class Product
        {
            public int id { get; set; }
            public string FullName { get; set; }
            public decimal Price { get; set; }
        }
    }
    View Code

    2、集合代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EncapsulateCollection
    {
        public class Order
        {
            public void InitList() {
                list = new List<Product>() {
                    new Product() {  id = 1, FullName ="冰箱", Price = 12000},
                    new Product() { id = 2, FullName = "烤箱", Price = 800 },
                    new Product() { id = 3, FullName = "微波炉", Price = 600 },
                };
            }
            private IList<Product> list;
    
            public IList<Product> GetProducts()
            {
                return list;
            }
    
            public IEnumerable<Product> OnlyGetProducts() {
                return list;
            }
    
            public void AddProduct(Product product) {
                list.Add(product);
            }
    
            public void RemoveProduct(Product product) {
                if (list.Contains(product)) {
                    list.Remove(product);
                }
            }
        }
    }
    View Code

    3、调用代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 封装集合
    /// </summary>
    namespace EncapsulateCollection
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Order order = new Order();
                order.InitList();
                Console.WriteLine("==================集合用IList作为返回=============");
                IList<Product> products =   order.GetProducts();
                Product productItem = new Product() { id = 4, FullName = "苹果", Price = 12 };
                products.Add(productItem);  //新增集合元素
                products.Remove(productItem); //删除集合元素
                order.AddProduct(new Product() { id = 5, FullName = "不粘锅", Price = 199 });
                foreach (var item in products)
                {
                    Console.WriteLine(item.FullName);
                }
    
                Console.WriteLine("==================集合改用IEnumerable作为返回=============");
                order.InitList();
                IEnumerable<Product> productList = order.OnlyGetProducts();
                order.AddProduct(new Product() { id = 4, FullName = "刺身刀", Price = 500 });
                foreach (var item in productList)
                {
                    Console.WriteLine(item.FullName);
                }
                Console.Read();
    
            }
        }
    }
    View Code

    结果展示:

    写写博客,方便自己也方便有需要的人~~

  • 相关阅读:
    强化学习的基本迭代方法
    基于文本描述的事务聚类
    学习强化学习之前需要掌握的3种技能
    其它 华硕 ASAU S4100U 系统安装 win10安装 重装系统 Invalid Partition Table 解决
    数据分析 一些基本的知识
    Python 取样式的内容 合并多个文件的样式 自定义样式
    电商 Python 生成补单公司需要的评论格式3
    SpringBlade 本地图片上传 生成缩略图
    SQL Server 字符串截取
    SpringBlade 本地图片上传
  • 原文地址:https://www.cnblogs.com/Yisijun/p/13213554.html
Copyright © 2011-2022 走看看