Tuesday 8 November 2011

Application on Model-Based Integration Mismatch Inference using Sonargraph-Architect

The goal of this experiement is to learn what is Model-Based Integration Mismatches, what are the causes of these mismatches, how to detect such type of mismatches, and what kind of available tools that can help/assist/automate the inference process.

I chose Sonargraph-Architect to See its analysis result.
I have a "Free-Community License" to use this tool. The license provided by www.hello2morrow.com; 2002-2011 hello2morrow GmbH, through an allowed/permited procedure.
The author of the tool: Alexander von Zitzewitz.

The example given in this post discussed in the A. Egyed and C. Gacek resaerch paper titled, "Automatically Detecting Mismatches during Component-Based and Model-Based Development", in Proc. of the International Conference on Automated Software Engineering, Cocoa Beach, Florida, Oct. 1999, pp. 191-198.
My reasons to study this example is only for academic purposes. I research in this area and I wanted to understand the Model-Based Integration Mismatches, apply the "View Integraion Framework" proposed by the authors, and regenerate the analysis results with the available tools.

I have been using Sonargraph-Architect for 5 months. While I was reading the paper mentioned above I was sure that Sonargraph-Architect will detect the Architecturtal mismatch shown in the example of the paper.
The example shows that a dependency in Design View violates some Architectural View Constraints.
In different words, a dependency in the Design View has no corresponding dependency in the Architectural View.
The Design model has a dependecy from User Interface to Inventory System.
However, the Architectural view does not have such a dependency.

I still haven't contact the Authors for a permission to copy the figures provided in their paper.
But, the Sonargraph analysis result shown below.








Usability Evaluation of Sonargraph-Architect - An Academic Course Assignment

ソフトウェア工学Ⅱ
Software Engineering II

Professor 大平 雅雄



Report I
Software Usability Evaluation





Student: Ahmad EL GHAFARI
ID: 1051133

Table of Contents
Title Page Number
Table of Figures 3
I. Introduction 4
II. Sonargraph-Architect 5
III. Feature Inspection (Usability Evaluation Approach) 7
III.1 Inspecting the Architecture Recovery Feature 8
III.2 Inspecting the Architecture Violation Feature 11
IV. Comparison with Rational Rose and Visual Paradigm 19
V. Limitations of my Usability Evaluation and Conclusion 20
VI. References 21


Table of Figures

Figure Number and Title Page Number

Fig. 1 The main Screen of Sonargraph 9
Fig. 2 The 'Exploration' tab screen shows the interdependencies 10
Fig. 3 The ‘Architecture’ tab in Sonargraph 11
Fig. 4 Defining two layers A and B and an allowed dependency 13
Fig. 5 Consistency warning appears at the top of the logical architecture 14
Fig. 6 Sonargraph takes the user to this view ‘1 consistency warning’ 15
Fig. 7 The ‘Exploration’ tab views the architecture violation 16
Fig. 8 The ‘Source’ tab views the violation implementation in source code file 17
Fig. 9 Brief assessment information about the subject system 18

I. Introduction
The task is to evaluate the usability of a chosen software application by the student attending the course. The evaluation method may rely on standards and rules suggested by experts and professionals in the arena. I chose Sonargraph-Architect to evaluate. The application is an Architecture Recovery and a Management tool of software systems architectures. It recovers the architecture and internal structure of other software applications. It performs Static Analysis on the source code of the subject system. It assumes Object Oriented Java code.
Sonargraph evaluates the Dependency of the subsystems and building elements of the subject system. The measurements are displayed in Sonargraph’s dashboard screen. That is it measures the cyclic Dependency and Coupling among the elements of the subject system. It applies Object Oriented Analysis and techniques suggested by masters in the area including, John Lakos, Robert Martin and Erich Gamma and colleagues.
The Architecture Management feature of Sonargraph is like a craftsman tool that facilitates defining a Logical Architecture of the subject system. This feature provides the capability for architects to model the intended logical architecture for the subject system. Sonargraph then will assess how much the source code is consistent with the defined logical architecture. As a result, Sonargraph will show if there is any violation to the defined logical architecture by the source code. For example, suppose we have a package ‘A’ and a package ‘B’. A user of Sonargraph defined in the Logical Architecture that package ‘A’ depends on package ‘B’. However, in the source code that is differently implemented, package ‘B’ depends on package ‘A’. Sonargraph will highlight a violation case and show where that violation happens in the source code by locating the file and line. The applicability of Sonargraph is crucial with systems having more than 30,000 bytecode instructions. We know for complex systems and real enterprise systems the number of bytecode instructions is counted in millions.

