Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# Newbie Question..
Message
 
À
16/03/2003 16:57:28
Alvin Lourdes
Children and Youth Services Cluster
Toronto, Ontario, Canada
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00766381
Message ID:
00766416
Vues:
20
This message has been marked as the solution to the initial question of the thread.
Alvin,

>However, Composition/Aggregation and how they can be interpreted in code eludes me.
>Could someone explain how I would use composition/aggregation in code.

In a composition relationship, objects acquire references to each other at run time. Here's an example...I've defined a MyIterator and MyVisitor class as well as an IVisitor interface. The interface defines a single "Process" method. The MyIterator class has a single field named "Visitor" that is of the type IVisitor. The RegisterVisitor method is used to establish the composition relationship. It accepts a single IVisitor parameter and stores the object reference in its protected Visitor field. You can create other custom methods in the MyIterator class that reference this Visitor property and call the Process() method..
// Iterator class
public class MyIterator
{
	protected IVisitor Visitor;

	public void RegisterVisitor(IVisitor visit)
	{
		this.Visitor = visit;
	}
}

// Visitor class
public class MyVisitor : IVisitor
{
	public bool Process(string s)
	{
		MessageBox.Show(s);
		return true;
	}
}

// IVisitor interface
public interface IVisitor
{
	bool Process(string s);
}
Here's some code that instantiates the iterator and visitor, then calls the RegisterVisitor method.
public class TestComposition
{
	public void Test()
	{
		// Instantiate the iterator
		MyIterator iterator = new MyIterator();
		// Instantiate the visitor
		MyVisitor visitor = new MyVisitor();
		// Register the visitor with the iterator
		iterator.RegisterVisitor(visitor);
	}
}
You can also factor this down as follows:
// Instantiate the iterator
MyIterator iterator = new MyIterator();
// Register the visitor with the iterator
iterator.RegisterVisitor(new MyVisitor());
Going further with the example, you could give the iterator a constructor that accepts an IVisitor parameter:
// Iterator class
public class MyIterator
{
	protected IVisitor Visitor;

	public MyIterator(IVisitor visit)
	{
		this.RegisterVisitor(visit);
	}

	public void RegisterVisitor(IVisitor visit)
	{
		this.Visitor = visit;
	}
}
You could then do the whole instantiation and registration process in a single line:
MyIterator iterator = new MyIterator(new MyVisitor());
If you want to create an aggregation relationship where one object is the owner of another, you could do much the same thing, except the owner object might instantiate the Aggregatee. If you go this route, I highly recommend instantiating the Aggregatee in a factory method. For example, the following code shows the Business Object class (the owner, or Aggregator) instantiating a Business Rule object (the Aggregatee) in its constructor by calling a factory method named CreateBusinessRulesObject(). Creating aggregate objects this way allows subclasses to override the factory method and instantiate a different Aggregatee.
// Business Object class
public class BusinessObject
{
	protected BusinessRules MyBizRules;

	// Constructor
	public BusinessObject()
	{
		this.CreateBusinessRulesObject();
	}

	// Factory method
	public void CreateBusinessRulesObject()
	{
		this.MyBizRules = new BusinessRules();
	}
}

// Business Rule class
public class BusinessRules
{
}
Hope this helps!
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform