Jimmypony的技术汇总区
很多都不会,很多都不懂,不要浮躁,静心学习
posts - 52,comments - 11,trackbacks - 0

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

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~ 阅读(35) | 评论 (0)编辑

1.单件模式----通过一次性构造全局唯一的对象

2.工厂模式----获取对象而不用关心他具体是什么,全由子类决定

3.适配器模式----将一个接口转化为另一个,以达到兼容的目的

4.装饰者模式----不改变接口,增加责任,并能多次重复添加

5.外观模式----将一群对象转化为接口,达到简单使用的目的

6.观察者模式----对象发生变化,观察者都会得到通知

7.命令模式----将需求的多种执行者进行封装,以方便需求者一次性调用

8.迭代器模式----提供一个方法来遍历集合,而不用关心集合的实现

9.组合模式----客户可以将对象的集合以及对象一视同仁来处理

10.策略模式----封装可以互换的行为,并用委托来决定需要那种行为

11.状态模式----封装基于状态的行为,由当前状态决定采用那种行为

 待续....

posted @ 2008-09-28 15:32 Jim~ 阅读(35) | 评论 (0)编辑

感觉就是策略模式强化版,但是绝非简单的通过状态来管理行为之间的逻辑关系,而是把行为当作状态,简单的说在什么状态下决定执行什么行为是有效的,同时改变下一行为(状态),还有一点是所有代码中没有IF,让当前行为决定做那些事情是有效的.

书上是这么说的:允许对象在内部状态改变是改变他的行为,对象看起来好像是修改了它的类.

以下以冰箱里面有一头鹿怎样把大象放进冰箱的状态关系为例说明:

  1. 打开冰箱
  2. 把鹿拿出冰箱
  3. 把大象塞进去
  4. 把冰箱门关上
Code
#region 接口

/// <summary>
/// 动物接口
/// </summary>
public interface IAnimal
{
String Name {
get; }
}

/// <summary>
/// 行为接口
/// </summary>
public interface IAction
{
void AddAnimal(IAnimal animal);
void RemoveAnimal(IAnimal animal);
void OpenIceBox();
void ClosedIceBox();

}

/// <summary>
/// 冰箱状态
/// </summary>
public interface IIceBox
{
IAction Status {
get; set; }
IAnimal Animal {
get; set; }

IAction OpenIcebox {
get; set; }
IAction InsertAnimal {
get; set; }
IAction DeleteAnimal {
get; set; }
IAction CloseIcebox {
get; set; }
}

#endregion

#region Status

/// <summary>
/// 打开冰箱
/// </summary>
public class OpenIcebox : IAction
{
IIceBox icebox;
public OpenIcebox(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"请先打开冰箱!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"请先打开冰箱!");
}

public void OpenIceBox()
{
Console.WriteLine(
"我打开了冰箱!");
icebox.Status
= icebox.DeleteAnimal;
}

public void ClosedIceBox()
{
Console.WriteLine(
"冰箱还没打开呢!");
}

#endregion
}
/// <summary>
/// 放入动物
/// </summary>
public class InsertAnimal : IAction
{
IIceBox icebox;
public InsertAnimal(IIceBox icebox)
{
this.icebox = icebox;
}
#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
string.Format("把{0}塞入了冰箱!", animal.Name));
icebox.Animal
= animal;
icebox.Status
= icebox.CloseIcebox;
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱里面没动物啊!");
}

public void OpenIceBox()
{
Console.WriteLine(
"冰箱已经打开了,不能重复打开!");
}

public void ClosedIceBox()
{
Console.WriteLine(
"还没放动物呢!!");
}

#endregion
}
/// <summary>
/// 拿出动物
/// </summary>
public class DeleteAnimal : IAction
{
IIceBox icebox;
public DeleteAnimal(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱里面有动物了啊!!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
string.Format("移除动物{0}", animal.Name));
icebox.Animal
= null;
icebox.Status
= icebox.InsertAnimal;
}

public void OpenIceBox()
{
Console.WriteLine(
"冰箱已经打开了,不能重复打开!");
}

public void ClosedIceBox()
{
Console.WriteLine(
"还没有移除动物呢");
}

#endregion
}
/// <summary>
/// 关闭冰箱
/// </summary>
public class CloseIcebox : IAction
{
IIceBox icebox;
public CloseIcebox(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱已经塞了动物了!!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"现在应该关门了");
}

public void OpenIceBox()
{
Console.WriteLine(
"门是开着的,不能重复打开");
}

public void ClosedIceBox()
{
Console.WriteLine(
"关上了冰箱门");
icebox.Status
= icebox.OpenIcebox;
}

#endregion
}

#endregion

#region 实体

public class Elephant : IAnimal
{

#region IAnimal Members

public string Name
{
get { return "大象"; }
}

#endregion
}

public class Deer : IAnimal
{
#region IAnimal Members

public string Name
{
get { return "中国麋鹿"; }
}

#endregion
}

