代理模式----为另一个对象提供一个替身活占位符以控制对这个对象的访问
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~ 阅读(123) 评论(0)
编辑
1.单件模式----通过一次性构造全局唯一的对象
2.工厂模式----获取对象而不用关心他具体是什么,全由子类决定
3.适配器模式----将一个接口转化为另一个,以达到兼容的目的
4.装饰者模式----不改变接口,增加责任,并能多次重复添加
5.外观模式----将一群对象转化为接口,达到简单使用的目的
6.观察者模式----对象发生变化,观察者都会得到通知
7.命令模式----将需求的多种执行者进行封装,以方便需求者一次性调用
8.迭代器模式----提供一个方法来遍历集合,而不用关心集合的实现
9.组合模式----客户可以将对象的集合以及对象一视同仁来处理
10.策略模式----封装可以互换的行为,并用委托来决定需要那种行为
11.状态模式----封装基于状态的行为,由当前状态决定采用那种行为
待续....
posted @ 2008-09-28 15:32 Jim~ 阅读(188) 评论(1)
编辑
感觉就是策略模式强化版,但是绝非简单的通过状态来管理行为之间的逻辑关系,而是把行为当作状态,简单的说在什么状态下决定执行什么行为是有效的,同时改变下一行为(状态),还有一点是所有代码中没有IF,让当前行为决定做那些事情是有效的.
书上是这么说的:允许对象在内部状态改变是改变他的行为,对象看起来好像是修改了它的类.
以下以冰箱里面有一头鹿怎样把大象放进冰箱的状态关系为例说明:
- 打开冰箱
- 把鹿拿出冰箱
- 把大象塞进去
- 把冰箱门关上

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~ 阅读(84) 评论(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~ 阅读(58) 评论(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~ 阅读(53) 评论(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>
/// 选择杯子
/// </summary>
public abstract void SelectCup();
/// <summary>
/// 添加配料
/// </summary>
public abstract void AddIngredient();
#endregion
#region 可选项,由子类决定是否覆盖
/// <summary>
/// 这里是个钩子来说明可选项,如果要添加辅料,返回true就可以了哦
/// </summary>
/// <returns></returns>
public virtual bool hook()
{
return false;
}
#endregion
}
/// <summary>
/// 双倍抹茶大碗DQ冰激淋
/// </summary>
public class Mochadabei : MakeDQ
{
public override void SelectCup()
{
Console.WriteLine("服务生拿了个大瓦缸 -_-!");
}
public override void AddIngredient()
{
Console.WriteLine("添加了双份抹茶");
}
public override bool hook()
{
return true;
}
}
/// <summary>
/// 小白杯DQ,不加辅料
/// </summary>
public class Xiaobaibei : MakeDQ
{
public override void SelectCup()
{
Console.WriteLine("服务生拿了个小杯");
}
public override void AddIngredient()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
Mochadabei m = new Mochadabei();
Xiaobaibei x = new Xiaobaibei();
m.Make();
Console.WriteLine("--------------帅气的分割线--------------");
x.Make();
Console.Read();
}
}

posted @ 2008-09-26 14:43 Jim~ 阅读(118) 评论(0)
编辑
跳转页面asp.net提供的三种方法比较
1 response.redirect 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳 转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session。
2 server.transfer 速度快,只需要一次postback ,但是。。。。他必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。你可以写个小程序试试:设计一个由页面一到页面二的跳转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!
3 sever.execute 这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。
总结:
当需要把用户跳转到另一台服务器上的页面的时候 使用redirect
当需要把用户跳转到非aspx页面时候,如html 使用redirect
需要把查询字符串作为url一部分的时候保留传给服务器的时候,因为其他2种方法不能做到2次postback,把数据先带回服务器 使用redirect
需要aspx页面间的转换(不涉及登录) 使用transfer
当需要把aspx页面的输出结果插入到另一个aspx页面的时候使用 execute方法。
当然,忘记了还有一个超链接!当然这个就不需要讲太多了。他是在党需要用户来决定什么时候跳转页面,就使用超链接。
顺便提一下,如何使用redirect方法在查询字符串中使用汉字,因为经常的情况是出现乱码,原因是url不支持汉字。这个时候需要转换:
string message =server.urlencode("欢迎来到赛跑专栏");
先转换,在使用查询字符串
response.redirect("webform2.aspx?msg="+message);
asp.net实现从一个页面跳转到另一页面方法的比较
实现页面跳转的方法很多,除了服务器控件是asp.net封装过外,其它在asp中也就有了。
这么多实现方法中,都是实现同样的功能,为什么还要支持如此多的方法呢,我们实际应用
中到底应该选择哪一种方法呢?也许我们大多数人都是需要用跳转时,随便想到一个便拿来用,
从不去考虑它们会有不同,其实之前我也是这样的,对他们没有一个系统的了解是不能发挥它
们各自的优势的。好,现在让我们来了解一下它们各自的用途吧
一、超键接
这就不说了,最简单的。
二、服务器控件
HyperLink
通过NavigateUrl属性来指定要跳转的url。
主要用于由用户来决定何时转换页面,但是用程序来控制转换的目标。
三、程序控制
1、Response.Redirect()
服务器--------》通知客户端----------》请求跳转到服务器端
主要用于需要将查询字符串作为url的一部分传递给另一页面,或跳转的页面是html页面
2、Server.Transfer()
使用此方法最大的特点是url仍然是跳转前的页面的url。
主要用于需要将执行流程转到同一web服务器的另一aspx页面时用。因为它在跳转后保留了
request和session值,并且可以使用跳转前页面的数据。
3、Server.Execute()
此方法执行完后程序控制权会返回到跳转代码的下一代码,相当于我们的模态窗口,此时可以得到
跳转过页面的返回值。
常 用于辅助选择操作,然后返回选择后的值到当前页面。
有了上面的介绍,我们就应该选择适当的时候用不同的跳转方法。
asp.net网页中的四种页面跳转技术的比较
在asp.net中有四种页面跳转导航方式,该如何为你的页面选择一个呢?
·如果要让用户来决定何时转换页面以及转到哪一个页面,超级链接最适合。
·如果要用程序来控制转换的目标,但转换的时机由用户决定,使用Web服务器的HyperLink控件,动态设置其NavigateUrl属性。
·如果要把用户连接到另一台服务器上的资源,使用Response.Redirect。
·用Response.Redirect把用户连接到非ASPX的资源,例如HTML页面。
·如果要将查询字符串作为URL的一部分保留,使用Response.Redirect。
·如果要将执行流程转入同一Web服务器的另一个ASPX页面,应当使用Server.Transfer而不是Response.Redirect,因为Server.Transfer能够避免不必要的网络通信,从而获得更好的性能和浏览效果。
·如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。
·如果要确保HTML输出合法,请使用Response.Redirect,不要使用Server.Transfer或Server.Execute方法。
关于Server.Execute,
这种页面导航方式类似于针对ASPX页面的一次函数调用,被调用的页面能够访问发出调用页面的表单数据和查询字符串集合,所以要把被调用页面Page指令的EnableViewStateMac属性设置成False。
默认情况下,被调用页面的输出追加到当前应答流。但是,Server.Execute方法有一个重载的方法,允许通过一个TextWriter对象(或者它的子对象,例如StringWriter对象)获取被调用页面的输出,而不是直接追加到输出流,这样,在原始页面中可以方便地调整被调用页面输出结果的位置。
posted @ 2008-09-23 09:06 Jim~ 阅读(1429) 评论(0)
编辑
摘要: 适配器模式和外观模式:适配器模式:所谓适配器模式就是将不兼容的接口想方设法让他兼容,通过继承目标接口的方式来实现,达到兼容的目的,或者说是改变接口。个人对适配器模式产生的理解:在后续的代码修改和功能扩充的过程中需要通过某个接口的转换,以达到版本的兼容。以下的例子可能不是很恰当,但是能说明问题:[代码]外观模式和适配器模式很像,但是他的目的并非是兼容,而是简单适用将一群对象转换为一个简单接口,以方便...
阅读全文
posted @ 2008-09-19 18:47 Jim~ 阅读(70) 评论(0)
编辑
摘要: 命令模式:把需求的请求者和需求的执行者从对象中解耦出来,我个人的理解是把请求者和执行者分离,执行者封装复杂的执行过程,通过请求者的指示找到不同的执行者来处理或者执行不同的过程。以下以命令者 命令传递者 命令执行者为例,解释妈妈喊起床的过程:[代码][代码]
阅读全文
posted @ 2008-09-19 16:24 Jim~ 阅读(64) 评论(0)
编辑
摘要: 单件模式:显而易见,单件模式就是只有一个,并且通过私有构造器的方式不能被继承(继承了就不是正真意义上的单件了),在这一点上和静态类是一致的,但是还是有区别的主要是以下2点:初始化时间,静态类在编译时初始化,单件类在需要时再初始化静态类没有构造器,但是单件有一个一次性的构造器,可以根据不同情况进行一次性的复杂构造 当某个类并不需要由构造器初始化并且不在乎初始化的时间时,2者可以混用!以下是单件模式的...
阅读全文
posted @ 2008-09-19 13:54 Jim~ 阅读(72) 评论(0)
编辑