Project – BookStore – Structural Patterns-3

In the preceding Create method, we create the corporation, add two stores, then return the result.The CreateTaleTowersStore and CreateEpicNexusStore methods create a store, set their name, address, and manager, and create three sections each:

private IComponent CreateTaleTowersStore()
{
    var store = new Store(
        “Tale Towers”,
        “125 Enchantment Street, Storyville, SV 72845”,
        “Malcolm Reynolds”
    );
    store.Add(CreateFantasySection());
    store.Add(CreateAdventureSection());
    store.Add(CreateDramaSection());
    return store;
}
private IComponent CreateEpicNexusStore()
{
    var store = new Store(
        “Epic Nexus”,
        “369 Parchment Plaza, Novelty, NV 68123”,
        “Ellen Ripley”
    );
    store.Add(CreateFictionSection());
    store.Add(CreateFantasySection());
    store.Add(CreateAdventureSection());
    return store;
}

Both stores share two sections (have the same books; highlighted code), each with a unique section. If we look at the CreateFictionSection method, it adds an imaginary book and a subsection:

private IComponent CreateFictionSection()
{
    var section = new Section(“Fiction”);
    section.Add(new Book(“Some alien cowboy”));
    section.Add(CreateScienceFictionSection());
    return section;
}

The CreateScienceFictionSection method adds an invented book and the Star Wars book set composed of three trilogies (a set of sets):

private IComponent CreateScienceFictionSection()
{
    var section = new Section(“Science Fiction”);
    section.Add(new Book(“Some weird adventure in space”));
    section.Add(new Set(
        “Star Wars”,
        new Set(
            “Prequel trilogy”,
            new Book(“Episode I: The Phantom Menace”),
            new Book(“Episode II: Attack of the Clones”),
            new Book(“Episode III: Revenge of the Sith”)
        ),
        new Set(
            “Original trilogy”,
            new Book(“Episode IV: A New Hope”),
            new Book(“Episode V: The Empire Strikes Back”),
            new Book(“Episode VI: Return of the Jedi”)
        ),
        new Set(
            “Sequel trilogy”,
            new Book(“Episode VII: The Force Awakens”),
            new Book(“Episode VIII: The Last Jedi”),
            new Book(“Episode IX: The Rise of Skywalker”)
        )
    ));
    return section;
}

Now, if we look at this part of the data structure, we have the following:

 Figure 11.10: The Fiction section of the Epic Nexus store dataFigure 11.10: The Fiction section of the Epic Nexus store data 

In the big scheme of things, the whole organizational structure, down to the section level (without the books and sets), looks like this:

 Figure 11.11: the composite hierarchy without the books and setsFigure 11.11: the composite hierarchy without the books and sets 

I omitted to publish the whole data structure, including the books, as an image because it is too large and would be hard to read. Rest assured, the content itself is unimportant, and the section we are studying is enough to understand the flexibility the composite pattern brings to the design.

As we explore this, we can see how flexible the design is. We can create almost any organizational structure we want.Now, let’s look at the Program.cs file and register our dependencies and an endpoint to query the data structure:using Composite.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<ICorporationFactory, DefaultCorporationFactory>();
var app = builder.Build();
app.MapGet(
    “/”,
    (ICorporationFactory corporationFactory)
        => corporationFactory.Create()
);
app.Run();

The preceding code registers the factory that creates the corporation data structure with the container and an endpoint to serve it.When we execute the code, we get the full data structure or the corporation. For brevity reasons, the following JSON represents the fiction section, excluding the books:{
  “ceo”: “Bosmang Kapawu”,
  “name”: “Boundless Shelves Corporation”,
  “type”: “Corporation”,
  “count”: 43,

The value of the count fields reflect the total count. In this case, there is no book so the count should be 0. If you run the program and play with the preprocessor symbols define in the DefaultCorporationFactory.cs file (ADD_BOOKS, ADD_SETS, and ONLY_FICTION), you will end up with different number.

The Composite pattern allowed us to render a complex data structure in a small method call. Since each component autonomously handles itself, the Composite pattern removes the burden of managing this complexity from the consumer.I encourage you to play around with the existing data structure so that you understand the pattern. You could also try adding a Movie class to manage movies; a bookstore must diversify its activities. You could also differentiate movies from books so that customers are not confused. The bookstores could have physical and digital books as well.If you are still looking for more, try building a new application from scratch and use the Composite pattern to create, manage, and display a multi-level menu structure or a file system API.

Leave a Reply

Your email address will not be published. Required fields are marked *



          Copyright © 2015-2024 | About | Terms of Service | Privacy Policy