11.4.  Extending Template Based Artifacts

Now let us assume we want to refactor one of our modules to have an extra layer. We cannot do this change in the template because this would apply to all modules. If we still want to be able to use the template for this module we need some way to extend or modify the elements in the template:

artifact Module2
{
    include "Module2/**"
    
    apply "layering"
    
    // New layer
    artifact BusinessInterface
    {
        include "**/businessinterface/**"
    }
    
    // Now Business and UI need access to BusinessInterface
    extend Business
    {
        connect to BusinessInterface
    }
    extend UI
    {
        connect to BusinessInterface
        // UI should not use Business directly
        disconnect from Business
    }
}
    

Extending an artifact only makes sense in the context of apply directives. It allows us to add nested elements to an artifact and/or modify its connections to other artifacts. Within an extended artifact you can also use the keyword override to override the definitions of interfaces or connectors defined in the the original version of the artifact:

artifact Module2
{
    // ...
    extend Business
    {
        // This assumes that the template version of Business has an interface named "X"
        override interface X
        {
            // Use other patterns or other exports
            include "**/x/*"
        }
        connect to BusinessInterface
    }
    // ...
}
    

This allows you to adapt the architecture elements derived from a template when needed.