Visual CppDepend        Visual Studio AddIn             CQL Language        Dependency Graph


Refactoring needs tools to understand the existing code , and also tool to compare modified code with the original one. CppDepend provides features to understand and compare different versions, for that we will see the utility of CppDepend when refactoring an existing code to add an abstract factory.

The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their concrete classes. The Abstract Pattern makes sure that the client does not know anything about the objects being created. The pattern hides the implementation of the product definitions and their class names and the only way the client can create a product is through the factory.
Let’s discover the benefits of this Pattern using CppDepend, and for that we analyze the following example.
In this example we have a code source before and after refactoring, you can copy the code into two distincts files factorybefore.cpp and factoryafter.cpp.

CppDepend Analysis

To analyze this example with CppDepend, do the following steps:
- Lanch VisualCppDepend.exe and create new project.
- Use “Create Project Description “ Button to launch the project maker wizard


- Add Project to solution.
- Add Files to project created



- Save as “Factory.sl”
- In VisualCppDepend use “Browse” button to specify Factory.sl file.
- Launch the analysis.

After the analysis of the example before using factory pattern, we analyze it after the changes, for that we have to modify the description of the project in the ProjectMaker wizard by removing “factorybefore.cpp” and adding “factoryafter.cpp”.
CppDepend provides an interesting functionality to compare between two versions, in our case we compare the project before and after refactoring and here's the result.



We see that there are some added new classes in the browser characterized by the Bold Style and there's also a modification in dependency matrix.

Where WindowsButton class is created?

To answer to this question let’s execute the following CQL request:

SELECT METHODS WHERE CreateA "WindowsButton" ORDER BY DepthOfCreateA

Before:



After:



So we can observe that before refactoring, the class is created in many places, but after refactoring the creation is isolated in WindowsFactory, the responsibility of the creation is now isolated and it impact the cohesion, so we have a higher cohesion than before.

What the impact for coupling?

Using Matrix can help to detect coupling between projects and classes.



If you look at the intersection between main method and MotifButton class we observe a red “-“sign, so we lose a dependency between Main and other classes like WindowsButton, MotifButton.

Let’s discover the dependency graph before and after:

Before:



After:



So the global methods are now isolated form concretes classes used, and we have a low coupling so we can add another family of classes without changing the client that use them, only the factory will be changed.

Conclusion:

Abstract Factory can improve the cohesion and coupling of classes.


Share |
Customers


Last Version: 2.8.7


CppDepend accelerate the process of your developement, migration and maintenance.

These Cases Studies shows the utility of CppDepend to have a deep view of your architecture, design and implementation.

CQL langage provides more flexibility to request code base, so you can customize your analysis and reports .

Home Contact Us SiteMap