更新时间:2017-05-24 09:11:05浏览次数:1+次
之前学习ASP.NET MVC的时候,接触到了Controller的创建过程,其实就是利用IOC来实现,那会对依赖倒置设计模式很模糊,这次学习Attribute 特性 的时候找到了比较详细的相关资料,做个笔记.
官方解释:高层模块不应该依赖底层模块,两者都应该依赖于抽象;抽象不应该依赖细节,细节应该依赖抽象.//Inventory.cs
using System;
using System.Collections;
namespace NiwalkerDemo
{
public class Inventory
{
private Hashtable inventory=new Hashtable();
public Inventory()
{
inventory["Item1"]=100;
inventory["Item2"]=200;
}
public bool Checkout(string product, int quantity)
{
int qty=GetQuantity(product);
return qty>=quantity;
}
public int GetQuantity(string product)
{
int qty=0;
if(inventory[product]!=null)
qty = (int)inventory[product];
return qty;
}
public void Update(string product, int quantity)
{
int qty=GetQuantity(product);
inventory[product]=qty-quantity;
}
}
}
//Logbook.cs
using System;
namespace NiwalkerDemo
{
public class Logbook
{
public static void Log(string logData)
{
Console.WriteLine("log:{0}",logData);
}
}
}
//Order.cs
using System;
namespace NiwalkerDemo
{
public class Order
{
private int orderId;
private string product;
private int quantity;
public Order(int orderId)
{
this.orderId=orderId;
}
public void Submit()
{
Inventory inventory=new Inventory(); //创建库存对象
//检查库存
if(inventory.Checkout(product,quantity))
{
Logbook.Log("Order"+orderId+" available");
inventory.Update(product,quantity);
}
else
{
Logbook.Log("Order"+orderId+" unavailable");
SendEmail();
}
}
public string ProductName
{
get{ return product; }
set{ product=value; }
}
public int OrderId
{
get{ return orderId; }
}
public int Quantity
{
get{ return quantity;}
set{ quantity=value; }
}
public void SendEmail()
{
Console.WriteLine("Send email to manager");
}
}
}
下面是调用程序:
//AppMain.cs
using System;
namespace NiwalkerDemo
{
public class AppMain
{
static void Main()
{
Order order1=new Order(100);
order1.ProductName="Item1";
order1.Quantity=150;
order1.Submit();
Order order2=new Order(101);
order2.ProductName="Item2";
order2.Quantity=150;
order2.Submit();
}
}
}
public interface IInventoryHandle
{
bool Checkout(string product, int quantity);
int GetQuantity(string product);
void Update(string product, int quantity);
}
public interface IOrderHandle
{
int orderId { get; set; }
string product { get; set; }
int quantity { get; set; }
void Submit();
void SendEmail();
}
调用部分:
public class Inventory : IInventoryHandle
{
public Hashtable inventory = new Hashtable();
public Inventory()
{
inventory["Item1"] = 100;
inventory["Item2"] = 200;
}
public bool Checkout(string product, int quantity)
{
int qty = GetQuantity(product);
return qty >= quantity;
}
public int GetQuantity(string product)
{
int qty = 0;
if (inventory[product] != null)
qty = (int)inventory[product];
return qty;
}
public void Update(string product, int quantity)
{
int qty = GetQuantity(product);
inventory[product] = qty - quantity;
}
}
public class Logbook
{
public static void Log(string logData)
{
Console.WriteLine("log:{0}", logData);
}
}
public class Order : IOrderHandle,IDisposable
{
public IInventoryHandle _inventory;
public Order(IInventoryHandle inventory, int _orderId)
{
_inventory = inventory;
orderId = _orderId;
}
public void Submit()
{
//检查库存
if (_inventory.Checkout(product, quantity))
{
Logbook.Log("Order" + orderId + " available");
_inventory.Update(product, quantity);
}
else
{
Logbook.Log("Order" +orderId + " unavailable");
SendEmail();
}
}
public void SendEmail()
{
Console.WriteLine("Send email to manager");
}
public int orderId
{
get;
set;
}
public string product
{
get;
set;
}
public int quantity
{
get;
set;
}
public void Dispose()
{
}
}
class Program
{
static void Main(string[] args)
{
var _order1 = new Order(new Inventory(),100);
_order1.product = "Item1";
_order1.quantity = 150;
_order1.Submit();
_order1.Dispose();
var _order2 = new Order(new Inventory(), 250);
_order2.product = "Item2";
_order2.quantity = 150;
_order2.Submit();
_order2.Dispose();
Console.Read();
}
}
参考: http://www.cnblogs.com/landeanfen/p/5169163.html http://blog.csdn.net/niwalker/article/details/8871
相关资讯