Jimmypony的技术汇总区
很多都不会,很多都不懂,不要浮躁,静心学习

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

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 @ 2008-09-30 00:06 Jim~ 阅读(352) 评论(0) 推荐(0) 编辑
摘要: 1.单件模式----通过一次性构造全局唯一的对象2.工厂模式----获取对象而不用关心他具体是什么,全由子类决定3.适配器模式----将一个接口转化为另一个,以达到兼容的目的4.装饰者模式----不改变接口,增加责任,并能多次重复添加5.外观模式----将一群对象转化为接口,达到简单使用的目的6.观察者模式----对象发生变化,观察者都会得到通知7.命令模式----将需求的多种执行者进行封装,以方... 阅读全文
posted @ 2008-09-28 15:32 Jim~ 阅读(670) 评论(1) 推荐(0) 编辑
摘要: 感觉就是策略模式强化版,但是绝非简单的通过状态来管理行为之间的逻辑关系,而是把行为当作状态,简单的说在什么状态下决定执行什么行为是有效的,同时改变下一行为(状态),还有一点是所有代码中没有IF,让当前行为决定做那些事情是有效的.书上是这么说的:允许对象在内部状态改变是改变他的行为,对象看起来好像是修改了它的类.以下以冰箱里面有一头鹿怎样把大象放进冰箱的状态关系为例说明:打开冰箱把鹿拿出冰箱把大象塞... 阅读全文
posted @ 2008-09-28 14:30 Jim~ 阅读(347) 评论(0) 推荐(0) 编辑
摘要: 所谓组合模式----允许将对象组合形成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象集合个人认为Xml数据的格式就很类似于组合模式,而XmlNode类适合于组合模式的描述,以下我将以长江源头描述起,来说明组合模式[代码] 阅读全文
posted @ 2008-09-27 16:10 Jim~ 阅读(272) 评论(0) 推荐(0) 编辑
摘要: 所谓迭代器模式,就是实现一个迭代器接口,从而实现对通用对象组的遍历,而又不暴露内部结构以下以鸭子和虫子为例说明:[代码] 阅读全文
posted @ 2008-09-27 10:31 Jim~ 阅读(263) 评论(0) 推荐(0) 编辑
摘要: 模板模式:个人认为模板模式可以很好的对不同类似对象复用,从而达到减少代码量和易于维护的目的。书上是这么说的:在一个方法中定义算法骨架,而将一些步骤延迟到子类中,末班方法使得子类在不改变算法结构的情况下,重新定义算法中的某些步骤。模板模式使用的原则------好莱坞原则 低层不调用高层 高层会调用低层和依赖倒转原则,两者皆依赖于抽象相比有什么不同呢?  个人认为2者都是为了达到解耦的目的才存在的,而... 阅读全文
posted @ 2008-09-26 14:43 Jim~ 阅读(330) 评论(0) 推荐(0) 编辑
摘要: 跳转页面asp.net提供的三种方法比较 1 response.redirect 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳 转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意... 阅读全文
posted @ 2008-09-23 09:06 Jim~ 阅读(1746) 评论(0) 推荐(0) 编辑
摘要: 适配器模式和外观模式:适配器模式:所谓适配器模式就是将不兼容的接口想方设法让他兼容,通过继承目标接口的方式来实现,达到兼容的目的,或者说是改变接口。个人对适配器模式产生的理解:在后续的代码修改和功能扩充的过程中需要通过某个接口的转换,以达到版本的兼容。以下的例子可能不是很恰当,但是能说明问题:[代码]外观模式和适配器模式很像,但是他的目的并非是兼容,而是简单适用将一群对象转换为一个简单接口,以方便... 阅读全文
posted @ 2008-09-19 18:47 Jim~ 阅读(254) 评论(0) 推荐(0) 编辑
摘要: 命令模式:把需求的请求者和需求的执行者从对象中解耦出来,我个人的理解是把请求者和执行者分离,执行者封装复杂的执行过程,通过请求者的指示找到不同的执行者来处理或者执行不同的过程。以下以命令者 命令传递者 命令执行者为例,解释妈妈喊起床的过程:[代码][代码] 阅读全文
posted @ 2008-09-19 16:24 Jim~ 阅读(270) 评论(0) 推荐(0) 编辑
摘要: 单件模式:显而易见,单件模式就是只有一个,并且通过私有构造器的方式不能被继承(继承了就不是正真意义上的单件了),在这一点上和静态类是一致的,但是还是有区别的主要是以下2点:初始化时间,静态类在编译时初始化,单件类在需要时再初始化静态类没有构造器,但是单件有一个一次性的构造器,可以根据不同情况进行一次性的复杂构造 当某个类并不需要由构造器初始化并且不在乎初始化的时间时,2者可以混用!以下是单件模式的... 阅读全文
posted @ 2008-09-19 13:54 Jim~ 阅读(272) 评论(0) 推荐(0) 编辑
< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5

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