C#.Net OOP系列之接口设计及策略应用实战
C#.Net OOP系列之接口设计及策略应用实战
OOP编程之策略模式接口设计
接口设计, 策略应用, 对象多态性, 及其它OOP技术在这个演示实例展示。
OOP编程之策略模式接口设计
接口设计, 策略应用, 对象多态性, 及其它OOP技术在这个演示实例展示。
如转载本文请注明出处!
来源:www.CSFramework.com, C/S结构框架学习网
如转载本文请注明出处!
扫一扫加作者微信
来源:www.CSFramework.com, C/S结构框架学习网
//数据显示器接口
public interface IShowCustomer
{
IList GetCustomers(); //函数
void ShowCustomers(IList customers); //方法
void ShowCustomers(); //方法,重载
}
//数据显示器基类
public class ShowerBase : IShowCustomer
{
public virtual IList GetCustomers()
{
return CustomerTester.GetCustomers();
}
//当没有指定数据源,作为预设的显示方法
public void ShowCustomers()
{
IList customers = this.GetCustomers();
//调用模板方法
this.ShowCustomers(customers);
}
//虚方法,用于override
//实现具体的业务逻辑
public virtual void ShowCustomers(IList customers) { }
}
//用Listbox显示
public class ShowListBox : ShowerBase
{
private ListBox _container;
public ShowListBox(ListBox container)
{
_container = container;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override overload
{
_container.Items.Clear();
foreach (object o in customers) _container.Items.Add(o);
}
}
//用表格显示
public class ShowGridView : ShowerBase
{
private DataGridView _gv;
public ShowGridView(DataGridView gv)
{
_gv = gv;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override
{
_gv.DataSource = null;
_gv.DataSource = customers;
}
}
//用TreeView显示
public class ShowTreeView : ShowerBase
{
private TreeView _view;
public ShowTreeView(TreeView view)
{
_view = view;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override
{
_view.Nodes.Clear();
TreeNode n;
foreach (Customer c in customers)
{
n = _view.Nodes.Add(c.CustomerName);
n.Nodes.Add(c.CustomerID);
n.Nodes.Add(c.CustomerAddress);
}
_view.ExpandAll();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
public interface IShowCustomer
{
IList GetCustomers(); //函数
void ShowCustomers(IList customers); //方法
void ShowCustomers(); //方法,重载
}
//数据显示器基类
public class ShowerBase : IShowCustomer
{
public virtual IList GetCustomers()
{
return CustomerTester.GetCustomers();
}
//当没有指定数据源,作为预设的显示方法
public void ShowCustomers()
{
IList customers = this.GetCustomers();
//调用模板方法
this.ShowCustomers(customers);
}
//虚方法,用于override
//实现具体的业务逻辑
public virtual void ShowCustomers(IList customers) { }
}
//用Listbox显示
public class ShowListBox : ShowerBase
{
private ListBox _container;
public ShowListBox(ListBox container)
{
_container = container;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override overload
{
_container.Items.Clear();
foreach (object o in customers) _container.Items.Add(o);
}
}
//用表格显示
public class ShowGridView : ShowerBase
{
private DataGridView _gv;
public ShowGridView(DataGridView gv)
{
_gv = gv;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override
{
_gv.DataSource = null;
_gv.DataSource = customers;
}
}
//用TreeView显示
public class ShowTreeView : ShowerBase
{
private TreeView _view;
public ShowTreeView(TreeView view)
{
_view = view;
}
//具体的业务逻辑
public override void ShowCustomers(IList customers) //方法,复写override
{
_view.Nodes.Clear();
TreeNode n;
foreach (Customer c in customers)
{
n = _view.Nodes.Add(c.CustomerName);
n.Nodes.Add(c.CustomerID);
n.Nodes.Add(c.CustomerAddress);
}
_view.ExpandAll();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
如转载本文请注明出处!
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网