Adaptive Code via C#: Agile coding with design patterns and SOLID principles by Gary McLean Hall

Adaptive Code via C#: Agile coding with design patterns and SOLID principles by Gary McLean Hall

Author:Gary McLean Hall [Hall, Gary McLean]
Language: eng
Format: epub, pdf
Published: 2014-09-16T19:58:06+00:00


private ICollection<IComponent> children;

}

// . . .

class Program

{

static void Main(string[] args)

{

var composite = new CompositeComponent();

composite.AddComponent(new Leaf());

composite.AddComponent(new Leaf());

composite.AddComponent(new Leaf());

component = composite;

component.Something();

ptg14200592

}

static IComponent component;

}

In the CompositeComponent class, there are methods for adding and removing other instances

of the IComponent. These methods do not form part of the interface and are for clients of the

Composite Component class, directly. Whichever factory method or class is tasked with creating

instances of the CompositeComponent class will also have to create the decorated instances and pass

them into the Add method; otherwise, the clients of the IComponent would have to change in order

to cope with compositions.

Whenever the Something method is cal ed by the IComponent clients, the list of composed in-

stances is enumerated, and their respective Something is cal ed. This is how you reroute the call to a

single instance of IComponent—of type CompositeComponent—to many other types.

Each instance that you supply to the CompositeComponent class must implement the IComponent

interface—and this is enforced by the compiler due to C#’s strong typing—but the instances need

not all be of the same concrete type. Because of the advantages of polymorphism, you can treat all

implementations of an interface as instances of that interface. In the example shown in Listing 5-15,

the CompositeComponent instances provided are of different types, further enhancing this pattern’s

utility.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.