Sonargraph is one of the very rare tools to find which can handle such massively huge number of information. I tried many Static Analysis tools, Architecture Recovery and Management tools, but I found Sonargraph is the most usable one and the most enjoyable.

II. Sonargraph - Architect

Product: Sonargraph Architect
(c) Copyright hello2morrow GmbH 2005 – 2011
www.hello2morrow.com
Author: Alexander von Zitzewitz
Evaluation Strategy: Feature Inspection and Comparison

I am using this tool with the ‘Community Free’ license provided by hello2morrow.

Overall Impression of Using Sonargraph
I have been using Sonargraph for four months. I use it to conduct experiments for my research. Download, installation and configuration are so straight and easy. Here we must notice that such kind of tools usually needs hard work until they finally can operate on the workstation of the user. Such kind of tools need lots of time to setup and configure with many daunting settings to make. However, Sonargraph is a powerful tool and at the same time it is unique in the simplicity of setup. Sonargraph never halted or crashed. Never let me wait or show me a sand-clock mouse icon. Start learning to use Sonargraph is so smooth. Starting a new project is so straight and takes three fast steps. The guidance and manual are clear. The supporting web page is helpful. The author supports the user-experience and willing to have your impression, comment and suggestion. Sonargraph uses seven colours1 [2] and their message is consistent everywhere in its screens. Feedback messages are so short and they are at the top of the screen. Sonargraph hands a huge amount of information however it only shows what is relevant to the user and what is only needed at the moment. Once the user wants to know more details or choose to follow the subsequent information and causes of some aspect, e.g., causes of a violation case, or why some dependency is highlighted in Red not in Green, and etc. Then Sonargraph will take the user to the next detail. Terminology used by Sonargraph is consistent with what any expert expects [4].



1. Seven colours is memorisable by humans, Don Norman found out this is an acceptable number of items for the user to easily recognise and memorise.
Overview of the Main Interface
Sonargraph gives results of analysis, feedback, exploration of interdependencies and architecture definition in screens organised in format of tabs. Sonargraph main interface has nine tabs. The number is still in the acceptable range for a human being to remember.

Outlines of the Evaluation Strategy
The following section, will evaluate in more details two of the most important features of Sonargraph. I applied ‘Feature Inspection’ approach to evaluate those two features on Sonargraph. Section IV will evaluate Sonargraph by comparison with two other tools.

III. Feature Inspection

Usability Issues Covered: Effectiveness.

The method [5] suggests one may pick up a set of features of the software being evaluated. The following table list the features I am going to inspect.

ID Feature (Use Case) End Result
UC.1 Architecture Recovery A visualisation is abstracted shown to the user
UC.2 Architecture Violation Number of cases, and pointers to the places where the violation occurs

1. Inspecting the Architecture Recovery Feature

Description of the Feature of UC.1
The feature is the starting point of conducting an analysis project with Sonargraph. In literature, Architecture Recovery (performing static analysis) is the process that parses a source code of software system and abstracts the contents in higher level of abstraction. The level of abstraction varies from a method to another and thus among tools available. Sonargraph considers the high level of architecture where packages and classes with their interdependencies. Architecture Revocery process typically ends up with a visualisation of these abstracts in models and views are comprehensible by the professionals. These models can be for example UML Diagrams, or alike.

The scenario
Step User Sonargraph
1 Click ‘New System’ from ‘File’ menu Displays ‘New system’ dialogue box
2 Specify a system name and directory. Then clicks ‘Next’ button Displays ‘Workspace Import’ dialogue. In this screen user specifies the nature of the source code of the subject system whether it is Eclipse, IntelliJ, and etc.
3 Select a radio button of the choices given and click ‘Next’ Display a dialogue box to let user specifies the directory where the source code of the subject system relies
4 Select a directory and click ‘Next’ Display a dialogue box to let the user select assigns the building units of the subject source code to be used in the new project
5 Click on the building unites desired and click ‘Finish’ button Parse the source code and display the elements of the source code with the Dependency and coupling measurements, exploration of the structure, and all other information. Each set of information is in different tab.