public class IceBox : IIceBox
{
IAction openIcebox;

public IAction OpenIcebox
{
get { return openIcebox; }
set { openIcebox = value; }
}
IAction insertAnimal;

public IAction InsertAnimal
{
get { return insertAnimal; }
set { insertAnimal = value; }
}
IAction deleteAnimal;

public IAction DeleteAnimal
{
get { return deleteAnimal; }
set { deleteAnimal = value; }
}
IAction closeIcebox;

public IAction CloseIcebox
{
get { return closeIcebox; }
set { closeIcebox = value; }
}

IAction status;

IceBox icebox;
IAnimal animal;

public IceBox()
{
openIcebox
= new OpenIcebox(this);
insertAnimal
= new InsertAnimal(this);
deleteAnimal
= new DeleteAnimal(this);
closeIcebox
= new CloseIcebox(this);

status
= openIcebox;
animal
= new Deer();
}

#region IIceBox Members

public IceBox Status
{
get { return icebox; }
set { icebox = value; }
}

public IAnimal Animal
{
get { return animal; }
set { animal = value; }
}

IAction IIceBox.Status
{
get
{
return status; }
set
{ status
= value; }
}

#endregion

public void Open()
{
status.OpenIceBox();
}
public void Remove(IAnimal animal)
{
status.RemoveAnimal(animal);
}
public void Add(IAnimal animal)
{
status.AddAnimal(animal);
}
public void Close()
{
status.ClosedIceBox();
}

}

#endregion



class Program
{
static void Main(string[] args)
{
Elephant e_1
= new Elephant();
Deer d_1
= new Deer();

IceBox icebox
= new IceBox();

icebox.Open();
Console.WriteLine(
"----------尝试错误第二次的打开门-----------");
icebox.Open();
icebox.Remove(d_1);
Console.WriteLine(
"----------尝试错误再移除大象-----------");
icebox.Remove(d_1);
icebox.Add(e_1);
Console.WriteLine(
"----------尝试错误再添加一头大象-----------");
icebox.Add(e_1);
icebox.Close();
Console.WriteLine(
"----------再关一次门-----------");
icebox.Close();
Console.WriteLine(
"----------关门放大象试试-----------");
icebox.Add(e_1);
Console.Read();
}
}

 

posted @ 2008-09-28 14:30 Jim~ 阅读(21) | 评论 (0)编辑

所谓组合模式----允许将对象组合形成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象集合

个人认为Xml数据的格式就很类似于组合模式,而XmlNode类适合于组合模式的描述,以下我将以长江源头描述起,来说明组合模式

 

Code
/// <summary>
/// 组合模式 抽象类
/// </summary>
public abstract class Component
{
public abstract string GetInfo();
public abstract List<Component> GetChilds();
public abstract void AddChild(Component com);
public abstract void RemoveChild(Component com);

/// <summary>
/// 打印全部信息
/// </summary>
public void PrintAll()
{
Print(
this,0);
}

/// <summary>
/// 递归方法遍历所有
/// </summary>
/// <param name="com"></param>
/// <param name="i"></param>
private void Print(Component com,int i)
{
Console.WriteLine(GetTab(i)
+ com.GetInfo());
List
<Component> childs =com.GetChilds();
if (childs != null)
{
foreach (Component list in childs)
Print(list,
++i);
}
}

/// <summary>
/// 这里是tab 为了使得层次更加清晰
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private string GetTab(int i)
{
string tab="";
for (int j = 0; j < i; j++)
tab
+= "\t";
return tab;
}
}

/// <summary>
/// 河 可以有分支溪流组成
/// </summary>
public class River : Component
{
List
<Component> Rivers;
string info;

public River(string info)
{
Rivers
= new List<Component>();
this.info = info;
}

public override string GetInfo()
{
return info;
}

public override List<Component> GetChilds()
{
return this.Rivers;
}

public override void AddChild(Component com)
{
Rivers.Add(com);
}

public override void RemoveChild(Component com)
{
Rivers.Remove(com);
}
}

/// <summary>
/// 溪流 没有分支啦
/// </summary>
public class Rivulet : Component
{
string info;

public Rivulet(string info)
{
this.info = info;
}

public override string GetInfo()
{
return info;
}

public override List<Component> GetChilds()
{
return null;
}

#region 以下2个方法不实现

public override void AddChild(Component com)
{
throw new NotImplementedException();
}

public override void RemoveChild(Component com)
{
throw new NotImplementedException();
}

#endregion

}


