Is it possible to inherit styles?

+1 vote
asked Dec 10, 2023 in Question / help by Chris

Lets say that I'd like to define a base style, and create a number of substyles defined off that. These substyles could either add (define) new properties for a style, or change settings on existing styles. 

For example:

.BaseStyle { BackgroundColor Green; FontColor White; LineColor Gold; }
.FirstDerivedStyle { <link to .BaseStyle>; FontColor Black; } ' change the existing .BaseStyle font color to black
.SecondDerivedStyle { <link to .BaseStyle>; LineStyle 3; } ' adds the line style attribute to .BaseStyle

1 Answer

+1 vote
answered 5 days ago by dickmaley (4,020 points)

Style inheritance works.

image

@startuml
<style>
  .BaseStyle {
    BackgroundColor Green
    FontColor White
    LineColor Gold
  }

  .FirstDerivedStyle {
    ' Inherits from BaseStyle implicitly when applied after it
    FontColor orange
    LineColor red
  }

  .SecondDerivedStyle {
    ' Inherits from BaseStyle implicitly when applied after it
    LineThickness 3  ' Adds a new property not in BaseStyle
    LineColor blue
  }
</style>

' Hide the stereotype labels for cleaner output
hide <<BaseStyle>> stereotype
hide <<FirstDerivedStyle>> stereotype
hide <<SecondDerivedStyle>> stereotype

' Define classes with the styles applied via stereotypes
class BaseExample <<BaseStyle>> {
  +method()
}

class FirstExample <<FirstDerivedStyle>> {
  +method()
}

class SecondExample <<SecondDerivedStyle>> {
  +method()
}

' Apply BaseStyle first, then override with FirstDerivedStyle
BaseExample <|.. FirstExample
BaseExample <|.. SecondExample

@enduml

...