Comment about the feature Architecture Recovery (UC.1)
As we can see making a new project in Sonargraph is done in 5 steps. The user makes only five simple actions (clicks). There are only four dialogue boxes present from the start of the use case to the end. The feature of the use case is well named and easily recognised. It consists with the global convention and its End Result is just what is expected by the user.

By the End Result of UC.1 Sonargraph will show the following screen.





Fig. 1 The main Screen of Sonargraph with focus on the ‘Dashbaord’ tab UC.1 End Result

The ‘Dashboard’ tab gives a general overview of the status of the system. The most important things that any architect and engineer needs to know. Number of bytecode instructions, number of line; Dependency health like package cyclicity; Warnings like is there any duplicate code, and etc.
The user then selects other tabs one by one to see the relative information. This is one way that Sonargraph manages information shown to the user. It does not make the user sinks in a huge amount information. Especially the user still has not meant to know about them. Let’s say now the user wants to see the abstraction visualisation of the internal interdependency of packages and subsequent elements of the source code. The user needs to click the ‘Exploration’ tab. And the following image shows the screen.


Fig. 2 The 'Exploration' tab screen shows the interdependencies among the elements of the subject system


This step does not need the user to wait. It is already there parsed and drawn from the source code and ready. As in convention The ‘Green’ colour means ‘OK’. The arcs between packages, classes and interfaces are green that means the interdependency is correct and does not violate any architectural intention.

2. Inspecting the Architecture Violation Feature

Description of the Feature of UC.2
The feature is the Analysis that does assess the conformance of the source code of the subject system to the Logical Architecture that the user intends. When the user defined a logical architecture, the user wants the source code to be the exact implementation of the defined architecture. Manual inspection is daunting. Typically, it is a process that is beyond humans’ capability. Practitioners cannot comprehend the huge architecture that source code may possess. Here is the job of Sonargraph with UC.2 to show the user weather the source code parsed is really the correct implementation of the architecture intended.

Therefore, for this feature to be performed, a logical architecture needs to be defined by the user.

How does Sonargraph facilitate defining a new logical architecture?
In the ‘Architecture’ tab (Fig.3), Sonargraph displays on the top a bar of the architecture artefacts that a user needs to define a new logical architecture.

Fig. 3 The ‘Architecture’ tab in Sonargraph before defining a logical architecture

This bar has the artefacts needed to define a logical architecture.


There are only seven artefacts. From Left to write, the first symbol defines a group of layers, the second defines a group of vertical slices, the third defines a layer, the fourth defines a vertical slice, the green shape of blocks defines a subsystem, the green arrow defines an allowed dependency and finally the red arrow specify a disallowed dependency.

A Brief Introduction of the Architectural Artefacts
Layer: to cut the application into horizontal slices (layers) by using technical critera. Typical layer names would be ‘User Interface’, ‘Service’, ‘DAO’, etc.
Vertical slice: Functional aspects should determine the vertical organisation of your application. Typical slice names would be ‘Customer’, ‘Contract’, ‘Framework’, etc.
Subsystem: A subsystem is the smallest of the architectural artefacts. It groups together all types implementing a specific mostly technical functionality. Typical subsystem names would be ‘logging’, ‘Authentication’, etc. Subsystems can be nested in layers and slices [A. Von Zitzewitz 1].

The user selects the layer symbol and type a title. Then link packages from the source code to the layer. Linking source code elements to a layer can be done in two ways. The first is a typical drag and drop technique and the second by using the context menu.

Let’s say we have two layers A, and B and we want to define a correct dependency. With the green arrow one may click on the source layer (dependent) and move (keeping the click) the mouse to the subject layer and click over the layer. Fig. 4.


Fig. 4 Defining two layers A and B. Defining an allowed dependency from A to B by a green arrow.


Comment about defining a logical architecture in Sonargraph
Defining a logical architecture is so smooth and do not need extra skills. The artefacts that are used do not confuse the user.

