• Non ci sono risultati.

Refactoring of Design Patterns from Object-Oriented to Aspect-Oriented: a practical approach

N/A
N/A
Protected

Academic year: 2021

Condividi "Refactoring of Design Patterns from Object-Oriented to Aspect-Oriented: a practical approach"

Copied!
39
0
0

Testo completo

(1)

Refactoring of Design Patterns from Object-Oriented to Aspect-Oriented: a practical approach

DIPARTIMENTO di

MATEMATICA e INFORMATICA

(2)

Daniele Contarino System 2015 – September 28, 2015 2 of 39

Introduction

 Recognition process

 Conversion from OODP to AODP

 Example: Singleton DP

 Annotations

Executive summary

(3)

The Design Patterns are …

Introduction

… of developers!

(4)

Daniele Contarino System 2015 – September 28, 2015 4 of 39

The Aspect-Oriented Programming we allow to separate the main concerns from cross-cutting concerns

Introduction

OOP classes

Aspects

(5)

Introduction

(6)

Daniele Contarino System 2015 – September 28, 2015 6 of 39

+

Introduction

Aspect Oriented

Programming = Oriented Design Aspect

Patterns

(7)

Introduction

import java.util.WeakHashMap;

public aspect MySingletonAOP {

private WeakHashMap<String, MyST> singles = new WeakHashMap<String, MyST>();

pointcut trapCreate() : call((MyST).new(..))

&& !within(MySingletonAOP);

Object around() : trapCreate() {

Object obj = singles.get("MyST");

if (obj == null) { obj = proceed();

singles.put(className, obj);

}

return obj;

};

}

public class MyST { private int attr;

public MyST(){

this.attr = 1;

}

public void setAttr(int attr){

this.attr = attr;

}

public int getAttr(){

return attr;

} }

(8)

Daniele Contarino System 2015 – September 28, 2015 8 of 39

Introduction

Design pattern in Aspect-Oriented:

Improves the code reusabilty

Reduce project complexity

Split from code the business logic (domain concerns)

and design pattern features (non-domain concerns)

(9)

Introduction

In real world

Convert all Object- Oriented Design Pattens into

Aspect Oriented Design Pattens …

… in a (very)

BIG software

project!

(10)

Daniele Contarino System 2015 – September 28, 2015 10 of 39

Introduction

(11)

 Introduction

Recognition process

 Conversion from OODP to AODP

 Example: Singleton DP

 Annotations

Executive summary

(12)

Daniele Contarino System 2015 – September 28, 2015 12 of 39

Recognition process

For recognize a design pattern, we observe their tipical

"signature" or rather the

design pattern features.

(13)

Recognition process

We propose three research approach:

1. Research a set of implemented class or inherited from a specific class

2. Match between interface with objects that

implement them

3. Check if may exists some

attributes and/or methods

(14)

Daniele Contarino System 2015 – September 28, 2015 14 of 39

Recognition process

An example of this research is the DP Observer search, because the relevant classes implement the Observer interface and

Observable (included in the JDK standard library).

1. Research a set of implemented class or inherited

from a specific class

(15)

Recognition process

Stating from a given project, we get all interface. We try to

observe where they are implemented and the behavior of the classes that implement the same interface. In case of DP

Composite it will be usefull to get a classes’ list that implements a determinated interface. We will check if a specific class contains a list of typed attributes with the same interface (a list of leafs).

2. Match between interface with objects that

implement them

(16)

Daniele Contarino System 2015 – September 28, 2015 16 of 39

Recognition process

(17)

Recognition process

Other examples of this type of recognizer is: Proxy, Factory

Method and Abstract Factory (search a interface that have a

method whose return type is another interface)

(18)

Daniele Contarino System 2015 – September 28, 2015 18 of 39

Recognition process

Some DP are characterized by the presence of some specific

attributes or properties concerning the methods (or assimilable as the constructors). The DP Singleton for example has a private constructor, a private static attribute of the same type of the

class and a static method that return a object of the same type of the class. Other examples of this type of recognizer is: Flyweight, Proxy, Object Pool (with a single Creator, like Singleton)

3. Check if may exists some attributes and/or methods

