Paradigmas de Linguagens de Programação - Biblioteca de Classes e Frameworks
Paradigmas de linguagens de programacao - aula#9
-
Upload
ismarfrango -
Category
Education
-
view
1.220 -
download
0
description
Transcript of Paradigmas de linguagens de programacao - aula#9
![Page 1: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/1.jpg)
Paradigmas de Linguagens de Programação
Paradigma Orientado a AspectosAula #9
(CopyLeft)2009 - Ismar Frango [email protected]
![Page 2: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/2.jpg)
A Programação Orientada por Aspectos (AOP), proposta por Gregor Kiczales em 1997 tem por objetivo modularizar decisões de projeto que não podem ser adequadamente definidas por meio da POO.
Na AOP, requisitos de sistemas são modelados por meio de classes, que implementam os objetos do mundo real, e aspectos, que implementamrequisitos transversais do sistema
AOP
![Page 3: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/3.jpg)
Melhora a modularidade de crosscutting concerns
Exemplos: distribuição, gerenciamento de dados, controle de concorrência, tratamento de exceções, logging, debugging, …
Auxilia na separation of concerns
Benefícios da AOP
“Programs that clearly express the design structure they implement are easier to maintain”Gregor Kiczales
![Page 4: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/4.jpg)
Exemplo: Hello, world (que novidade!)
class Hello{public static void main(String args[ ]){ System.out.println("Início da Monitoração"); System.out.println("Hello World!"); System.out.println("Final da monitoração");}}
Tangled code
![Page 5: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/5.jpg)
Exemplo: Hello, world (novidade!)
aspect Monitor{
pointcut impressao(): execution (public static void Hello.main(String []));
before(): impressao () { System.out.println("Inicio da impressao"); } after(): impressao () { System.out.println("Final da impressao"); }
}
class Hello{ public static void main(String args[ ]){ System.out.println("Hello World!"); }}
Hello.java
Monitor.aj
![Page 6: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/6.jpg)
Weaving
Hello.java
Monitor.aj
ajc
Hello.class
Monitor.class
![Page 7: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/7.jpg)
Exemplo: Logger OO
Exemplo de Tirelo et al. – JAI2004
![Page 8: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/8.jpg)
Exemplo: Logger AOP
public aspect LoggingAspect {pointcut publicMethods(): execution (public * *(..));pointcut logObjectCalls() : execution(* Logger.*(..));pointcut loggableCalls() : publicMethods() && ! logObjectCalls();
before(): loggableCalls() {Logger.logEntry(thisJoinPoint.getSignature().toString());}
after() returning: loggableCalls() {Logger.logNormalExit(thisJoinPoint.getSignature().toString());}
![Page 9: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/9.jpg)
Exemplo de Sérgio Soares e Paulo Borba - UFPE
Exemplo: Disque-saúde com RMI
![Page 10: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/10.jpg)
Implementação com RMIpublic class Complaint implements java.io.Serializable {
private String description;
private Person complainer; ...
public Complaint(String description, Person complainer, ...) {
...
}
public String getDescription() {
return this.description;
}
public Person getComplainer() {
return this.complainer;
}
public void setDescription(String desc) {
this.description = desc;
}
public void setComplainer(Person complainer) {
this.complainer = complainer;
} ...
}
public class HealthWatcherFacade implements IFacade {
public void update(Complaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException {
...
}
public static void main(String[] args) {
try {
HealthWatcherFacade facade = HealthWatcherFacade.getInstance();
System.out.println("Creating RMI server...");
UnicastRemoteObject.exportObject(facade);
java.rmi.Naming.rebind("/HealthWatcher");
System.out.println("Server created and ready.");
}
catch (RemoteException rmiEx) {... }
catch (MalformedURLException rmiEx) { ...}
catch(Exception ex) {... }
}
}
public class ServletUpdateComplaintData extends HttpServlet {
private IFacade facade;
public void init(ServletConfig config) throws ServletException {
try {
facade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher");
}
catch (java.rmi.RemoteException rmiEx) {...}
catch (java.rmi.NotBoundException rmiEx) {...}
catch (java.net.MalformedURLException rmiEx) {...}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
facade.update(complaint);
...
} ...
}
public class Person implements java.io.Serializable {
private String nome; ...
public Person(String nome, …) {
this.nome = nome; …
}
public String getNome() {
return nome;
} …
}public interface IFacade extends java.rmi.Remote {
public void updateComplaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException,
RemoteException;
. . .
}
![Page 11: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/11.jpg)
Visão geral do código OO
public class Complaint implements java.io.Serializable {
private String description;
private Person complainer; ...
public Complaint(String description, Person complainer, ...) {
...
}
public String getDescription() {
return this.description;
}
public Person getComplainer() {
return this.complainer;
}
public void setDescription(String desc) {
this.description = desc;
}
public void setComplainer(Person complainer) {
this.complainer = complainer;
} ...
}
public interface IFacade extends java.rmi.Remote {
public void updateComplaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException,
RemoteException;
. . .
}
public class HealthWatcherFacade implements IFacade {
public void update(Complaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException {
...
}
public static void main(String[] args) {
try {
HealthWatcherFacade facade = HealthWatcherFacade.getInstance();
System.out.println("Creating RMI server...");
UnicastRemoteObject.exportObject(facade);
java.rmi.Naming.rebind("/HealthWatcher");
System.out.println("Server created and ready.");
}
catch (RemoteException rmiEx) {... }
catch (MalformedURLException rmiEx) { ...}
catch(Exception ex) {... }
}
}
public class Person implements java.io.Serializable {
private String nome; ...
public Person(String nome, …) {
this.nome = nome; …
}
public String getNome() {
return nome;
} …
}
public class ServletUpdateComplaintData extends HttpServlet {
private IFacade facade;
public void init(ServletConfig config) throws ServletException {
try {
facade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher");
}
catch (java.rmi.RemoteException rmiEx) {...}
catch (java.rmi.NotBoundException rmiEx) {...}
catch (java.net.MalformedURLException rmiEx) {...}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
facade.update(complaint);
...
} ...
}
Código RMI
![Page 12: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/12.jpg)
Problemas na implementação OO
• Tangled code (código entrelaçado)– lógicade distribuição misturado com
lógicade negócio
• Spread code (código espalhado)– código de distribuição em várias classes– distribuição é um crosscutting concern
• Difícil de manter e reusar– mudanças no protocolo de distribuição
(RMI CORBA EJB ) são invasivas
![Page 13: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/13.jpg)
Disque-saúde com AOP
![Page 14: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/14.jpg)
Implementação em AspectJpublic class Complaint {
private String description;
private Person complainer; ...
public Complaint(String description, Person complainer, ...) {
...
}
public String getDescription() {
return this.description;
}
public Person getComplainer() {
return this.complainer;
}
public void setDescription(String desc) {
this.description = desc;
}
public void setComplainer(Person complainer) {
this.complainer = complainer;
}
}
public class ServletUpdateComplaintData extends HttpServlet {
private HealthWatcherFacade facade;
public void init(ServletConfig config) throws ServletException {
try {
facade = HealthWatcherFacade.getInstance();
}
catch (Exception ex) {...}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
} ...
}
public class Person {
private String nome; ...
public Person(String nome, ...) {
this.matricula = matricula; ...
}
public String getNome() {
return nome;
} ...
}
public class HealthWatcherFacade {
public void update(Complaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException {
...
}
}
public interface IFacade extends java.rmi.Remote {
public void updateComplaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException,
RemoteException;
. . .
}
aspect DistributionAspect {
declare parents: HealthWatcherFacade implements IFacade;
declare parents: Complaint || Person implements java.io.Serializable;
public static void HealthWatcherFacade.main(String[] args) {
try {
HealthWatcherFacade facade = HealthWatcherFacade.getInstance();
System.out.println("Creating RMI server...");
UnicastRemoteObject.exportObject(facade);
java.rmi.Naming.rebind("/HealthWatcher");
System.out.println("Server created and ready.");
} catch (RemoteException rmiEx) {...}
catch (MalformedURLException rmiEx) {...}
catch(Exception ex) {...}
}
//segue...
private IFacade remoteFacade;
pointcut facadeMethodsExecution():
within(HttpServlet+) && execution(* HealthWatcherFacade.*(..)) &&
this(HealthWatcherFacade);
before(): facadeMethodsExecution() { prepareFacade();}
private synchronized void prepareFacade() {
if (healthWatcher == null) {
try { remoteFacade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher");
} catch (java.rmi.RemoteException rmiEx) {...}
catch (java.rmi.NotBoundException rmiEx) {...}
catch (java.net.MalformedURLException rmiEx) {...}
}
void around(Complaint complaint) throws TransactionException, RepositoryException
ObjectNotFoundException,ObjectNotValidException:
facadeRemoteExecutions() && args(complaint) &&
call(void update(Complaint)) {
try { remoteFacade.update(complaint);
} catch (RemoteException rmiEx) {...}
}
}
![Page 15: Paradigmas de linguagens de programacao - aula#9](https://reader036.fdocument.pub/reader036/viewer/2022081512/555c3d83d8b42a2c068b4b59/html5/thumbnails/15.jpg)
Disque-Saúde em AOP : Visão geral
public class Complaint {
private String description;
private Person complainer; ...
public Complaint(String description, Person complainer, ...) {
...
}
public String getDescription() {
return this.description;
}
public Person getComplainer() {
return this.complainer;
}
public void setDescription(String desc) {
this.description = desc;
}
public void setComplainer(Person complainer) {
this.complainer = complainer;
}
}
public interface IFacade extends java.rmi.Remote {
public void updateComplaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException,
RemoteException;
. . .
}
public class HealthWatcherFacade {
public void update(Complaint complaint)
throws TransactionException, RepositoryException,
ObjectNotFoundException, ObjectNotValidException {
...
}
}
public class Person {
private String nome; ...
public Person(String nome, ...) {
this.matricula = matricula; ...
}
public String getNome() {
return nome;
} ...
}
public class ServletUpdateComplaintData extends HttpServlet {
private HealthWatcherFacade facade;
public void init(ServletConfig config) throws ServletException {
try {
facade = HealthWatcherFacade.getInstance();
}
catch (Exception ex) {...}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
} ...
}
aspect DistributionAspect {
declare parents: HealthWatcherFacade implements IFacade;
declare parents: Complaint || Person implements java.io.Serializable;
public static void HealthWatcherFacade.main(String[] args) {
try {
HealthWatcherFacade facade = HealthWatcherFacade.getInstance();
System.out.println("Creating RMI server...");
UnicastRemoteObject.exportObject(facade);
java.rmi.Naming.rebind("/HealthWatcher");
System.out.println("Server created and ready.");
} catch (RemoteException rmiEx) {...}
catch (MalformedURLException rmiEx) {...}
catch(Exception ex) {...}
}
private IFacade remoteFacade;
pointcut facadeMethodsExecution():
within(HttpServlet+) && execution(* HealthWatcherFacade.*(..)) &&
this(HealthWatcherFacade);
before(): facadeMethodsExecution() { prepareFacade();}
private synchronized void prepareFacade() {
if (healthWatcher == null) {
try { remoteFacade = (IFacade) java.rmi.Naming.lookup("//HealthWatcher");
} catch (java.rmi.RemoteException rmiEx) {...}
catch (java.rmi.NotBoundException rmiEx) {...}
catch (java.net.MalformedURLException rmiEx) {...}
}
void around(Complaint complaint) throws TransactionException, RepositoryExceptio n
ObjectNotFoundException,ObjectNotValidException:
facadeRemoteExecutions() && args(complaint) &&
call(void update(Complaint)) {
try { remoteFacade.update(complaint);
} catch (RemoteException rmiEx) {...}
}
}
Sistema local
Aspectos de Distribuição para RMI