Software modularity and reuse Paulo Borba Informatics Center Federal University of Pernambuco
[email protected] ◈ twitter.com/pauloborba Monday, September 20, 2010
1
Components and parametrization
Monday, September 20, 2010
2
Different devices, 15 to 60 different applications…
Different clients, different products
http://www.androidauthority.com Monday, September 20, 2010
3
Reuse problems
Monday, September 20, 2010
4
Modularity problems
Monday, September 20, 2010
5
Refactoring problematic component
Test 1 Test 2
improved component
Test 1 Test 2
...
...
Monday, September 20, 2010
refactoring
Test n
Test n
6
Patterns
Monday, September 20, 2010
7
Variability patterns (avoiding code duplication in product families) Monday, September 20, 2010
8
Template method
http://www.refactoring.com/catalog/formTemplateMethod.html Monday, September 20, 2010
9
PDC, bridge variation... class AccountRecord { private AccountRepository accounts;... public void register(Account a) { if (a != null) { String n = a.getNumero(); if (!accounts.exists(n)) { accounts.insert(a); } } } Monday, September 20, 2010
10
variation is whole data structure interface AccountRepository { void insert(Account account); Account search(String number); boolean exists(String number); } class AccountArray implements AccountRepository {...} class AccountVector implements AccountRepository {...} Monday, September 20, 2010
11
Compare patterns
Monday, September 20, 2010
12
But how are variants chosen? Monday, September 20, 2010
13
Product family assets Java files, abstract
Java files, concrete
Java files, decision ... new AccountArray ... ...
Makefiles
Monday, September 20, 2010
new AccountVector ...
14
Parametrized factory... Java files, abstract
Java files, concrete
Java file, decision ... if (...) {... new AccountArray
...
} else {... new AccountVector
Makefiles
...
} ...
Monday, September 20, 2010
15
with property (or XML) file Properties p = new Properties(); p.load(new FileInputStream("repository.prop")); ... if (p.getProperty("ACCOUNT_REP") == 1) { ... } else {...} ...
Monday, September 20, 2010
16
Property files as assets Java files, abstract
Java files, concrete
Java file, decision ... if (...) {...} else {...} ...
Property files Makefiles
ACCOUNT_REP=1 ACCOUNT_REP=2
Monday, September 20, 2010
17
Properties parametrize behavior and data Properties p = new Properties(); p.load(new FileInputStream("device_screen.prop")); ... draw(p.getProperty("SCREEN_WIDTH"),50); ...
Monday, September 20, 2010
18
Compare mechanisms
Monday, September 20, 2010
19
Analyzing subtype polymorphism • Variation point: supertypes (methods) in the program text • Variation: subtypes (including methods and fields) • Binding time: execution time, based on makefile choice (compilation/build time) • Mechanism: subtype definition, variation modularization Monday, September 20, 2010
20
Analyzing file parametrization • Variation point: expressions in the program text, and commands too (with conditionals) • Variation: constants and values, commands • Binding time: execution time, based on makefile choice (build time) • Mechanism: value instantiation, decision
Monday, September 20, 2010
21
Decision mechanisms for subtype polymorphism • Makefiles (different mains or factories)
developer
• Makefiles + property_files + conditionals • Makefiles + property_files + reflection • Input + conditionals
user
• Input + reflection
Monday, September 20, 2010
22
Environment decision, with dependency injection... class AccountRecord { private AccountRepository accounts; void setRepository(AccountRepository r) { accounts = r; } ... Not a developer decision, nor } a user decision!
Monday, September 20, 2010
23
Components • Improvement over subtype polymorphism and Java packages • not by defining subtypes of abstract classes, but of interfaces • clientship + subtype polymorphism • deployment independence • service location and management • module visibility and versions
Monday, September 20, 2010
24
Containers Client
component component
XML
Java Java Java
component component
component
Monday, September 20, 2010
25
Extending Eclipse...
Monday, September 20, 2010
26
and allowing extensions Monday, September 20, 2010
27
Acessing variations ep = Platform...getExtensionPoint("ELP_EP.languageSet"); IExtension[] extensions = ep.getExtensions(); for (IExtension e : extensions) { configs = e.getConfigurationElements(); for (IConfigurationElement c : configs) { if (c.getName().equals("language")) { ...new Path(c.getAttribute("languageFile")); } } }
Monday, September 20, 2010
28
Components in Spring
http://www.developer.com/img/articles/2005/04/13/List108.gif Monday, September 20, 2010
29
When and where to use this?
Monday, September 20, 2010
30
Parametrization via parametric polymorphism public class Record
{ ... public void insert(ComplaintType o) { ... } } ... Record ... Monday, September 20, 2010
31
Parametric polymorphism core assets Java file, generic Makefiles
Java files, specific ... Record ... Monday, September 20, 2010
... Record ... 32
Parametrizing data + operations public class Record< ComplaintType extends CodeIdentifiable> { ... public ComplaintType search(int code){ ... } public void insert(ComplaintType o) { ... } interface CodeIdentifiable { } int getCode(); } Monday, September 20, 2010
33
Analyzing parametric polymorphism • Variation point: class parameters (types and methods) in the program text • Variation: types (including methods) • Binding time: compilation time, based on makefile choice • Mechanism: type instantiation, variation and “common” code modularization Monday, September 20, 2010
34
Black belt parametrization! Monday, September 20, 2010
35
Smells good?
functions as parameters
artigos.each{|a| map = {} a.elements.each("DADOS-BASICOS-DO-ARTIGO"){|d| atts = d.attributes map["TITULO-DO-ARTIGO"] = atts["TITULO-DO-ARTIGO"] map["ANO-DO-ARTIGO"] = atts["ANO-DO-ARTIGO"] } a.elements.each("DETALHAMENTO-DO-ARTIGO"){|d| atts = d.attributes map["TITULO-DO-MEIO"] = atts["TITULO-DO-MEIO"] map["VOLUME"] = atts["VOLUME"] map["FASCICULO"] = atts["FASCICULO"] map["PAGINA-INICIAL"] = atts["PAGINA-INICIAL"] map["PAGINA-FINAL"] = atts["PAGINA-FINAL"] } ... Monday, September 20, 2010
36
Not only for articles... ... a.elements.each("AUTORES"){|author| map["AUTORES"] = (if map["AUTORES"] then map["AUTORES"] else [] end) + [author.attributes["NOME-PARA-CITACAO"]] } }
Monday, September 20, 2010
37
XML structure as parameter artigos.each{|a| map = {} structure = { "DADOS-BASICOS-DO-ARTIGO" => ["TITULO-DO-ARTIGO", "ANO-DO-ARTIGO"], "DETALHAMENTO-DO-ARTIGO" => ["TITULO-DO-MEIO", "VOLUME", "FASCICULO", "PAGINA-INICIAL", "PAGINA-FINAL"], "AUTORES" => ["*", "NOME-PARA-CITACAO"]} }
Monday, September 20, 2010
38
Abstracting structure details structure.keys.each {|e| a.elements.each(e) {|d| atts = d.attributes if structure[e].include?("*") then (structure[e] - ["*"]).each {|att| map[e] = (if map[e] then map[e] else [] end) + [atts[att]] } else structure[e].each {|att| map[att] = atts[att] } end } } Monday, September 20, 2010
39
Improvement is more often needed than not, so abstract to see it! Monday, September 20, 2010
40
Take notes, now! Monday, September 20, 2010
41
Components and parametrization
Monday, September 20, 2010
42
Software modularity and reuse Paulo Borba Informatics Center Federal University of Pernambuco
[email protected] ◈ twitter.com/pauloborba Monday, September 20, 2010
43