class Program
{
static void Main(string[] args)
{
River changjiang
= new River("长江");
River yalongjiang
= new River("雅砻江");
River minjiang
= new River("岷江");

Rivulet tuotuohe
= new Rivulet("沱沱河");
Rivulet tongtianhe
= new Rivulet("通天河");
Rivulet daduhe
= new Rivulet("大渡河");
Rivulet jialingjiang
= new Rivulet("嘉陵江");
Rivulet wujiang
= new Rivulet("乌江");
Rivulet yuanjiang
= new Rivulet("沅江");
Rivulet xiangjiang
= new Rivulet("湘江");
Rivulet hanjiang
= new Rivulet("汉江");
Rivulet ganjiang
= new Rivulet("赣江");
Rivulet huangpujiang
= new Rivulet("黄浦江");

yalongjiang.AddChild(daduhe);
yalongjiang.AddChild(jialingjiang);
yalongjiang.AddChild(wujiang);

minjiang.AddChild(yuanjiang);
minjiang.AddChild(xiangjiang);
minjiang.AddChild(hanjiang);
minjiang.AddChild(ganjiang);

changjiang.AddChild(tuotuohe);
changjiang.AddChild(tongtianhe);
changjiang.AddChild(yalongjiang);
changjiang.AddChild(minjiang);
changjiang.AddChild(huangpujiang);

changjiang.PrintAll();

Console.Read();
}
}

 

posted @ 2008-09-27 16:10 Jim~ 阅读(23) | 评论 (0)编辑

所谓迭代器模式,就是实现一个迭代器接口,从而实现对通用对象组的遍历,而又不暴露内部结构

以下以鸭子和虫子为例说明:

 

Code
/// <summary>
/// 迭代器模式接口
/// </summary>
public interface Iterator
{
bool HasNext();
object Next();
}

/// <summary>
/// 一种带数组成员类
/// </summary>
public class Worms : Iterator
{
string[] worms;
int nums = -1;

public Worms(string[] worms)
{
this.worms = worms;
}

#region Iterator Members

public bool HasNext()
{
if (worms.Length > nums + 1)
{
nums
+= 1;
return true;
}
else
return false;
}

public object Next()
{
return worms[nums];
}

#endregion
}


/// <summary>
/// 一种带泛型成员类
/// </summary>
public class Duck : Iterator
{
List
<string> ducks;
int nums = -1;

public Duck(List<string> ducks)
{
this.ducks = ducks;
}
#region Iterator Members

public bool HasNext()
{
if (ducks.Count > nums+1)
{
nums
+= 1;
return true;
}
else
return false;
}

public object Next()
{
return ducks[nums];
}

#endregion
}


public class Info
{
Worms worms;
Duck ducks;

public Info(Worms worms,Duck ducks)
{
this.worms = worms;
this.ducks = ducks;
}

public void ReadIterator(string type)
{
if (type == "ducks")
Read(ducks);
if (type == "worms")
Read(worms);
}

//统一遍历接口
private void Read(Iterator i)
{
while(i.HasNext())
Console.WriteLine(i.Next().ToString());
}
}


class Program
{
static void Main(string[] args)
{
Worms worms
= new Worms(new string[] { "毛毛虫", "鼻涕虫", "蚂蚁", "苍蝇", "蚊子", "瓢虫", "蟋蟀", "蜈蚣" });
Duck ducks
= new Duck(new List<string> { "鸳鸯", "", "黄头鸭", "北京填鸭", "绿头鸭", "白头鸭", "黑鸭鸭", "白鸭鸭", "长嘴鸭", "无毛鸭" });

Info info
= new Info(worms, ducks);

info.ReadIterator(
"ducks");
Console.WriteLine(
"-------------华丽的分割线----------------");
info.ReadIterator(
"worms");

Console.Read();
}
}

 

 

posted @ 2008-09-27 10:31 Jim~ 阅读(19) | 评论 (0)编辑

模板模式:

个人认为模板模式可以很好的对不同类似对象复用,从而达到减少代码量和易于维护的目的。

书上是这么说的:在一个方法中定义算法骨架,而将一些步骤延迟到子类中,末班方法使得子类在不改变算法结构的情况下,重新定义算法中的某些步骤。

模板模式使用的原则------好莱坞原则 低层不调用高层 高层会调用低层

和依赖倒转原则,两者皆依赖于抽象相比有什么不同呢?

  个人认为2者都是为了达到解耦的目的才存在的,而依赖倒转是设计类时的原则,好莱坞原则则是编程习惯或者是一种技巧吧 我觉得。

以下以制作DQ暴风雪冰激凌为例实现模板模式的过程:

 

Code
public abstract class MakeDQ
{
public void Make()
{
#region 模板必选项
SelectCup();
AddIceCream();
#endregion

#region 模板可选项
if (hook())
AddIngredient();
#endregion

#region 模板必选选项
Mix();
GiveToCustomer();
#endregion

}


#region 通用方法

/// <summary>
/// 添加主料
/// </summary>
private void AddIceCream()
{
Console.WriteLine(
"加入白奶冰激凌原料");
}
/// <summary>
/// 搅拌过程
/// </summary>
private void Mix()
{
Console.WriteLine(
"将冰激淋带杯子放入机器进行搅拌~~");
}
/// <summary>
/// 交付过程
/// </summary>
private void GiveToCustomer()
{
Console.WriteLine(
"将暴风雪冰激淋在客人面前倒置下下,交给客人!");
}

#endregion

#region 由子类来实现的抽象方法

/// <summary>
/// 选择杯子