(19)

Recognition process

(20)

Daniele Contarino System 2015 – September 28, 2015 20 of 39

 Introduction

 Recognition process

Conversion from OODP to AODP

 Example: Singleton DP

 Annotations

Executive summary

(21)

Conversion from OODP to AODP

OODP

AODP

1. Project analysis and research of the classes that implements a

particular DP

2. Creation of the object that will perform the conversion

3. Creation of the Aspects

4. Change the code of the

project

(22)

Daniele Contarino System 2015 – September 28, 2015 22 of 39

Conversion from OODP to AODP

Base toolkit

(23)

Conversion from OODP to AODP

getDesignerPattern(IJavaProject)

take in input a Java project e return a array of AspectDesignerPattern objects already setted for carry out the

refactoring

OODP

AODP

(24)

Daniele Contarino System 2015 – September 28, 2015 24 of 39

Conversion from OODP to AODP

createAspect()

to create a custom Aspect for he found class; he use AspectPrototype class who noted the various internal elements of Aspect (pointcuts, advice, attributes, etc.) and write the code in the .aj file into indicated package

OODP

AODP

(25)

Conversion from OODP to AODP

replaceCode()

to delete and/or modify all those parts of code characterized a given DP in OO paradigm.

OODP

AODP

(26)

Daniele Contarino System 2015 – September 28, 2015 26 of 39

 Introduction

 Recognition process

 Conversion from OODP to AODP

Example: Singleton DP

Annotations

Executive summary

(27)

Example: Singleton DP

(28)

Daniele Contarino System 2015 – September 28, 2015 28 of 39

Example: Singleton DP

public static AspectDesignePattern getDesignePattern(ICompilationUnit classFile, IJavaProject project){

if(classFile == null) return null;

String className;

IMethod[] methods;

try {

for(IType classStub : classFile.getTypes()){

className = classStub.getElementName();

methods = classStub.getMethods();

IMethod privateConstructor = Singleton.getOnlyPrivateConstructor(methods);

if( privateConstructor == null) return null;

IMethod getInstanceMethod = Singleton.getInstanceMethod(methods, className);

if( getInstanceMethod == null) return null;

IField[] fields = classStub.getFields();

IField staticInstance = Singleton.getInstanceAttribute(fields, className);

if(staticInstance == null) return null;

return new Singleton(classFile, project, classStub, privateConstructor, getInstanceMethod, staticInstance);

}

} catch (JavaModelException e) {}

return null;

}

(29)

Example: Singleton DP

private static IMethod getOnlyPrivateConstructor(IMethod[] methods) throws JavaModelException{

int numOfPrivateConstructors = 0, privateConstructorsIndex = -1;

for(int i=0; i < methods.length; i++) if(methods[i].isConstructor() &&

Flags.isPrivate(methods[i].getFlags())) { numOfPrivateConstructors++;

privateConstructorsIndex = i;

}

if(numOfPrivateConstructors != 1) return null;

else return methods[privateConstructorsIndex];

}

(30)

Daniele Contarino System 2015 – September 28, 2015 30 of 39

Example: Singleton DP

private static IMethod getInstanceMethod(IMethod[] methods, String className) throws JavaModelException{

for(int i=0; i < methods.length; i++)

if(methods[i].getReturnType().compareTo("Q"+className+";") == 0 &&

Flags.isPublic(methods[i].getFlags()) &&

Flags.isStatic(methods[i].getFlags()))

return methods[i];

return null;

}

private static IField getInstanceAttribute(IField[] fields, String className) throws JavaModelException{

for(int i=0; i < fields.length; i++)

if(fields[i].getTypeSignature().compareTo("Q"+className+";") == 0 &&

Flags.isStatic(fields[i].getFlags())) return fields[i];

return null;

}

(31)

Example: Singleton DP

MyClass uniqueObject = MyClass.getInstance();

MyClass uniqueObject = new MyClass();

After conversion, all calls like

will be replaced by

(32)

Daniele Contarino System 2015 – September 28, 2015 32 of 39

 Introduction

 Recognition process

 Convertion from OODP to AODP

 Example: Singleton DP