Now, we can talk about the Architecture Violation in Sonargraph.
After defining a logical architecture and assign source code units to the architecture, Sonargraph instantly highlights any occurrence of violation when the implementation parsed from source code is not consistent with the architecture definition. For example, suppose there are two layers Logging and Data. The user defined a dependency from Logging layer to Data layer using the green arrow. See Fig. 5.
The user assigned the package com.cots.logging to Logging layer and assigned com.data.data package to Data layer.
However, in the source code Sonargraph finds out that com.data.data depends on com.cots.logging.


Fig. 5 Consistency warning appears at the top of the logical architecture definition. After defining an allowed dependency from Data layer on Logging layer; Sonargraphs finds out that the implementation in the source code violates the intention.


The user can click on ‘1 consistency warning’ and Sonargraph will take the user to view more details about this violations. Sonargraph will display the ‘Architecture Consistency’ tab under the ‘Summary’ tab; Fig. 6.


Fig. 6 Sonargraph takes the user to this view ‘1 consistency warning’ of the previous view was clicked. In the bottom section of the tab ‘Architecture Consistency’, one can see the Description feedback telling the user why this happens to be an architecture violation.


And for the user to know more about this violation, the Exploration tab view, as shown in Fig. 7, will show more details.


Fig. 7 The ‘Exploration’ tab views the red arrow between Logging layer and Data layer. The red arrow ends link the exact implementation units that cause the violation.

And finally, for the user to know how this violation happens and what the piece of code this is implemented, the user may click on the red arrow that appears in Fig. 7. Sonargraph will show the place of the violation in the source code by file and number of line, as shown in Fig. 8.


Fig. 8 The ‘Source’ tab view where Sonargraph opens the file where the violation is implemented and alos pointing to the line number of the code file.

Also, there is a description of each violation case at the bottom of the tab as usual.
There are more feedback are given in the bottom section of the ‘Source’ tab. Description of how the violation happens and why the code is not consistent with the defined logical architecture.
In general, the dashboard will always show the final status (assessment) of the architecture of the subject system including the architecture violation number of occurrence s and their types. Once the user wants more details, the user may click on the number appears in front of ‘Architecture violations:’ in the tab view. This is found in the ‘Architecture’ section of the view. Fig. 9.


Fig. 9 Brief assessment information about the subject system; The most important things to know and the entry point for each kind of assessment. The text written in blue is linked to the subsequent information and related details.

Comment about the feature Architecture Violation (UC.2)
The user does not need to go throw a daunting scenario to discover the architecture violation occurrences. Rather, Sonargraph has been evaluated the implementation of the source code and compares the structure and dependency and interconnections among the building units of the systems, including, packages classes, interfaces, etc. The analysis was made just with the first feature (UC.1 Architecture Recovery). Once the user defined the logical architecture intended, Sonargraph highlights instantly a warning for any event of violation. These warning are consistent everywhere in each tab, and visualised in a way that copes with the context and job of each tab.

IV. Comparison with Rational Rose and Visual Paradigm
I have used many Architecture Recovery tools, CASE tools, and Static Analysers. Some of these tools take lots of physical memory and they halt. They take noticeable amount of time to parse the source code of the subject system to abstract the architecture, for example, Visual Paradigm.
CASE Tools are powerful; they allow defining logical architectures. However they are so rigid and linking to the source code is almost artificial. I have used Rational Rose 2002 during my work at software industry.
With CASE tools there is no two-way path where each modification to the source code will be instantly reflected in the working project. However, Sonargraph can parse the source code and update the analysis instantly and by on key button: ‘F5’. That is if the user modified the source code, Sonargraph is capable and on the fly to notice any change. By comparison, I can give the effectiveness of Sonargraph to be the top. One more thing, Sonargraph deals with many levels of hierarchy of building units and naturally with their dependencies.

V. Limitations of my Usability Evaluation and Conclusion
I do not claim that this survey is enough to give an objective evaluation of Sonargraph Usability, because it lacks to have a pilot or systematic survey. For example, it would be much better to ask 10 users (who are experts) to use Sonargraph and to fill the questionnaire like the one provided by QUIS [4].
Although my evaluation is still subjective but I used “Feature Inspection” [5] approach which I found it can give reliable results with only taking the analysis of an expert who uses the subject system, and one expert is enough.
In conclusion, Sonargraph is very powerful, accurate, and effective. Sonargraph satisfies the user (the architect) needs and expectation. It takes little time to start using it and also little time and effort to master it. It is unique among other tools I have used.

