## 3 Extract Class Refactoring Based on Clients
### An Application Example  
In order to consider an example to show how the proposed approach can be applied, let class \(X\) be the class to be refactored. Let \(M(X)=\{m_{1},m_{2},m_{3},m_{4},m_{5},m_{6},m_{7}\}\). Let the clients of each method in \(X\) be the following,  
\[Client(m_{1}) =\{A,B\},\] \[Client(m_{2}) =\{A,B,C\},\] \[Client(m_{3}) =\{B,C\},\] \[Client(m_{4}) =\{D,E,F\},\]\(Client(m_{5})=\{F,G\}\), \(Client(m_{6})=\{F,G,H\}\), and \(Client(m_{7})=\{G,H\}\).  
```
Input: 1) \(F\): the output of Algorithm 1 which may include small subsets of methods. 2) \(minNumMethods\): the minimum number of methods that each extracted class can have 3) \(k\times k\) matrix: holds \(Sim_{clients}\) between each pair of methods in the original class where \(k\) is the number of methods in the class Output: \(F\) after merging small subsets begin for\(X\in F\)do if\(|X|<minNumMethods\)then  find \(Y\in F-X\) that has the highest average of client-based similarities after the  merger of \(X\) and \(Y\);  add the elements of \(X\) to \(Y\);  remove \(X\) from \(F\);  end if  end for return \(F\)  end for
```  
**Algorithm 2**mergeSmallClasses(\(F\), \(minNumMethods\), \(k\times k\) matrix)  
Given the above sets, we can compute the client-based similarity between each pair of methods of class \(X\) using Definition 4. The matrix shown in Table 1 holds the client-based similarity between each pair of methods of class \(X\). The entry \([i|j]\) of the matrix holds the value of \(Sim_{clients}(m_{i},m_{j})\).  
Algorithm 1 is applied next to perform the Extract Class refactoring on the class \(X\). The algorithm takes three inputs: \(M(X)\), a _Threshold_ value, and the matrix shown in Table 1. We set the value of _Threshold_ to 0.5, which is the median of the values in the matrix in Table 1 excluding the zero values and the main diagonal of the matrix. The output of Algorithm 1 is the following clutter of subsets,  
\(F=\{\{m_{1},m_{2},m_{3}\},\{m_{4}\},\{m_{5},m_{6},m_{7}\}\}\).  
Each subset represents a class that can be extracted from \(X\).  
Algorithm 2 can be applied next to avoid extracting classes with a small number of methods. The algorithm takes three inputs: the clutter \(F\) (i.e., the output of Algorithm 1); \(minNumMethods\), which is a chosen value for the minimum number of methods that each extracted class can have; and the matrix shown in Table 1. In this example, \(minNumMethods=2\). The following is the output of Algorithm 2,  
\(F=\{\{m_{1},m_{2},m_{3}\},\{m_{4},m_{5},m_{6},m_{7}\}\}\).  
\begin{table}
\begin{tabular}{c c c c c c c c} \hline \hline  & \(m_{1}\) & \(m_{2}\) & \(m_{3}\) & \(m_{4}\) & \(m_{5}\) & \(m_{6}\) & \(m_{7}\) \\ \hline \(m_{1}\) & 1 & 0.67 & 0.33 & 0 & 0 & 0 & 0 \\ \(m_{2}\) & 0.67 & 1 & 0.67 & 0 & 0 & 0 & 0 \\ \(m_{3}\) & 0.33 & 0.67 & 1 & 0 & 0 & 0 & 0 \\ \(m_{4}\) & 0 & 0 & 0 & 1 & 0.25 & 0.20 & 0 \\ \(m_{5}\) & 0 & 0 & 0 & 0.25 & 1 & 0.67 & 0.33 \\ \(m_{6}\) & 0 & 0 & 0 & 0.20 & 0.67 & 1 & 0.67 \\ \(m_{7}\) & 0 & 0 & 0 & 0 & 0.33 & 0.67 & 1 \\ \hline \hline \end{tabular}
\end{table}
Table 1: The client-based similarity between each pair of methods in the class \(X\).  
The output of Algorithm 2 shows that the subset \(\{m_{4}\}\) is merged with the subset \(\{m_{5},m_{6},m_{7}\}\). Thus, two classes are suggested to be extracted from the original class \(X\) in our example. The first extracted class has the following set of methods \(\{m_{1},m_{2},m_{3}\}\) and the second extracted class has the following set of methods \(\{m_{4},m_{5},m_{6},m_{7}\}\).