Structural diagrams can be used to model structure of the software. The most important aspects of the class diagrams are the Class diagrams including inheritance and aggregations.
Same as C++ code:
class Foo { public: void publicMethod(); protected: void protectedMethod(); private: int privateMember; };
Same as C++ code:
class Animal { }; class Bird : public Animal { }; class Mammal : public Animal { }; class Penguin : public Bird { }; class Chicken : public Bird { }; class Raven : public Bird { }; class Bear : public Mammal { };
An association represents a family of links. Typically associations is used to describe relationship between classes. A binary association (with two ends) is normally represented as a line. An association can link any number of classes. An association with three links is called a ternary association. An association can be named, and the ends of an association can be adorned with role names, ownership indicators, multiplicity, visibility, and other properties.
An example on association, describing situation where Foo-class uses Bar class:
Same as C++ code (in this case Foo uses Bar at method call):
Foo:someMethod(Bar* theBar) { theBar->doSomething(); }
Aggregation is a variant of the “has a” association relationship; aggregation is more specific than association. It is an association that represents a part-whole or part-of relationship.
An example on association, describing situation where Car has four tires. Note that wheels are independent on car, so if car is deleted, tires Still exist:
Same as C++ code:
class Wheel { }; class Car { Wheel* m_wheels[4]; };
An example on association, describing situation where Car has four tires. Note that wheels are dependent on car, so if car is deleted, tires are deleted too:
Same as C++ code:
class Wheel { }; class Car { Wheel m_wheels[4]; };
C++ code implementation:
enum EventType { PASS, COIN, RESET }; enum State { LOCKED, UNLOCKED, VIOLATION } State currentState = LOCKED; void event(EventType ev) { switch(currentState) { case LOCKED: switch(ev) { case PASS: alarm(); currentState = VIOLATION; break; case COIN: unlock(); currentState = UNLOCKED; break; } break; case UNLOCKED: switch(ev) { case PASS: lock(); currentState = LOCKED; break; case COIN: thankYou(); break; } break; case VIOLATION: switch(ev) { case RESET : lock(); currentState = LOCKED; break; } break; }