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

Desenvolva um programa que exiba uma mensagem diferente para cada dia da semana, usando o padrão Strategy.

 

Arquivo Weekdays.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Strategy
 */
public interface Weekdays
{
  /**
   * Retornar uma mensagem
   * 
   * @return mensagem
   */
  public String message();
}

Arquivo Sunday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Sunday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Sunday!";
  }
}

Arquivo Monday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Monday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Monday!";
  }
}

Arquivo Tuesday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Tuesday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Tuesday!";
  }
}

Arquivo Wednesday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Wednesday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Wednesday!";
  }
}

Arquivo Thursday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Thursday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Thursday!";
  }
}

Arquivo Friday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Friday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Friday!";
  }
}

Arquivo Saturday.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Concrete Strategy
 */
public class Saturday implements Weekdays
{
  /* (non-Javadoc)
   * @see com.ybadoo.Week#message()
   */
  public String message()
  {
    return "Today is Saturday!";
  }
}

Arquivo Week.java

/**
 * Copyright (C) 2009/2024 - 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;

/**
 * Context
 */
public class Week
{
  /**
   * Retornar o dia da semana
   * 
   * @param weekdays dia da semana
   * @return instancia do dia da semana
   */
  public Weekdays getWeekdays(int weekdays)
  {
    // Verificar o dia da semana desejado
    switch(weekdays)
    {
      case 1: return new Sunday();
      case 2: return new Monday();
      case 3: return new Tuesday();
      case 4: return new Wednesday();
      case 5: return new Thursday();
      case 6: return new Friday();
      case 7: return new Saturday();
    }

    // Dia da semana invalido
    throw new IllegalArgumentException("'" + weekdays + 
                                       "' is an illegal argument for the parameter weekdays!");
  }

  /**
   * Retornar o dia da semana, via reflexao
   * 
   * @param weekdays dia da semana
   * @return instancia do dia da semana
   */
  public Weekdays getWeekdays(String weekdays)
  {
    try
    {
      // Obter a instancia da classe desejada
      return (Weekdays) Class.forName(weekdays).newInstance();
    }
    catch(Exception exception)
    {
      // Dia da semana invalido
      throw new IllegalArgumentException("'" + weekdays + 
                                         "' is an illegal argument for the parameter weekdays!");
    }
  }
}

Arquivo Application.java

/**
 * Copyright (C) 2009/2024 - 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.Calendar;
import java.util.Locale;

/**
 * Classe de teste da aplicacao
 */
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)
  {
    Week week = new Week();

    Calendar calendar = Calendar.getInstance();

    System.out.println(week.getWeekdays(calendar.get(Calendar.DAY_OF_WEEK)).message());

    System.out.println(week.getWeekdays(calendar.getDisplayName(Calendar.DAY_OF_WEEK, 
                                        Calendar.LONG, Locale.ENGLISH)).message());
  }
}