没有合适的资源?快使用搜索试试~ 我知道了~
首页与操作容器有关的设计模式
与操作容器有关的设计模式
需积分: 9 9 下载量 51 浏览量
更新于2023-03-03
评论
收藏 206KB PDF 举报
本文从容器操作角度将几种设计模式归为一类.涉及到Composite, Visitor, Iterator模式。文章中有一部分源于网络,已注明。
资源详情
资源评论
资源推荐
与容器操作的设计模式
与容器操作的设计模式有 Composite( 结构型模式 ), Visitor( 行为模式 ),Iterator( 行为模式 )
采用几种设计模式设计容器类对象 , 可大大减少暴露容器对象内部结构和状态的程序 . 从而降
低代码的耦合程序,从而提高稳定性,降低客户使用这个类的复杂程度。
组合( Composite
Composite
Composite
Composite
)模式
组合模式类图及意图见《 23 种模式目录》
组合模式的实例源于 http://www-128.ibm.com/developerworks/cn/java/j-jinfh/ 在此基础上 ,
我将其中的代码修改成 c#.
现在需要 对一个软件产品管理系统的实体建模:某公司开发了一系列软件集
( SoftwareSet
) , 包含了多种品牌 (
Brand
) 的软件产品。 每个品牌下面又有各种产品 (
Pro duct
)。
非面向对象的编程方式。
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
File1.cs
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class SoftwareComponent
{
public virtual void AddComponent( SoftwareComponent component)
{
throw new Exception ( "Can't add component" );
}
}
class AbsSoftwareComposite : SoftwareComponent
{
public override void AddComponent( SoftwareComponent component)
{
if (component != null )
{
ComponentList.Add(component);
}
}
// 为了演示方便 , 暴露内部成员
public List < SoftwareComponent > ComponentList = new List < SoftwareComponent >();
}
/// <summary>
/// 软件产品系列
/// </summary>
class SoftwareSet : AbsSoftwareComposite
{
}
class Brand : AbsSoftwareComposite
{
}
/// <summary>
/// 软件产品
/// </summary>
class Product : SoftwareComponent
{
public Product( double price)
{
_price = price;
}
public double getTotalPrice()
{
return _price;
}
protected double _price;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Fil2.cs
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
SoftwareSet softSet = new SoftwareSet ();
softSet.AddComponent( new Product (9.8));
softSet.AddComponent( new Product (2.2));
double price = GetPrice(softSet);
Console .WriteLine(price);
Console .Read();
}
static double GetPrice( SoftwareComponent component)
{
// 为了得到价格信息 , 暴露了 SoftwareComponent 类及子类的内部结构
// 增加了代码的耦合度。这样的代码不易扩展。 切记代码设计的核心
// 原则:高聚敛,低耦合 . 我们也可以看出,低耦合的前提之一是高聚敛
if (component is SoftwareSet )
{
List < SoftwareComponent > list = (( SoftwareSet )(component)).ComponentList;
double price = 0.0;
foreach ( SoftwareComponent compt in list)
{
if (compt is Product )
{
price += (( Product )(compt)).getTotalPrice();
}
}
return price;
}
else if (component is Product )
{
return (( Product )component).getTotalPrice();
}
else
{
return 0.0;
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
“ 这段代码的好处是实现业务逻辑的时候无需对前面已经定好的数据结构做改动, 并且 效
率比较高; 缺点是代码凌乱而且频繁使用了类型转换, 代码的可读性不强, 如果层次多了 代
剩余12页未读,继续阅读
dreamcs
- 粉丝: 121
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0