Annotations

Executive summary

(33)

Annotation

Despite the use of the Aspect-Oriented

programming, the non- domanis concers is still related with business logic (often in Aspect code we find the name of the class)

import java.util.WeakHashMap;

public aspect MySingletonAOP {

private WeakHashMap<String, MyST> singles = new WeakHashMap<String, MyST>();

pointcut trapCreate() : call((MyST).new(..))

&& !within(MySingletonAOP);

Object around() : trapCreate() {

Object obj = singles.get("MyST");

if (obj == null) { obj = proceed();

singles.put(className, obj);

}

return obj;

};

(34)

Daniele Contarino System 2015 – September 28, 2015 34 of 39

Annotation

We propose another solution using Aspects and Java Annotation

package it.unict.dmi.aoa.singleton;

import java.util.WeakHashMap;

public aspect SingletonPattern {

private WeakHashMap<String, Object> singles = new WeakHashMap<String, Object>();

pointcut trapCreate() :

call((@Singleton *).new(..)) && !within(SingletonPattern);

Object around() : trapCreate() { String className =

thisJoinPoint.getSignature().getDeclaringTypeName();

Object obj = singles.get(className );

if (obj == null) { obj = proceed();

singles.put(className, obj);

}

return obj;

};

}

@Singleton

public class MyST { private int attr;

public MyST(){

this.attr = 1;

}

public void setAttr(int attr){

this.attr = attr;

}

public int getAttr(){

return attr;

} }

(35)

Future works

 Complete the recognition of all design patterns;

 Parallelize the recognition;

 Create a library with all Aspect- Oriented

Design Patterns with Annotation;

(36)

Daniele Contarino System 2015 – September 28, 2015 36 of 39

References

1. G. Kiczales, J. Lamping, A. Mendhekar, C. Maeda, C. Lopes, J.M. Loingtier, and J. Irwin, “Aspect-oriented programming,”

vol. 1241, pp. 220–242.

2. R. Giunta, G. Pappalardo, and E. Tramontana, “Aodp:

Refactoring code to provide advanced aspect-oriented

modularization of design patterns”, Proceedings of the 27th

Annual ACM Symposium on Applied Computing, 2012.

(37)

References

3. R. Giunta, G. Pappalardo, and E. Tramontana, “Using aspects and annotations to separate application code from design patterns”, Proceedings of the 2010 ACM Symposium on Applied Computing, 2010.

4. R. Giunta, G. Pappalardo, and E. Tramontana, “Aspects and

annotations for controlling the roles application classes

play for design patterns”, Proceedings of the 2011 18th

Asia-Pacific Software Engineering Conference, 2011.

(38)

Daniele Contarino System 2015 – September 28, 2015 38 of 39

References

5. D. Sardina and E. Tramontana, “Realizzazione di un plug-in di eclipse per la generazione di codice ad aspetti che

implementa alcuni design pattern”, Università di Catania,

2012.

(39)

DIPARTIMENTO di

MATEMATICA e INFORMATICA

Refactoring of Design Patterns from Object-Oriented to Aspect-Oriented: a practical approach

Thank you for you attention!

Riferimenti

Documenti correlati

Non portable programs (must be recompiled) It is possible to declare a variable (or a function) and later define it (difference between .c and .h) Variables and functions can be in

res and s[] are local variables scope: body of function main lifetime: duration of the program. Lipari (Scuola Superiore Sant’Anna) Introduction to C++ November 15, 2010 27

The expected benefits of applying well-know design structures Finding the right code structure (which classes, their relationship)..

Lipari (Scuola Superiore Sant’Anna) OO Design Principles March 13, 2011 1 / 47.. An object

public void setSender(String sender) { /* set sender;*/ } public void setReceiver(String receiver) { /* set receiver;*/ } public void setContent(String content) { /* set content;*/

Lipari (Scuola Superiore Sant’Anna) OO Design Principles October 28, 2011 1 /

You might be tempted to make the constructor private, so the only way to construct an instance is to use the static factory method however, keep in mind that, if the constructor

Lipari (Scuola Superiore Sant’Anna) Design Patterns November 11, 2011 4 / 60..