I presented at the Black Marble Architecture Forum in the North event in December and one of the things I showed was how to create a toolbox item extension to add a UML design pattern into Visual Studio’s UML toolbox. Having done this I checked and couldn’t find any extensions that covered the Gang of Four design patterns, so I thought I’d create an extension to do just that. Let’s start at the end with where to download them and what they give you…
The toolbox items extension gives you some extra toolbox items that cover the majority of the core Gang of Four design patterns. Once installed, if you open a UML class diagram you should see the following additions under Structural, Creational and Behavioural (yes, British spelling) Patterns:
Select one of the toolbox items and this will place a ready to go representation of that design pattern on the class diagram, in this example it’s the Abstract Factory:
Underlying the visible Toolbox extension is a UML profile that contains a number of UML stereotypes to mark classes and interfaces with roles in the relevant pattern. So in the example above there is an IFactory interface stereotype and an Abstract Factory class stereotype.
If that looks interesting then depending on what you want you can get hold of this in two places:
I’ve published the .vsix extension to the Visual Studio Gallery here. All you need to do is download the .vsix, install it and restart Visual Studio. To check it installed successfully then go to Tools | Extension Manager and you should find these extensions have been added (I’ve filtered on UML to simplify it):
Then open or create a UML modelling project and once you have a class diagram you should see the new toolbox items.
I’ve added the Visual Studio solutions that I created this extension from to Codeplex here. You can download the solutions, take a look at the projects and update/improve/extend/adapt the toolbox items to suit your needs.
Take a look at the Codeplex project if you want to download the full solutions but as an overview this is essentially simple. It breaks down into 4 key aspects:
I wanted to create a UML profile in order to define the stereotypes that I would use in the design patterns. As an example I wanted to have <<Singleton>> stereotype so that any class that you wanted to implement the Singleton pattern could be identified as such without having to rely on naming conventions, and the name can be independent of the stereotype. A secondary reason is that if I wanted to generate code from the design patterns this would allow the T4 templates to generate the appropriate code for the specified stereotype. I haven’t got round to this part but using a profile means that this will be possible in the future.
The documentation on creating a custom profile is here. This is what I followed but a summary is:
1: <profile dslVersion="22.214.171.124"
2: name="GoFDesignPatterns" displayName="GoF Design Patterns"
2: <stereotype name="IVisitor" displayName="IVisitor" >
4: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IInterface" />
7: <stereotype name="ConcreteVisitor" displayName="ConcreteVisitor" >
9: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
12: <stereotype name="Element" displayName="Element" >
14: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
17: <stereotype name="ConcreteElement" displayName="ConcreteElement" >
19: <metaclassMoniker name="/GoFDesignPatterns/Microsoft.VisualStudio.Uml.Classes.IClass" />
2: <!-- List of metaclasses (that is, classes in the UML spec) that are mentioned in the stereotype definitions. -->
3: <metaclass name="Microsoft.VisualStudio.Uml.Classes.IClass"/>
4: <metaclass name="Microsoft.VisualStudio.Uml.Classes.IInterface"/>
Having created the profile itself (actually I did this iteratively and not in one go), then you need to package this as a Visual studio extension, a .vsix file. This is actually fairly straightforward and there are good instructions here. The key points I would highlight are:
I followed the documentation here and it was pretty easy. My points to note:
2: <customToolboxItem fileName="AbstractFactory.classdiagram">
7: <value>Creational Patterns</value>
10: <bmp fileName="DesignPatternIcon.bmp"/>
16: <value>Groups object factories that have a common theme.</value>
Exactly the same as the extension project for the profile with the following exceptions:
I hope that either the toolbox items themselves or the explanation on how to create them are useful or interesting, and I’d encourage you to think of opportunities to extend and customise your Visual Studio environment to help you work more effectively. And if you don’t like my implementation of the patterns then please improve them via the Codeplex project.