VI. References
[1] The lectures handouts of Software Engineering II course provided by the professor.
[2] Don Norman, Don Norman and The Design of Everyday things, http://katsvision.com/canm606/session_3/03_norman_01.pdf
[3] A. von Zitzewitz 1 Reference Card Software Architecture Management, white paper provided in the web site of Sonargraph Architect, www.hello2morrow.com
[4] QUIS, http://lap.umd.edu/quis/
[5] http://www.usabilityhome.com/FramedLi.htm?FeatureI.htm

Wednesday 28 July 2010

On Refactoring Methods

On Refactoring Methods
Report No.10
先端ソフトウェア工学Ⅰ Advanced Software Engineering I  


Ahmad EL GHAFARI
ID#1051133
Software Design Laboratory
NAIST, Japan July 2010


1. Extract Method
Suppose I have a software system that collects files found in media (e.g. hard disk). Starting from selected root directory by user, the system seeks folders, and sub folders, grabs the files, and then lists them for the user in UI platform.
User can specify whether all files should be listed all just files match particular category selected. User highlighted that only ZIP, PDF and Executable files only are of concern.

In case of all files found are listed. User required two features.
 User needs to sort (or group) the files matching a particular file type.
 User needs to see a list of files filtered by a selected type, i.e. ZIP type files.

Let’s show the initial class diagram of the system illustrated in Figure 1 File Collector System Initial Class Diagram



Figure 1 File Collector System Initial Class Diagram

Here, I want to focus only on the methods that we are interested in to highlight how refactoring using Extract Method eliminates duplicate code. We assume that the functionality of grabbing files from the selected root directory and every subfolder is defined as “collectAllFiles()” member method—in the class Collector and that it returns the result to data member “m_collectedFiles”. Here, I provide Source code of the system defined written in Java.

Collector.java
public class Collector {
private String m_rootDirectory;
private java.util.List m_collectedFiles =null;

public Collector(String rootDirectory){
this.m_rootDirectory = rootDirectory;
this.m_collectedFiles = collectAllFiles();
}

private java.util.List collectAllFiles(){
//go to the selected directory found in data member m_rootDirectory…

//collect the files, and check all folders and //subfolders under the root directry and //collect all //the files found

}
public String getCollectedFiles(){
String result=null;
java.util.Iterator files = this.m_collectedFiles.iterator();
while(files.hasNext()){
File file = (File) files.next();
result += file.fileInfo();
}
return result;
}
}

In the following discussions, we will mention the other two functions
“sortCollectedFilesByType()” and “filterCollectedFiles”.


Sorting Scenario

public String sortCollectedFilesByType(){
String result="\nCollected files sorted \n";
java.util.Iterator files = this.m_collectedFiles.iterator();
while(files.hasNext()){
File file = (File) files.next();
if (file.getType() == FileType.EXECUTABLE)
result += file.fileInfo();
}

files = this.m_collectedFiles.iterator();
while(files.hasNext()){
File file = (File) files.next();
if (file.getType() == FileType.PDF)
result += file.fileInfo();
}

files = this.m_collectedFiles.iterator();
while(files.hasNext()){
File file = (File) files.next();
if (file.getType() == FileType.ZIP)
result += file.fileInfo();
}
return result;
}
As we can see the same code chunk is repeated 3 times. We experience duplicate code.
Using Extract method that has the algorithm (logical chunk) in one method and call it in all places required.
public String sortCollectedFilesByType(){
String result="\nCollected files sorted \n";
result+=getFilesSortedAs(FileType.EXECUTABLE);
result+=getFilesSortedAs(FileType.PDF);
result+=getFilesSortedAs(FileType.ZIP);
return result; }

The Extracted Method is
private String getFilesSortedAs(
FileType selectedFileType){
String result="";

java.util.Iterator files = this.m_collectedFiles.iterator();

while(files.hasNext()){
File file = (File) files.next();
if (file.getType() == selectedFileType)
result += file.fileInfo();
}
return result;
}
As we can see the duplicate code was removed preserving the behavior of the system.


