22 Proposal of a Design Pattern Retrieval Method with Partial Source Code Queries 1110295 2011 3 1
,... Web,.,.,,.,...,.,,,, i
Abstract Proposal of a Design Pattern Retrieval Method with Partial Source Code Queries Yasuo YAMASAKI When finding a solution to a recurring problem in designing object-oriented software, a designer can consider the use of design patterns. Design patterns are descriptions that document good historical solutions for recurring design problems. A designer can make well-designed software by using design patterns. The necessity for a search system for design patterns is increasing because design patterns are continuously produced on books and web pages. Therefore, a pattern search system with keyword queries has been researched. In this paper, a design pattern retrieval method with partial source code queries is proposed. The proposed method extracts pattern s structure from source code s structure by focusing on generalization relationship and retrieves patterns by calculating similarity score between the source code and each pattern. The algorithm that calculates similarity between graph vertices is used for calculating similarity score. Finaly, the results of an a experiment where we apply the proposed method to partial source code show its validness for the desired pattern retrieval. key words design pattern, search, matrix, similarity, generalization relationship ii
1 1 2 4 2.1................. 4 2.2........................... 4 2.3........................ 6 3 8 3.1................................ 8 3.2................................ 9 3.2.1..................... 9 3.2.2........ 11 4 15 5 17 18 19 A 20 A.1 Command............................. 20 A.2 Adapter.............................. 21 A.3 Factory Method.......................... 23 A.4 Proxy............................... 24 A.5 Composite............................. 27 iii
1.1 Command........................ 3 1.2....................... 3 1.3.......................... 3 2.1 Command........ 5 3.1.... 12 3.2................... 13 3.3 factory method..................................... 13 3.4..... 13 3.5...................... 14 3.6.. 14 iv
4.1.............................. 16 v
1,,, [1].,.,,.., Web, [6].,.,, [2].,.. 1, 2.. 1.1 Command. 1
1.2. 1.2 Command A Command B Receiver Command ConcreteCommand Receiver. 1.2 Command [1]...., 1.2 Command. Command. ConcreteCommand Receiver Command Command A Command B Receiver. Command,,.,,..,. 2
1.1 Command 1.2 1.3,,., 1.3. 2, Tsantalis [2]. 3. 4, 5. 5,. 3
2, Tsantaris [2]. 2.1 [2],,..,., [2] 1.1 Command 2.1. Command ConcreteCommand Receiver.,,. {X ij i j. 2.2 [2]. similarity scoring algorithm. Kleinberg [3], Web Blondel [4] 4
2.2 2.1 Command, 2.. G A, G B n A, n B. S n A n B. S ij G A j G B i, 2 similarity score. S. 1. Z 0 = 1. 2.,. Z k+1 = 3. S Z k. A, B G A G B. BZ ka T + B T Z k A BZ k A T + B T Z k A 1 (2.1) 5
2.3 Z 0 1 n B n A.. 1 1,. G A G B. n B n A.., i i k i [0, 1]., x, S ij x 1 x i j.,. 2.3 [2].,,,,,,,,,., 6
2.3 Observer, Composite, Decorator, Composite Adapter Command Template Method Factory Method private, static, Singleton Visitor [2]. 1,. 7
3,. 3.1,.., 1 1.2 Command 1.1. [2] Command, Command : 1 Command 2, 2 Command ConcreteCommand Receiver, Command. 1.2 2, 1, ConcreteCommand Receiver Invoker. 2 8
3.2,..,.. 1. [2].. 2.. 3.. 4. 1 3, 3.2. 4 2.2. 3.2 [1] 23 Command, Adapter, Factory Method, Proxy, Composite 5., 5,. 3.2.1. 9
3.2 generalization. association. instantiation. factory method.. method invocation. invoked method in inherited method. similar method invocation. similar method invocation from sibling subclass. iterative similar method invocation. iterative similar abstract method invocation 10
3.2., [2] factory method,,. 3.2.2. 3.2.2,.,.., 3.1,, 3.2 factory method, factory method, 11
3.2 3.1 3.3, 3.4,, 3.5,, 3.6 12
3.2 3.2 3.3 factory method 3.4 13
3.2 3.5 3.6 14
4 [1] 23, Command, Adapter, Factory Method, Proxy, Composite 5, [7][9].,... 1.,.,. 2., 3.2,. 3.., Command, Adapter, Factory Method, Proxy, Composite 5. [2].. 15
\ Command Adapter Factory Method Proxy Composite Adapter/Command TP TP TN FP TN Factory Method TN TN TP TN TN Proxy TN TN TN TP TN Composite TN TN TN TN TP 4.1 True-Positive TP :,. True-Negative TN :,. False-Positive FP :,. False-Negative FN :,. 4.1. 4.1, Adapter Command., [2]. Proxy, Adapter/Command.,.,. 16
5,.,, [2]., [7][9],,..,,.,..,.,,,.,.,.,. 17
.,,,..,,,..,..,,. 18
[1] E. Gamma, R. Helm, R. Johnson, and J. Vlissides,,,,, 1999. [2] N. Tsantalis, A. Chatzigeorgiou, G. Stephanides, and S. Halkidis, Design Pattern Detection Using Similarity Scoring, IEEE Transaction on Software Engineering, vol.32, no.11, pp.896 909, 2006. [3] J.M. Kleinberg, Authoritative Sources in a Hyperlinked Environment, Journal of the ACM, vol.46, no.5, pp.604 632, 1999. [4] V.D. Blondel, A. Gajardo, M. Heymans, P. Senellart, and P. VanDooren, A Measure of Similarity between Graph Vertices: Applications to Synonym Extraction and Web Searching, SIAM Review, vol.46, no.4, pp.647 666, 2004. [5],,, FIT2010 9, pp.289 290, 2010. [6],,,,,, vol.25, no.2, pp.114 134, 2008. [7] Huston Design Patterns, http://www.vincehuston.org/dp/, 2011. [8] Design Pattern detection using Similarity Scoring, http://java.uom.gr/ nikos/pattern-detection.html, 2011. [9], Java,, 2002. 19
A 4,,. Java. A.1 Command Huston design pattern Java demos [8]. class Fan { public void startrotate() { System.out.println("Fan is rotating"); public void stoprotate() { System.out.println("Fan is not rotating"); 20
A.2 Adapter class FanOnCommand { private Fan myfan; public FanOnCommand ( Fan F) { myfan = F; public void execute( ) { myfan. startrotate( ); class FanOffCommand { private Fan myfan; public FanOffCommand ( Fan F) { myfan = F; public void execute( ) { myfan. stoprotate( ); A.2 Adapter Huston Design Pattern [7] Adapter Example. 21
A.2 Adapter class LegacyLine { public void draw( int x1, int y1, int x2, int y2 ) { System.out.println( "line from (" + x1 +, + y1 + ") to (" + x2 +, + y2 + ) ); class LegacyRectangle { public void draw( int x, int y, int w, int h ) { System.out.println( "rectangle at (" + x +, + y + ") with width " + w + " and height " + h ); class Line { private LegacyLine adaptee = new LegacyLine(); public void draw( int x1, int y1, int x2, int y2 ) { adaptee.draw( x1, y1, x2, y2 ); class Rectangle { private LegacyRectangle adaptee = new LegacyRectangle(); public void draw( int x1, int y1, int x2, int y2 ) { adaptee.draw( Math.min(x1,x2), Math.min(y1,y2), Math.abs(x2-x1), Math.abs(y2-y1) ); 22
A.3 Factory Method A.3 Factory Method Huston design pattern Java demos [8]. interface Product { public String getname(); class ConcreteProductA implements Product{ public String getname() { return "ProductA"; class ConcreteProductB implements Product { public String getname() { return "ProductB"; 23
A.4 Proxy class ConcreteCreatorA { public Product factorymethod() { return new ConcreteProductA(); class ConcreteCreatorB { public Product factorymethod() { return new ConcreteProductB(); A.4 Proxy Java [9, pp.323 335]. class Printer { private String name; public Printer() { heavyjob("printer "); public Printer(String name) { // this.name = name; heavyjob("printer (" + name + ") "); 24
A.4 Proxy public void setprintername(string name) { // this.name = name; public String getprintername() { // return name; public void print(string string) { // System.out.println("=== " + name + " ==="); System.out.println(string); private void heavyjob(string msg) { // ( ) System.out.print(msg); for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); catch (InterruptedException e) { System.out.print("."); System.out.println(" "); class PrinterProxy { private String name; // 25
A.4 Proxy private Printer real; // public PrinterProxy() { public PrinterProxy(String name) { // this.name = name; public synchronized void setprintername(string name) { // if (real!= null) { real.setprintername(name); // this.name = name; public String getprintername() { // return name; public void print(string string) { // realize(); real.print(string); private synchronized void realize() { // if (real == null) { real = new Printer(name); 26
A.5 Composite A.5 Composite Java [9, pp.153 167]. import java.util.iterator; import java.util.arraylist; class File { private String name; private int size; public File(String name, int size) { this.name = name; this.size = size; public String getname() { return name; public int getsize() { return size; class Directory { private String name; // 27
A.5 Composite private ArrayList directory = new ArrayList(); // private ArrayList file = new ArrayList(); public Directory(String name) { // this.name = name; public String getname() { // return name; public int getsize() { // int size = 0; Iterator itfile = file.iterator(); Iterator itdir = directory.iterator(); while (itdir.hasnext()) { Directory direntry = (Directory)itDir.next(); size += direntry.getsize(); while (itfile.hasnext()) { File fileentry = (File)itFile.next(); size += fileentry.getsize(); return size; public Directory adddirectory(directory entry) { // directory.add(entry); 28
A.5 Composite return this; public Directory addfile(file entry) { // file.add(entry); return this; 29