框架设计基本概念-第一篇

时间:2022-12-19 17:46:23


给大家介绍一个订单的设计,考虑到商品普通价和VIP打折后的不同订单金额


运行结果:

框架设计基本概念-第一篇



UML图:

框架设计基本概念-第一篇

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
class Program
{
static void Main(string[] args)
{

IOperationPrice operationPrice = new OperationNormalPrice();

Order order = new Order(operationPrice)
{
Status = OrderStatus.Active,
Items = new Items()
};
Console.WriteLine("普通价:" + order.GetPrice());

operationPrice = new OperationVipPrice();
order = new Order(operationPrice)
{
Status = OrderStatus.Active,
Items = new Items()
};
Console.WriteLine("*VIP价:" + order.GetPrice());

Console.ReadKey();

}
}
}



Item.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public class Item
{
public string ItemName { get; set; }
public string ItemCode { get; set; }
public int Price { get; set; }
}
}


Items.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public class Items : List<Item>, IEnumerable<Item>
{
public Items()
{
this.Add(new Item { ItemName = ".net高级编程", ItemCode = "001", Price = 100 });
this.Add(new Item { ItemName = "程序员修炼", ItemCode = "002", Price = 200 });
}
}
}


IOperationPrice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public interface IOperationPrice
{

double GetPrice(Order order);

}
}



OperationNormalPrice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public class OperationNormalPrice : IOperationPrice
{
public double GetPrice(Order order)
{
double result = 0;
foreach (var item in order.Items)
{
result += item.Price;
}
return result;
}
}
}


OperationVipPrice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public class OperationVipPrice : IOperationPrice
{
public double GetPrice(Order order)
{
double result = 0;
foreach (var item in order.Items)
{
result += item.Price-20;
}
return result;
}
}
}



Order.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
public class Order
{

public OrderStatus Status;
public Items Items;

private IOperationPrice OperationPrice;

public Order(IOperationPrice operationPrice)
{
this.OperationPrice = operationPrice;
}

public double GetPrice()
{
return OperationPrice.GetPrice(this);
}
}

public enum OrderStatus
{
Active = 1,
DeActive = 2

}
}