Figure 2 Collector Class Diagram after Extract Method Refactoring

In Figure 2 Collector Class Diagram after Extract Method Refactoring is shown how the Collector Class object refinement after applying Extract method refactoring.

2. Form Template Method
Filtering scenario
After the system listed all files, User chooses a type category from Filter menu. Then, the system should relist only the files matched the category. User interested in only three types of files, Executable, PDF and ZIP files.
One can define the method as the following

public String filterCollectedFiles(
FileType selectedFileType){
String result="\nCollected files filtered as " +selectedFileType+"\n";

java.util.Iterator files = this.m_collectedFiles.iterator();

while(files.hasNext()){
File file = (File) files.next();
if (file.getType() == selectedFileType)
result += file.fileInfo();
}
return result;
}
However, after some time, the developers collected new user requirements. User raised an issue of the need to filter collected files not only by Type category, but rather they need to be able to filter the files by file Title, and Size.

Let’s say do this. Copy the method two times changing the parameter for each to match the criteria. For filtering by title we provide a String selected title, and the same for size, as the following.

public String filterCollectedFiles(
String selectedTitle){
String result="\nCollected files filtered as " +selectedTitle+"\n";

java.util.Iterator files = this.m_collectedFiles.iterator();

while(files.hasNext()){
File file = (File) files.next();
if (file.getTitle().compareTo(selectedTitle)==0)
result += file.fileInfo();
}
return result;
}

public String filterCollectedFiles(
double selectedSize){
String result="\nCollected files filtered as " +selectedSize+"\n";

java.util.Iterator files = this.m_collectedFiles.iterator();

while(files.hasNext()){
File file = (File) files.next();
if (file.getSize()==selectedSize)
result += file.fileInfo();
}
return result;
}
It’s obvious how the logic is the same, and what differs is only the parameter sent via the methods signatures and the condition/checking criteria. This is only a duplicate in two places. But, suppose some new changes happen to the system requirements. For example, the system should filter files by “DateCreated”. In fact, requirements change is open and we need more abstract way of thinking to enhance extensibility of our system. Form Template Method can eliminate the duplicate code as the following.

Suppose we have an abstract Class Filter which is a superclass of all the alternative Filter classes in which each of the subclasses having the operation of filtering logic.
Refactoring by applying Form Template Method of the invariant logic of the filtering, will remove the duplicate code exist in every subclass, i.e. FilterType, FilterSize, etc. Each concrete sub class will tweak the algorithm for its needs with providing its own implementation variation, as following.
Filter class is abstract and have the Template method

final String filter()
And has abstract method that will be overridden by its children

abstract Boolean compare()
All the filtering types have the same algorithm—perform similar operations with a few minor differences—differ in the comparison. For example, one needs to compare two Strings of Title, second wants to compare Size of files of type “Double”, a third needs to compare the Type of the file, PDF, ZIP etc.
The Class Diagram and amendments listing made to the source code of the source code are given as following. Some refinements was made also two the Collector and File classes. A new data member of Type Filter was added to Collector.
We need to get the title and size of file, so two more get public methods were added, as shown in Figure 3 Template Method Pattern as Filter Class Hierarchy Class Diagram.


Figure 3 Template Method Pattern as Filter Class Hierarchy Class Diagram

Filter.java
public abstract class Filter {
abstract Boolean compare(
File file, Object selectedCriteria);

final String filterCollectedFiles(
java.util.List collectedFiles, Object selectedCriteria){

String result="\n Collected files filtered as " +selectedCriteria.toString()+"\n";

java.util.Iterator files =collectedFiles.iterator();

while(files.hasNext()){
File file = (File) files.next();
if (compare (file, selectedCriteria))
result += file.fileInfo();
}
return result;
}
}

Children of Filter Class are FilterType to filter by type, FilterTitle to filter by title and FilterSize to filter according to a selected size.
Here the source code of the Filter children.

