设计模式学习笔记八——Composite模式

本文解析了组合模式的应用场景及其实现方式,通过将对象组织成树形结构,解决了客户代码过度依赖对象容器内部实现的问题,提高了代码的可维护性和扩展性。
动机:客户代码过多地依赖于对象容器复杂的内部实现结构,对象容器内部实现结构(而非抽象接口)的变化将引起客户代码的频繁变化,带来了代码维护性、扩展性等弊端。本模式通过将对象组合成树形结构以表示“部分-整体”的层次结构,让组合对象实现自身的复杂结构,使得用户对单个对象和组合对象的使用具有一致性。

应用:ASP.NET子父控件关系。

场景:以树为例,叶子为最低级原子节点,树为容器,可以包括子树和叶子。需要使客户程序对树和叶子的处理一致,保持透明性,而不需要关心处理的是树还是叶子。

结构

Composite设计模式结构图


代码实现

None.gifnamespace DesignPattern.Composite
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public interface ITree
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
void Process();
InBlock.gif        
void Add(ITree tree);
InBlock.gif        
void Remove(ITree tree);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class Leaf : ITree
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void Process()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Add(ITree tree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("叶子不支持该方法!");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Remove(ITree tree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("叶子不支持该方法!");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class Tree : ITree
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        IList
<ITree> treeCollection = null;
InBlock.gif
InBlock.gif        
public void Process()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (ITree tree in treeCollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tree.Process();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Add(ITree tree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            treeCollection.Add(tree);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Remove(ITree tree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            treeCollection.Remove(tree);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * 树工厂
ExpandedBlockEnd.gif
*/

None.gif
namespace DesignPattern.Composite
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class TreeFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static ITree GetTree()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (ITree)Assembly.Load("DesignPattern.Composite").CreateInstance("DesignPattern.Composite" + "." + System.Configuration.ConfigurationSettings.AppSettings["TreeName"].ToString());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * 客户程序
ExpandedBlockEnd.gif
*/

None.gif
namespace DesignPattern.Composite
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class TreeClient
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ITree tree;
InBlock.gif        
public TreeClient()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tree 
= TreeFactory.GetTree();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void ProcessTree()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 客户代码依赖抽象接口
InBlock.gif
            tree.Process();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



要点:
1、本模式采用树形结构来实现普遍存在的对象容器,从而将“一对多” 的关系转化为“一对一”的关系,使得客户代码可以一致地处理单个对象和对象容器,而无需关心处理的是单个对象还是组合的对象容器。
2、将“客户代码与复杂的对象容器结构”解耦是本模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口——而非对象容器的复杂内部实现结构——发生依赖关系,从而能够应对变化。
3、本模式中,将单个对象不具有的操作的(如Add和Remove)定义于接口或抽象类中,还是组合对象中,需要从透明性和安全性两方面平衡考虑。应用此模式,更强调透明性,所以有可能违背面向对象的“单一职责”原则。
 4、本模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存来改善性能。

转载于:https://www.cnblogs.com/Charly/archive/2007/06/11/779050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值