Your SlideShare is downloading. ×
0
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Paradigmas de linguagens de programacao - aula#9
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Paradigmas de linguagens de programacao - aula#9

1,225

Published on

Paradigmas de Linguagens de Programação - Aula #9 …

Paradigmas de Linguagens de Programação - Aula #9
Prof. Ismar Frango

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Paradigmas de Linguagens de Programação Paradigma Orientado a Aspectos Aula #9 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
  • 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. 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. 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. 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. Weaving Hello.java Monitor.aj ajc Hello.class Monitor.class
  • 7. Exemplo: Logger OO Exemplo de Tirelo et al. – JAI2004
  • 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. Exemplo: Disque-saúde com RMI Exemplo de Sérgio Soares e Paulo Borba - UFPE
  • 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. 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. 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. Disque-saúde com AOP
  • 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. 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

×