Upload
cleopatra-melton
View
213
Download
0
Embed Size (px)
Citation preview
CONTI 2004 [email protected]
Automatic Detection of Missing Abstract Factory Design
Pattern in Object-Oriented Code
as. eng. Călin-Viorel Jebelean
CONTI 2004 [email protected]
Design Patterns
Design pattern = a general solution to a frequently occurring design problem that guarantees better flexibility, extendibility and maintainability of object-oriented code23 such design problems have been identified and documented [GoF]A good design often contains many design pattern instancesA bad design surely needs some design pattern instances
CONTI 2004 [email protected]
Goals of the Paper
To propose a methodology for identifying places within object-oriented code where a design pattern should have been used, but wasn’t
To simply indicate suspect entities, without correcting the underlying problem
Chosen design pattern = Abstract Factory
Chosen programming language = Java
CONTI 2004 [email protected]
Abstract Factory AntiPattern (1)
Symptoms: Clients depend on a fixed family of products Hard to introduce a new family No restriction to use products from different families
CONTI 2004 [email protected]
Abstract Factory AntiPattern (2)
Client code (how it looks):…
if * product family 1 is chosen then
…new ProductA1(…);…new ProductB1(…);…
else if * product family 2 is chosen then
…new ProductA2(…);…new ProductB2(…);…
end if
Client code (how it should look):
…Factory factory =
new Factory2(…);…factory.createProductA(…);
// will create a ProductA2…factory.createProductB(…);
// will create a ProductB2…
CONTI 2004 [email protected]
The Approach
ICP of a method = Instantiations along one Control Path of a methodAn ICP for a method is a set of classes (no duplicates are allowed) instantiated along one of the control paths of that methodA method would typically contain many control paths, and also many ICPsWe should compute the ICPs for every method in the target projectThen, the conceptual algorithm would be …
CONTI 2004 [email protected]
Conceptual Algorithm
Let ICP1 and ICP2 be two different ICPs of the system
Then, ICP1 belongs to some method m1 of class C1 and ICP2 belongs to some method m2 of class C2
If: ICP1 contains 2 classes CA1 (ProductA1) and CB1 (ProductB1) ICP2 contains 2 classes CA2 (ProductA2) and CB2 (ProductB2) CA1 and CA2 are brothers CB1 and CB2 are brothers
Then: (C1 , m1) is a suspect and so is (C2 , m2)
CONTI 2004 [email protected]
The Approach – Quick View
Suspects
Metamodel (Prolog KB)
OO code (Java)
automatic
automaticmanual
CONTI 2004 [email protected]
From Sources to Metamodel
class C extends SC { public void m(int x, int y) { new X(); if (x > y) { if (x > 0) new Y(); else { new Z(); new Z(); } } else if (y > x) { new W(); } }}
% Prolog metamodel – tool generated
class(‘C’).
extends(‘C’, ‘SC’).
method(‘m’, ‘C’).
instantiates(‘m’, ‘C’, [‘X’, ‘Y’]).instantiates(‘m’, ‘C’, [‘X’, ‘Z’]).instantiates(‘m’, ‘C’, [‘X’, ‘W’]).instantiates(‘m’, ‘C’, [‘X’]).
CONTI 2004 [email protected]
From Metamodel to Suspects
suspect(C1, M1, CA1, CB1, C2, M2, CA2, CB2) :- instantiates(M1, C1, ICP1), member(CA1, ICP1), member(CB1, ICP1), not(brothers(CA1, CB1)), brothers(CA1, CA2), brothers(CB1, CB2), not(member(CA2, ICP1)), not(member(CB2, ICP1)), instantiates(M2, C2, ICP2), member(CA2, ICP2), member(CB2, ICP2).
CONTI 2004 [email protected]
Practical Results
We ran the tools on 4 medium-size projects, one of them containing a “planted” Abstract Factory anti-pattern
Metamodel generation and suspect identification were successful in under 2 seconds each
The largest project successfully analyzed had 35 Kloc
The metamodel generator ran out of memory on the JDK 1.4.2 sources
CONTI 2004 [email protected]
Conclusions & Future Work
Conclusions Abstract Factory identification is feasible on large
projects if smarter algorithms are used to compute ICPs
Metamodel generation is exponential in the number of conditionals, yet, in practical situations, this complexity does rarely show up
Future work Improve ICP computation by disregarding classes that
do not extend other classes Improve ICP computation by factoring out classes that
are instantiated on all or more control paths Imagine detection strategies for other design patterns