FilterType.java
public class FilterType extends Filter{
@Override
Boolean compare(File file, Object selectedCriteria){
Return file.getType().toString().compareTo(
selectedCriteria.toString())==0 ? true:false;
}
}
FilterTitle.java
public class FilterTitle extends Filter {
@Override
Boolean compare(File file, Object selectedCriteria) {
return
(file.getTitle().compareTo(
selectedCriteria.toString())==0)?true:false;
}
}
FilterSize.java
public class FilterSize extends Filter{
@Override
Boolean compare(File file, Object selectedCriteria) {
return file.getSize()==Double.valueOf(selectedCriteria.toString()).doubleValue()?true:false;
}
}
What Happened to the three copies of our filtering method in Class Collector filterCollectedFiles ()?
Actually, the method and all its duplicates replaced by one just 3 lines, as following

public String filterCollectedFiles
(Object keyFilter, Filter filterType){

filter = filterType;
return filter.filter (m_collectedFiles, keyFilter);
}

We Formed Template Method “filter()” which is the invariant, while each inherited class will do its own check variance by introducing its own “compare()” From now on, any client will specify the filter type and the key filter passing them as parameters to our original method “filterCollectedFiles()” while this method will call the Template method “filter” in Filter Class. As a result, all code duplicates were eliminated.

3. Extract Class
Suppose now Customer raised a new requirement. They want the system not only print out the files information on screen, rather generating XML log files having information about listed files shown in screen. Also, they want the system read such XML log files in future. In addition, Customer wants also to generate log files for some files filtered in specific category (i.e. all PDF files).

Here, we can add a public method in Collector Class that generates log files having the list of collected files with their information. And, another method that reads generated log files. And a method generates a log file with filtered files according to some category. Here, we can see that with the first method and third method is the same if we passed the list of files as an argument to the methods parameters. And, as a result a duplicate code!

In addition, it turns out that two methods are not enough to do so, unless we are going to hard-coding or doing procedural programming. Since, generating an XML file needs creating a file system at storage media, and needs I/O functionalities, in addition to Serialization procedure and for reading we need Deserialization. Actually, this is not a job for Class Collector. In other words, we want to apply Object-Oriented and design patterns to exhibit clarity, modifiability and extensibility.

Let’s say that all the said amendments are taken and made to Class Collector. The result is Class Collector is doing too much work, which is a work that should be done by two classes. Extract Class refactoring witnesses the birth of a Class that is responsible of doing such kind of job. This class also needs as an attribute of type File as in Class Collector and another attribute of type Filter. Here, what we have is two features in two classes, common attributes, and common methods. In addition, The two classes Class Filter and Class File will be also aggregated to the new born Class which is unacceptable coupling in the system architecture that rises the complexity. The new Class called XMLGenerator.
As shown in Figure 4 The System Class Diagram with the .




Figure 4 The System Class Diagram with the Extracted Class XMLgenerator

Notice the common attributes “m_filter: Filter” and “m_collectedFiles : List” and the common method “filterCollectedFile()” in both classes. Also, the aggregation and composition among Collector and XMLGenerator and File and Filter classes are obvious which signs for high coupling and complexity of the system architecture.

According to Folwer’s refactoring catalogue [‎1], and as as desined in [‎3] “when you have two classes with similar features, create a superclass and move the common features to the superclass”, we can apply Extract Class refactoring to eliminate duplicate code in the two classes Class Collector and Class XMLGenerator. In fact, Extract Class will also solve the two coupling issues among the two said classes and the other two classes Filter and File. The Extracted Class called FileHandler acts as a superclass for Collector and XMLGenerator. The common attributes and method will be moved to the FileHandler As shown in Figure 5.


Figure 5 The System Class Diagram After Extracted Superclass FileHandler as Superclass for Collector and XMLGenerator

The code listing as the following

