//封装集合,只提供调用端需要的接口
//原文链接:http://lostechies.com/seanchambers/2009/07/31/31-days-of-refactoring/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplicationTest
{
class Program
{
static void Main(string[] args)
{
Order order = new Order();
order.AddOrderLine(new Orderline(12.3));
Console.WriteLine(order.OrderLines.First().Total);
Console.ReadKey();
}
}
public class Order
{
private List<Orderline> _orderlines;
private double _orderTotal;
public Order()
{
_orderlines = new List<Orderline>();
}
public IEnumerable<Orderline> OrderLines
{
get { return _orderlines; }
}
public void AddOrderLine(Orderline orderline)
{
_orderTotal += orderline.Total;
_orderlines.Add(orderline);
}
public void RemoveOrderLine(Orderline orderline)
{
orderline = _orderlines.Find(o => o == orderline);
if (null == orderline)
return;
_orderTotal -= orderline.Total;
_orderlines.Remove(orderline);
}
}
public class Orderline
{
public Orderline(double t)
{
this.Total = t;
}
public double Total { get; set; }
}
}
项目代码重构(一)
最新推荐文章于 2023-01-03 17:14:43 发布