Desenvolva um programa que simule o funcionamento de um semáforo, utilizando o padrão State para representar o status do semáforo. O semáforo deverá permanecer t segundos no estado amarelo e 5 * t segundos nos demais estados. Utilize uma Thread
para alterar o estado do semáforo de forma automática.
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
import java.io.Serializable;
/**
* Estado do semáforo
*
* State - define uma interface para encapsulamento associado com um
* determinado estado do Context (Gamma, 2000).
*/
public abstract class State implements Serializable
{
/**
* Identificador de serialização da classe
*/
private static final long serialVersionUID = 1L;
/**
* Tempo padrão, em segundos, para alteração do estado do semáforo
*/
private static final int TIME = 2;
/**
* Tempo restante para alteração do estado do semáforo
*/
private int remain;
/**
* Inicializar o estado do semáforo
*
* @param multiplier multiplicador do tempo padrão, em segundos, para
* alteração do estado do semáforo
*/
public State(int multiplier)
{
remain = (TIME * multiplier) + 1;
}
/**
* Alteração do estado do semáforo
*
* @param semaphore instância do semáforo
*/
public void change(Semaphore semaphore)
{
remain = remain - 1;
if(remain == 0)
{
semaphore.setState(nextState());
}
else
{
System.out.format("Remain: %02d%n", remain);
}
}
/**
* Retornar o próximo estado do semáforo
*
* @return próximo estado do semáforo
*/
protected abstract State nextState();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
/**
* Estado do semáforo - vermelho
*
* ConcreteState - cada subclasse implementa um comportamento associado com
* um estado do Context (Gamma, 2000).
*/
public class StateRed extends State
{
/**
* Identificador de serialização da classe
*/
private static final long serialVersionUID = 1L;
/**
* Inicializar o estado vermelho no semáforo
*/
public StateRed()
{
super(5);
System.out.println("RED");
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio33.State#nextState()
*/
@Override
protected State nextState()
{
return new StateGreen();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
/**
* Estado do semáforo - amarelo
*
* ConcreteState - cada subclasse implementa um comportamento associado com
* um estado do Context (Gamma, 2000).
*/
public class StateYellow extends State
{
/**
* Identificador de serializacao da classe
*/
private static final long serialVersionUID = 1L;
/**
* Inicializar o estado amarelo no semáforo
*/
public StateYellow()
{
super(1);
System.out.println("YELLOW");
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio33.State#nextState()
*/
@Override
public State nextState()
{
return new StateRed();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
/**
* Estado do semáforo - verde
*
* ConcreteState - cada subclasse implementa um comportamento associado com
* um estado do Context (Gamma, 2000).
*/
public class StateGreen extends State
{
/**
* Identificador de serialização da classe
*/
private static final long serialVersionUID = 1L;
/**
* Inicializar o estado verde no semáforo
*/
public StateGreen()
{
super(5);
System.out.println("GREEN");
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.tutorial10.exercicio33.State#nextState()
*/
@Override
protected State nextState()
{
return new StateYellow();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
import java.util.TimerTask;
/**
* Classe responsável pela representação de um semáforo
*
* Context - define a interface de interesse para os clientes;
* - mantém uma instância de uma subclasse ConcreteState que define
* o estado corrente (Gamma, 2000).
*/
public class Semaphore extends TimerTask
{
/**
* Estado corrente do semáforo
*/
private State state;
/**
* Inicializar o semáforo
*/
public Semaphore()
{
state = new StateRed();
}
/* (non-Javadoc)
* @see java.util.TimerTask#run()
*/
@Override
public void run()
{
state.change(this);
}
/**
* Alterar o estado corrente do semáforo
*
* @param state estado corrente do semáforo
*/
public void setState(State state)
{
this.state = state;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (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 *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
* *
* Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic) *
* OpenJDK Version "1.8.0_121" *
* OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode) *
*************************************************************************/
package com.ybadoo.tutoriais.poo.tutorial10.exercicio33;
import java.util.Timer;
/**
* Classe responsável pela execução do padrão State
*/
public class Application
{
/**
* Construtor para inicializar a execução do padrão State
*/
private Application()
{
}
/**
* Método principal da linguagem de programação Java
*
* @param args argumentos da linha de comando (não utilizado)
*/
public static void main(String[] args)
{
Timer timer = new Timer();
Semaphore semaphore = new Semaphore();
timer.schedule(semaphore, 0, 1000);
}
}