FileHandler.java
public abstract class FileHandler {
protected Filter filter =null;
protected java.util.List m_collectedFiles =null;

final String filterCollectedFiles(
Object keyFilter, Filter filterType){
filter = filterType;
return filter.filter(m_collectedFiles, keyFilter);
}
}
XMLGenerator.java
public class XMLGenerator extends FileHandler{

public XMLGenerator(java.util.List list)
{
super.m_collectedFiles=list;
}

public void generateXML(
Object keyFilter, Filter filterType)
{
if (keyFilter == null && filterType==null ) {
System.out.println("XML log file was generated successfully.\n");

//serialize files to xml
...
}else
{
String s=super.filterCollectedFiles (
keyFilter, filterType);

//serialize files to xml

...

System.out.println(
"files were filtered and XML log file was generated successfully." + "\n"+s+"\n")

}
}

The result is one code plays the role for many clients. The features that are called in many places as needed placed in one place with a newly extracted class called FileHandler removing the duplicate code, though the solution introduced might be not the ultimate, but at least showing how Extract Class refactoring can eliminate duplicate code.


4. Pull Up Method

In the previous section “Extract Class” we moved up the common method “filterCollectedFile()” that appeared in both Collector and XMLGenerator. We Pulled Up the method to the superclass FileHandler. Since the behavior was the same of the two methods, Pull Up Method refactoring worked by removing the duplicate code and now the algorithm (a piece of code) exists uniquely in one place in the system.

References




1. Matrting Folwer, Kent Beck, John Brant, William Opdyke, and Don Roberts. Refactoring: Improving the Design of Existing Code. Addison-Wesley, Reading, MA, 1999.
2. Robert France, Sudipto Ghosh, Eunjee Song, and Dae-Kyoo Kim. A metamodeling approach to pattern-based model refactoring. IEEE Software, pages 52-58, September 2003.
3. Sami Beydeda, Matthias Book, Volker Grunhn (Eds.). Model-Driven Software Development. Generic and Domain-Specific Model Refactoring Using a Model Transformation Engine, Jing Zhang, Yuehua Lin, and Jeff Gray, pages 199-206, Springer 2005.

Monday 12 July 2010

An Implementation of Bridge Pattern - Modellers Commit Capability



A cut of a report for a course of my master's programme at Nara Institute of Science and Technology, Japan.

Wednesday 22 July 2009

情報技術の問題は何か?

    
  生活を良くするため、エンジニアはソフトウェア・アプリケーションを開発します。けれども、これわ新しい問題を引き起こします!例えば、私は時々携帯電話を使うとき、難しいと思います。私だけでなく携帯を使うとき困る人がいます。

  技術は私達のすべての希望に応えるわけにはいきません。また、技術は私達の時間を浪費します!


  三つの例があげます:

  1.機能ありすぎる。
  2.必要な機能がない。
  3.必要な機能があっても、見つけることができない。

  そのため、技術の使い方に満足することができません。
  
  どうして情報技術の会社は完璧なアプリケーションを製造しないのでしょうか?

  三つの理由があります:

  一番の理由は:

  多くの種類のユーザーがいます。
  そのため、特定のアプリケーションは満足されていますが、全員ではありません。

  二番の目の理由は:
  
  基本的に、ソフトウェアの品質を確保することが保証できないからです。
  エンジニアはもっと使い勝っ手がいいソフトウェアを開発していますが、品質を確保が困難であいます。

  そのため会社は人的資源を消費します。
  また、顧客、例えば、私達と他の会社は多くのお金を支払います。
 
  三番目の理由は:
  
  ソフトウェアテストが十分でないことです。
  それでもなお、エンジニアは重要なテストをすることができます。しかし、記録は正確ではありません!
  車輛生産テスト、例えば、自動車テストは成人の段階です。でも、ソフトウェア工学はまた幼児の段階です。

  技術会社は良いテストをしないことがあります。
  テストの完了前に商品を販売します。
  代わりに、ユーザーが、それを使っている間にアプリケーションをテストしています!
  つまり、会社がユーザーからフィードバックを受けてソフトウェアを修正します。節約と販売速度を早めるためです。


  皆さん、私達は技術から何を必要としますか?
  その答えは:
  
  技術に奉仕してもらいたい、ということです。
  また、物を自動化するために、情報技術が発明されました。
  しかし、今日まで、技術で100%自動化された物はありません!
  したがって、私達が技術に奉仕しています!

  人間の言語と情報技術の言語の間には隙間があります。
  情報技術が人間を理解できれば、もっと問題が少ないソフトウェアができます。
  それには時間が必要です。
  私はその問題を十分に調査したいです。
  
  すべての種類のユーザーを満足させるために、情報技術はかならずユーザーのニーズに応えなければなりません。
  
  情報技術のアプリケーションを考え、設計し、開発するために、より高い抽象概念と拡張性を得ることが必要です。

  
  
  

Wednesday 12 November 2008

Singleton

It is a punishment;
when a developer commits a violation, or abuses an engineering principle!