Paradigmas de Linguagens de Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenz...
AOP A Programação Orientada por Aspectos (AOP), proposta por Gregor Kiczales em 1997 tem por objetivo modularizar decisões...
Benefícios da AOP <ul><li>Melhora a modularidade de  crosscutting concerns </li></ul><ul><ul><li>Exemplos: distribuição, g...
Exemplo: Hello, world  (que novidade!)   class  Hello{ public static void  main(String args[ ]) {  System.out.println(&quo...
Exemplo: Hello, world  (novidade!) aspect  Monitor{ pointcut  impressao(): execution  ( public static void  Hello.main(Str...
Weaving Hello.java Monitor.aj ajc Hello.class Monitor.class
Exemplo: Logger OO Exemplo de Tirelo et al. – JAI2004
Exemplo: Logger AOP public aspect  LoggingAspect { pointcut  publicMethods():  execution  (public  * * (..)); pointcut  lo...
Exemplo: Disque-saúde com RMI Exemplo de Sérgio Soares e Paulo Borba - UFPE
Implementação com RMI public class Complaint  implements java.io.Serializable  { private String description; private Perso...
Visão geral do código OO public class Complaint  implements java.io.Serializable  { private String description; private Pe...
Problemas na implementação OO <ul><li>Tangled code  (código entrelaçado) </li></ul><ul><ul><li>lógicade distribuição mistu...
Disque-saúde com AOP
Implementação em AspectJ public class Complaint { private String description; private Person complainer; ...  public Compl...
Disque-Saúde em AOP : Visão geral public class Complaint { private String description; private Person complainer; ...  pub...
Upcoming SlideShare
Loading in...5
×

E:\Plp 2009 2\Plp 9

529

Published on

Paradigmas de Linguagens de Programacao
Aula #9

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
529
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

E:\Plp 2009 2\Plp 9

  1. 1. Paradigmas de Linguagens de Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
  2. 2. AOP 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 implementam requisitos transversais do sistema
  3. 3. Benefícios da AOP <ul><li>Melhora a modularidade de crosscutting concerns </li></ul><ul><ul><li>Exemplos: distribuição, gerenciamento de dados, controle de concorrência, tratamento de exceções, logging, debugging, … </li></ul></ul><ul><li>Auxilia na separation of concerns </li></ul>“ Programs that clearly express the design structure they implement are easier to maintain” Gregor Kiczales
  4. 4. Exemplo: Hello, world (que novidade!) class Hello{ public static void main(String args[ ]) { System.out.println(&quot;Início da Monitoração&quot;); System.out.println(&quot;Hello World!&quot;); System.out.println(&quot;Final da monitoração&quot;); } } Tangled code
  5. 5. Exemplo: Hello, world (novidade!) aspect Monitor{ pointcut impressao(): execution ( public static void Hello.main(String [])); before (): impressao () { System.out.println(&quot;Inicio da impressao&quot;); } after (): impressao () { System.out.println(&quot;Final da impressao&quot;); } } class Hello{ public static void main(String args[ ]){ System.out.println(&quot;Hello World!&quot;); } } Hello.java Monitor.aj
  6. 6. Weaving Hello.java Monitor.aj ajc Hello.class Monitor.class
  7. 7. Exemplo: Logger OO Exemplo de Tirelo et al. – JAI2004
  8. 8. 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()); }
  9. 9. Exemplo: Disque-saúde com RMI Exemplo de Sérgio Soares e Paulo Borba - UFPE
  10. 10. Implementação com RMI 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 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(&quot;Creating RMI server...&quot;); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind(&quot;/HealthWatcher&quot;); System.out.println(&quot;Server created and ready.&quot;); } 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(&quot;//HealthWatcher&quot;); } 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; . . . }
  11. 11. 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(&quot;Creating RMI server...&quot;); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind(&quot;/HealthWatcher&quot;); System.out.println(&quot;Server created and ready.&quot;); } 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(&quot;//HealthWatcher&quot;); } 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
  12. 12. Problemas na implementação OO <ul><li>Tangled code (código entrelaçado) </li></ul><ul><ul><li>lógicade distribuição misturado com lógicade negócio </li></ul></ul><ul><li>Spread code (código espalhado) </li></ul><ul><ul><li>código de distribuição em várias classes </li></ul></ul><ul><ul><li>distribuição é um crosscutting concern </li></ul></ul><ul><li>Difícil de manter e reusar </li></ul><ul><ul><li>mudanças no protocolo de distribuição (RMI  CORBA  EJB ) são invasivas </li></ul></ul>
  13. 13. Disque-saúde com AOP
  14. 14. Implementação em AspectJ 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 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(&quot;Creating RMI server...&quot;); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind(&quot;/HealthWatcher&quot;); System.out.println(&quot;Server created and ready.&quot;); } 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(&quot;//HealthWatcher&quot;); } 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) {...} } }
  15. 15. 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(&quot;Creating RMI server...&quot;); UnicastRemoteObject.exportObject(facade); java.rmi.Naming.rebind(&quot;/HealthWatcher&quot;); System.out.println(&quot;Server created and ready.&quot;); } 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(&quot;//HealthWatcher&quot;); } 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

×