下面的代码如何改写,移植性高,可重用性高,总之是面对对象的编程

caicoko 2011-05-12 09:40:07
下面是一段升级程序的代码,目前用的是传统的形式,如何改写实现移植性高,可重用性高,总之是面对对象的编程。




using System;

using System.Web;



namespace Caicai.Jingpin.UI.Update

{

public class U_1_2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string action = Request.QueryString["action"];



if (action == "update")

{

this.Update();

}

}



public void Update()

{

//升级文件程序

HttpContext.Current.Response.Write("V1.0升级到V2.0成功!");

HttpContext.Current.Response.Write("<br />");

}

}

}

view sourceprint?using System;

using System.Web;



namespace Caicai.Jingpin.UI.Update

{

public class U_2_3 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string action = Request.QueryString["action"];

string version = Request.QueryString["version"];



if (version == "1")

{

new U_1_2().Update();

}



if (action == "update")

{

this.Update();

}

}



public void Update()

{

//升级文件程序

HttpContext.Current.Response.Write("V2.0升级到V3.0成功!");

HttpContext.Current.Response.Write("<br />");

}

}

}

view sourceprint?using System;

using System.Web;



namespace Caicai.Jingpin.UI.Update

{

public class U_3_4 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string action = Request.QueryString["action"];

string version = Request.QueryString["version"];



if (version == "1")

{

new U_1_2().Update();

new U_2_3().Update();

}



if (version == "2")

{

new U_2_3().Update();

}



if (action == "update")

{

this.Update();

}

}



public void Update()

{

//升级文件程序

HttpContext.Current.Response.Write("V3.0升级到V4.0成功!");

HttpContext.Current.Response.Write("<br />");

}

}

}

view sourceprint?using System;

using System.Web;



namespace Caicai.Jingpin.UI.Update

{

public class U_4_5 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string action = Request.QueryString["action"];

string version = Request.QueryString["version"];



if (version == "1")

{

new U_1_2().Update();

new U_2_3().Update();

new U_3_4().Update();

}



if (version == "2")

{

new U_2_3().Update();

new U_3_4().Update();

}



if (version == "3")

{

new U_3_4().Update();

}



if (action == "update")

{

this.Update();

}

}



public void Update()

{

//升级文件程序

HttpContext.Current.Response.Write("V4.0升级到V5.0成功!");

HttpContext.Current.Response.Write("<br />");

}

}

}

...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
caicoko 2011-05-12
  • 打赏
  • 举报
回复
没有人帮忙吗?
  • 打赏
  • 举报
回复
public void Update() 像这样重复的抽出来。。
其实这是个很好的问题。
lee576 2011-05-12
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 抽象构件
/// </summary>
/// <remarks>给出一个抽象接口,规范被附加方法与属性的接受对象</remarks>
public abstract class Component
{
abstract public void Operation();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体构件
/// </summary>
/// <remarks>Component的实现</remarks>
public class ConcreateComponent : Component
{
public override void Operation()
{
Console.WriteLine("Concreate Component");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器A
/// </summary>
/// <remarks>给Component添加属性AddState</remarks>
public class ConcreateDecoratorA : Decorator
{
private int addState;
public ConcreateDecoratorA(Component component):base(component)
{
}
/// <summary>
/// 附加的属性
/// </summary>
public int AddProperty
{
get
{
return addState;
}
set
{
addState = value;
}
}
public override void Operation()
{
base.Operation();
Console.WriteLine("AddProperty:{0}", AddProperty);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hbb0b0.Decorator
{
/// <summary>
/// 具体装饰器B
/// </summary>
/// <remarks>给Component添加属性AddMethod</remarks>
public class ConcreateDecoratorB : Decorator
{
public ConcreateDecoratorB(Component component):base(component)
{
}
/// <summary>
/// 附加的方法
/// </summary>
public void AddMethod()
{
Console.WriteLine("AddMethod");
}
public override void Operation()
{
base.Operation();
AddMethod();
}
}
}
调用
Component myComponent = new ConcreateComponent();
//给myComponent添加A功能
ConcreateDecoratorA decoratorA = new ConcreateDecoratorA(myComponent);
//给myComponent添加B功能
ConcreateDecoratorB decoratorB = new ConcreateDecoratorB(myComponent);
decoratorA.AddProperty = 100;
Console.WriteLine("给myComponent添加 A属性");
//测试装饰器A
decoratorA.Operation();
Console.WriteLine("给myComponent添加 B方法");
//测试装饰器B
decoratorB.Operation();
//给myComponent添加A,B功能
Console.WriteLine("给myComponent A属性 ,添加 B方法");
decoratorB = new ConcreateDecoratorB(decoratorA);
decoratorB.Operation();
lee576 2011-05-12
  • 打赏
  • 举报
回复
又仔细看了一下,楼主这个可以用“装饰器模式”
不同的版本update方法,写成一个装饰器

装饰模式,是在“继承”之外,扩充和修改类的行为的一种方式。
就是拿一个类A来封装另外一个类B,但没有继承关系
通过采用组合、而非继承的手法, 装饰模式模式实现了在运行时动态地扩展对象功能的能力,而且可以根据需要扩展多个功能。避免了单独使用继承带来的“灵活性差”和“多子类衍生问题”。
设计模式
lee576 2011-05-12
  • 打赏
  • 举报
回复
楼主听说过MVC模式没有?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