45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:C#设计模式之合成模式介绍

C#设计模式之合成模式介绍

2016-09-03 03:34:40 来源:www.45fan.com 【

C#设计模式之合成模式介绍

Composite模式定义了单个对象和组合对象的类层次结构。(1)单个对象组合成复杂的组合对象,而多个组合对象又可以被组合成一个新的组合对象。其中,一个组合对象中的某些对象可能包容了其他对象。某些对象则表现单个基本对象,称为叶对象;某些对象代表一组对象,称为枝对象。叶对象可以组合成复杂的枝对象,而枝对象又可以被组合成新的枝对象,可以不断组合下去。

Composite模式使用户对单个对象和组合对象的使用具有一致性。客户可以一致地使用组合结构和单个对象,而不用关心处理的是一个叶节点还是一个组合组件。Composite关键是一个抽象类,这个类既可以代表一个对象,也可以代表一个容器来包含一组对象。(1)那么。Composite关键两个思想:

一、一个Composite对象既可包容Leaf对象,也可以其他包容Composite对象。

二、一个Leaf类和一个Composite共享一个在抽象类中定义的公共接口Component。(2)

在以下情况下考虑使用合成模式:

(1)表示对象的部分和整体层次结构

(2)忽略枝对象和叶对象的不同,用户同意使用组合结构的所有对象。

结构图如下:

合成模式的实现根据公共接口Component的区别分为两种形式,分别称为安全模式和透明模式。

透明模式:

公共接口Component中里面声明所有的用来管理子类对象的方法,包括Add()、Remove(),以及GetChild()方法。这样所有的叶对象和枝对象都有相同的接口,那么客户端可以将叶对象和枝对象同等对待而不用区分。

示例代码如下:

C#设计模式之合成模式介绍usingSystem;

C#设计模式之合成模式介绍
usingSystem.Collections.Generic;

C#设计模式之合成模式介绍
usingSystem.Text;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍
namespaceComposite

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///透明模式

C#设计模式之合成模式介绍
///一个Leaf类和一个Composite共享一个在抽象类中定义的公共接口

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍publicinterfaceITransparence

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得学生的总成绩

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns>返回学生的总成绩</returns>

C#设计模式之合成模式介绍intGetStudentScores();

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得学生的所有科目名称

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns>返回学生的所有科目名称</returns>

C#设计模式之合成模式介绍stringGetStudentSubjects();

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///增加叶节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<paramname="transparence">接口类型对象</param>

C#设计模式之合成模式介绍///<returns>操作是否成功</returns>

C#设计模式之合成模式介绍boolAdd(ITransparencetransparence);

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///移除叶节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<paramname="transparence">接口类型对象</param>

C#设计模式之合成模式介绍///<returns>操作是否成功</returns>

C#设计模式之合成模式介绍boolRemove(ITransparencetransparence);

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得所有的子节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns></returns>

C#设计模式之合成模式介绍List<ITransparence>GetChild();

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///学生叶子节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍publicclassStudentLeaf:ITransparence

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///语文

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privatestring_chinese;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///数学

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privatestring_math;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///英语

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privatestring_english;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///语文成绩

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privateint_chineseScore;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///数学成绩

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privateint_mathScore;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///英语成绩

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍privateint_englishScore;

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///构造函数:语文,数学,英语的中文名称和它的分数

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<paramname="chinese"></param>

C#设计模式之合成模式介绍///<paramname="math"></param>

C#设计模式之合成模式介绍///<paramname="english"></param>

C#设计模式之合成模式介绍publicStudentLeaf(stringchinese,stringmath,stringenglish,intchineseScore,intmathScore,intenglishScore)

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍_chinese
=chinese;

C#设计模式之合成模式介绍_math
=math;

C#设计模式之合成模式介绍_english
=english;

C#设计模式之合成模式介绍_chineseScore
=chineseScore;

C#设计模式之合成模式介绍_mathScore
=mathScore;

C#设计模式之合成模式介绍_englishScore
=englishScore;

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得某个学生总成绩

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns></returns>

C#设计模式之合成模式介绍publicintGetStudentScores()

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍
intscores=_chineseScore+_mathScore+_englishScore;

C#设计模式之合成模式介绍
returnscores;

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得某个学生的所有科目名称

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns></returns>

C#设计模式之合成模式介绍publicstringGetStudentSubjects()

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍StringBuildersb
=newStringBuilder();

C#设计模式之合成模式介绍sb.Append(
"语文:");

C#设计模式之合成模式介绍sb.Append(_chinese);

C#设计模式之合成模式介绍sb.Append(
" ");

C#设计模式之合成模式介绍sb.Append(
"数学:");

C#设计模式之合成模式介绍sb.Append(_math);

C#设计模式之合成模式介绍sb.Append(
" ");

C#设计模式之合成模式介绍sb.Append(
"英语:");

C#设计模式之合成模式介绍sb.Append(_english);

C#设计模式之合成模式介绍sb.Append(
" ");

C#设计模式之合成模式介绍
returnsb.ToString();

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///增加叶节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<paramname="transparence">接口类型对象</param>

C#设计模式之合成模式介绍///<returns>操作是否成功</returns>

C#设计模式之合成模式介绍publicboolAdd(ITransparencetransparence)

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍
//叶子节点不能使用该方法

C#设计模式之合成模式介绍returnfalse;

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///移除叶节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<paramname="transparence">接口类型对象</param>

C#设计模式之合成模式介绍///<returns>操作是否成功</returns>

C#设计模式之合成模式介绍publicboolRemove(ITransparencetransparence)

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍
//叶子节点不能使用该方法

C#设计模式之合成模式介绍returnfalse;

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///获得所有的子节点

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍///<returns></returns>

C#设计模式之合成模式介绍publicList<ITransparence>GetChild()

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
{

C#设计模式之合成模式介绍
returnnull;

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍}

C#设计模式之合成模式介绍

C#设计模式之合成模式介绍C#设计模式之合成模式介绍
///<summary>

C#设计模式之合成模式介绍///学生合成类

C#设计模式之合成模式介绍
///</summary>

C#设计模式之合成模式介绍publicclassStudentComposite:ITransparence

本文地址:http://www.45fan.com/a/question/71502.html
Tags: 模式 合成 Composite
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部