Jimmypony的技术汇总区
很多都不会,很多都不懂,不要浮躁,静心学习
posts - 51,comments - 22,views - 75074

代理模式----为另一个对象提供一个替身活占位符以控制对这个对象的访问

head first书上是用JAVA RMI来做的例子,我这边用销售房子 房子--房产商--中介--买家为例说明什么是代理模式

 

复制代码
Code
#region 接口

/// <summary>
/// 购房者
/// </summary>
public interface ICustomer
{
//找中介
void FindProxy(IProxy proxy);
//看房
void LookHouse();
//买房
void BuyHouse(string id,int money);
}

/// <summary>
/// 房产中介
/// </summary>
public interface IProxy
{
//代理房产商
void SetLandAgent(ILandAgent landAgent);
//提供房产信息
void ShowHouse();
void SellHouse(string id,int money);
}

/// <summary>
/// 房产商
/// </summary>
public interface ILandAgent
{
//添加可售房源
void AddHouse(IHouse house);
//显示房产信息
void ShowInfo();
void SoldHouse(string id,int money);
}

/// <summary>
/// 房子
/// </summary>
public interface IHouse
{
//房子信息
void GetInfo();
string Property { get; set; }
}


#endregion


/*
* 在这里房子的代理是房产商
* 房产商的代理是中介
* 买家直接找中介买房子,来改变房子的拥有权
*
*/
#region

public class House : IHouse
{
private string info;
private string property;

public House(string info,string id)
{
this.info = info;
Property
= id;

}
#region IHouse Members

public void GetInfo()
{
Console.WriteLine(
string.Format("房产信息:{0},房产ID:{1}", info,Property));
}

public string Property
{
get{ return property; }
set{ property = value; }
}

#endregion
}

public class LandAgent : ILandAgent
{
private List<IHouse> houses;

public LandAgent()
{
houses
= new List<IHouse>();
}

#region ILandAgent Members

public void AddHouse(IHouse house)
{
houses.Add(house);
}

public void ShowInfo()
{
foreach (IHouse house in houses)
{
house.GetInfo();
}

}

public void SoldHouse(string id, int money)
{
IHouse soldHouse
=new House("","");
foreach (IHouse house in houses)
{
if (house.Property == id)
{
soldHouse
= house;
break;
}
}
if (soldHouse.Property != "")
{
houses.Remove(soldHouse);
Console.WriteLine(
string.Format("我卖出了房子{0}通过中介获得了购房款{1}", id, money));
soldHouse.Property
= "已售出";
soldHouse.GetInfo();
}
else
Console.WriteLine(
"没有找到这房子 @_@");
}

#endregion
}

public class Proxy : IProxy
{
ILandAgent landAgent;

#region IProxy Members

public void SetLandAgent(ILandAgent landAgent)
{
if (landAgent != null)
this.landAgent = landAgent;
}

public void ShowHouse()
{
if (landAgent != null)
landAgent.ShowInfo();
else
Console.WriteLine(
"我还没获得代理房产商的代理权呢");
}

public void SellHouse(string id, int money)
{
Console.WriteLine(
string.Format("我卖出了房子:{0},获得了5%的中介费:{1}", id, money / 20));
landAgent.SoldHouse(id, money
/ 20 * 19);
}

#endregion
}

public class Customer : ICustomer
{
private IProxy proxy;

#region ICustomer Members

public void FindProxy(IProxy proxy)
{
if (proxy != null)
this.proxy = proxy;
}

public void LookHouse()
{
proxy.ShowHouse();
}

public void BuyHouse(string id,int money)
{
proxy.SellHouse(id, money);
}

#endregion
}

#endregion

class Program
{

static void Main(string[] args)
{
ICustomer customer
= new Customer();
IProxy zhongyuandichan
= new Proxy();
ILandAgent wanke
= new LandAgent();

IHouse jinsewanli
= new House("万科金色万里1楼101室,230平米超大私人空间,售价200万", "001");
IHouse huayuanxiaocheng
= new House("万科花园小城13楼602室,104平米二室,售价120万", "002");
IHouse wankejinse
= new House("万科金色万里6楼603室,98平米一室户,售价98万", "003");
IHouse wankeyangguang
= new House("万科阳光10楼1008室,156平米三室两厅,售价132万", "004");

//房子通过房产商来改变拥有属性
wanke.AddHouse(jinsewanli);
wanke.AddHouse(huayuanxiaocheng);
wanke.AddHouse(wankejinse);
wanke.AddHouse(wankeyangguang);
//房子卖不出去了,房产商只好通过销售外包的方式销售房子
zhongyuandichan.SetLandAgent(wanke);
//顾客找到了中介
customer.FindProxy(zhongyuandichan);
//顾客寻求房产信息
customer.LookHouse();
//顾客看中了房子,通过中介对房子的拥有权进行了修改
customer.BuyHouse("003", 980000);
//顾客再次看房
customer.LookHouse();
Console.ReadKey();
}
}
复制代码

 

posted on   Jim~  阅读(352)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
< 2008年9月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示