Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva um programa que notifique o usuário quando um e-mail chegar a sua caixa de entrada.

O usuário pode ser notificado por diversos meios, como por exemplo, pelo celular, por um aplicativo desktop, e assim por diante.

Utilize o padrão de projeto Observer para que todos os meios utilizados pelo usuário sejam notificados quando um e-mail chegar a sua caixa de entrada.

 

Arquivo Email.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Helper
 */
public class Email
{
  /**
   * Destinatario do e-mail
   */
  private String receiver;
  
  /**
   * Remetente do e-mail
   */
  private String sender;
  
  /**
   * Assunto do e-mail
   */
  private String subject;
  
  /**
   * Texto do e-mail
   */
  private String text;
  
  /**
   * Construtor para inicializar o e-mail
   * 
   * @param sender remetente do e-mail
   * @param receiver destinatario do e-mail
   * @param subject assunto do e-mail
   * @param text texto do e-mail
   */
  public Email(String sender, String receiver, String subject, String text)
  {
    this.sender = sender;
    this.receiver = receiver;
    this.subject = subject;
    this.text = text;
  }

  /**
   * Retornar o destinatario do e-mail
   * 
   * @return destinatario do e-mail
   */
  public String getReceiver()
  {
    return this.receiver;
  }

  /**
   * Retornar o remetente do e-mail
   * 
   * @return remetente do e-mail
   */
  public String getSender()
  {
    return this.sender;
  }

  /**
   * Retornar o assunto do e-mail
   * 
   * @return assunto do e-mail
   */
  public String getSubject()
  {
    return this.subject;
  }

  /**
   * Retornar o texto do e-mail
   * 
   * @return texto do e-mail
   */
  public String getText()
  {
    return this.text;
  }
}

Arquivo Subscriber.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Observer
 */
public interface Subscriber
{
  /**
   * Informar o recebimento de um novo e-mail
   * 
   * @param email e-mail recebido
   */
  public void update(Email email);
}

Arquivo Inbox.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

import java.util.ArrayList;
import java.util.List;

/**
 * Observable
 */
public class Inbox
{
  /**
   * Lista de dispositivos que observam a caixa de entrada
   */
  private List<Subscriber> subscribers;
  
  /**
   * Construtor padrao
   */
  public Inbox()
  {
    this.subscribers = new ArrayList<Subscriber>();
  }
  
  /**
   * Receber um novo e-mail
   * 
   * @param email e-mail
   */
  public void addEmail(Email email)
  {
    for(Subscriber subscriber: this.subscribers)
    {
      subscriber.update(email);
    }
  }
  
  /**
   * Acrescentar o dispositivo a lista
   * 
   * @param subscriber dispositivo
   */
  public void attach(Subscriber subscriber)
  {
    this.subscribers.add(subscriber);
  }
  
  /**
   * Retirar o dispositivo da lista
   * 
   * @param subscriber dispositivo
   */
  public void detach(Subscriber subscriber)
  {
    this.subscribers.remove(subscriber);
  }
}

Arquivo Desktop.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * ConcreteObservable
 */
public class Desktop implements Subscriber
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Subscriber#update(com.ybadoo.tutoriais.poo.Email)
   */
  public void update(Email email)
  {
    System.out.println("Desktop: " + email.getSubject() + " from " + email.getSender());
  }
}

Arquivo Cellphone.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * ConcreteObservable
 */
public class Cellphone implements Subscriber
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Subscriber#update(com.ybadoo.tutoriais.poo.Email)
   */
  public void update(Email email)
  {
    System.out.println("Cellphone: " + email.getSubject() + " from " + email.getSender());
  }
}

Arquivo Watch.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * ConcreteObservable
 */
public class Watch implements Subscriber
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Subscriber#update(com.ybadoo.tutoriais.poo.Email)
   */
  public void update(Email email)
  {
    System.out.println("Watch: " + email.getSubject() + " from " + email.getSender());
  }
}

Arquivo Application.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Client
 */
public class Application
{
  /**
   * Metodo principal da linguagem de programacao Java
   * 
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Inbox inbox = new Inbox();
    
    Desktop desktop = new Desktop();
    inbox.attach(desktop);
    
    Cellphone cellphone = new Cellphone();
    inbox.attach(cellphone);
    
    Watch watch = new Watch();
    inbox.attach(watch);
    
    Email email1 = new Email("fulano@xpto.com.br", "ciclano@xpto.com.br", "Encontro",
                             "Vamos nos reunir hoje?");
    
    // Desktop: Encontro from fulano@xpto.com.br
    // Cellphone: Encontro from fulano@xpto.com.br
    // Watch: Encontro from fulano@xpto.com.br
    inbox.addEmail(email1);
    
    inbox.detach(cellphone);
    
    Email email2 = new Email("ciclano@xpto.com.br", "fulano@xpto.com.br", "Re:Encontro",
                             "Combinado!");
    
    // Desktop: Re:Encontro from ciclano@xpto.com.br
    // Watch: Re:Encontro from ciclano@xpto.com.br
    inbox.addEmail(email2);
  }
}