Common Bits&Bytes Patterns - Composite Pattern, Part II (1 of 2)

Composite Pattern?

During the next short burst of patterns, continuing from http://dotnet.org.za/willy/archive/2008/05/23/common-bits-amp-bytes-patterns-factory-pattern-part-5-of-5.aspx, we focus on the composite and the decorator pattern. As always, for detailed information on patterns refer to the Gang of Four (GOF) publication, a software engineering book titled Design Patterns: Elements of Reusable Object-Oriented Software ISBN 0-201-63361-2 authored by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Category Structural design pattern
Intent

Compose objects into tree structures to represent a hierarchy.

Applicability

Use this pattern to:

  • Represent a hierarchy of objects
  • Allow consumers to ignore the differences between composite and individual objects and treat all objects uniformly.

Class Diagram

The simple example simulates a simple toolkit hierarchy, which is represented by MFC, ATL, AWT or Swing type frameworks in real life.

image

Source Code Example

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace CompositePattern
   7: {
   8:     class Component
   9:     {
  10:         protected void InitializeOpsService(object ios)
  11:         {
  12:         }
  13:  
  14:         protected object GetOpsService()
  15:         {
  16:             return null;
  17:         }
  18:     }
  19:  
  20:     class FxContainer : Component
  21:     {
  22:         protected void Activate()
  23:         {
  24:         }
  25:  
  26:         protected void Deactivate()
  27:         {
  28:         }
  29:     }
  30:  
  31:     class FxComponent : FxContainer
  32:     {
  33:     }
  34:  
  35:     class FxButton : FxComponent
  36:     {
  37:         protected void SetState(int state)
  38:         {
  39:         }
  40:  
  41:         protected int GetState()
  42:         {
  43:             return 0;
  44:         }
  45:     }
  46:  
  47:     class FxLabel : FxComponent
  48:     {
  49:         protected void SetText(string text)
  50:         {
  51:         }
  52:  
  53:         protected string GetText()
  54:         {
  55:             return string.Empty;
  56:         }
  57:     }
  58:  
  59:     class FxTextBox : FxComponent
  60:     {
  61:         protected void SetText(string text)
  62:         {
  63:         }
  64:  
  65:         protected string GetText()
  66:         {
  67:             return string.Empty;
  68:         }
  69:     }
  70:  
  71:     class FxWindow : FxContainer
  72:     {
  73:         protected void SetTitle(string text)
  74:         {
  75:         }
  76:  
  77:         protected string GetTitle()
  78:         {
  79:             return string.Empty;
  80:         }
  81:     }
  82:  
  83:     class FxFrame : FxWindow
  84:     {
  85:     }
  86:  
  87:     class FxDialogBox : FxWindow
  88:     {
  89:     }
  90: }

Code Complexity Code Metrics

image

Next post will have a peek at the Decorator pattern.

Published Monday, June 23, 2008 10:06 AM by willy

Comments

# Common Bits&Bytes Patterns - Decorator Pattern, Part II (2 of 2)

Monday, June 23, 2008 5:29 PM by Willy-Peter Schaub's Cave of Chamomile Simplicity

Continued from dotnet.org.za/.../common-bits-amp-bytes-patterns-composite

# 58 » Blog Archive » Common Bits&Bytes Patterns - Composite Pattern, Part II (1 of 2)

Pingback from  58  » Blog Archive   » Common Bits&Bytes Patterns - Composite Pattern, Part II (1 of 2)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: