Desenvolva um programa que simule o funcionamento de uma porta automática, utilizando o padrão State para representar o status da porta automática.
A porta automática pode assumir os seguintes estados:
• closed: porta fechada
• closing: porta fechando
• open: porta aberta
• opening: porta abrindo
• stayOpen: manter aberta
As ações que a porta automática pode executar são:
• click: botão para abrir/fechar a porta automática
• complete: fim do processo de abertura/fechamento da porta automática
• timeout: passagem de tempo num dado estado
A figura a seguir apresenta as transições entre os estados, com as suas respectivas ações.
/**
* Copyright (C) 2009/2025 - 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;
/**
* State
*
* Define uma interface para encapsulamento associado com um determinado estado do Context
*/
public abstract class DoorState
{
/**
* Referencia ao context
*/
protected Door door;
/**
* Construtor padrao
*
* @param door referencia ao context
*/
public DoorState(Door door)
{
this.door = door;
}
/**
* Click action
*/
public abstract void click();
/**
* Complete action
*/
public void complete()
{
}
/**
* Retornar o status da porta
*
* @return status da porta
*/
public abstract String status();
/**
* Timeout action
*/
public void timeout()
{
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* ConcreteState
*
* Implementa um comportamento associado com um estado do Context
*/
public class DoorClosed extends DoorState
{
/**
* Construtor padrao
*
* @param door referecia ao context
*/
public DoorClosed(Door door)
{
super(door);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#click()
*/
public void click()
{
this.door.setState(this.door.OPENING);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#status()
*/
public String status()
{
return "CLOSED";
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* ConcreteState
*
* Implementa um comportamento associado com um estado do Context
*/
public class DoorClosing extends DoorState
{
/**
* Construtor padrao
*
* @param door referencia ao context
*/
public DoorClosing(Door door)
{
super(door);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#click()
*/
public void click()
{
this.door.setState(this.door.OPENING);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#complete()
*/
public void complete()
{
this.door.setState(this.door.CLOSED);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#status()
*/
public String status()
{
return "CLOSING";
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* ConcreteState
*
* Implementa um comportamento associado com um estado do Context
*/
public class DoorOpen extends DoorState
{
/**
* Construtor padrao
*
* @param door referencia ao context
*/
public DoorOpen(Door door)
{
super(door);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#click()
*/
public void click()
{
this.door.setState(this.door.STAYOPEN);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#timeout()
*/
public void timeout()
{
this.door.setState(this.door.CLOSING);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#status()
*/
public String status()
{
return "OPEN";
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* ConcreteState
*
* Implementa um comportamento associado com um estado do Context
*/
public class DoorOpening extends DoorState
{
/**
* Construtor padrao
*
* @param door referencia ao context
*/
public DoorOpening(Door door)
{
super(door);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#click()
*/
public void click()
{
this.door.setState(this.door.CLOSING);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#complete()
*/
public void complete()
{
this.door.setState(this.door.OPEN);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#status()
*/
public String status()
{
return "OPENING";
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* ConcreteState
*
* Implementa um comportamento associado com um estado do Context
*/
public class DoorStayOpen extends DoorState
{
/**
* Construtor padrao
*
* @param door referencia ao context
*/
public DoorStayOpen(Door door)
{
super(door);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#click()
*/
public void click()
{
this.door.setState(this.door.CLOSING);
}
/* (non-Javadoc)
* @see com.ybadoo.DoorState#status()
*/
public String status()
{
return "STAYOPEN";
}
}
/**
* Copyright (C) 2009/2025 - 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
*
* Define a interface de interesse para os clientes
* Mantem uma instancia de uma subclasse ConcreteState que define o estado corrente
*/
public class Door
{
/**
* ConcreteState
*/
public final DoorState CLOSED = new DoorClosed(this);
/**
* ConcreteState
*/
public final DoorState CLOSING = new DoorClosing(this);
/**
* ConcreteState
*/
public final DoorState OPEN = new DoorOpen(this);
/**
* ConcreteState
*/
public final DoorState OPENING = new DoorOpening(this);
/**
* ConcreteState
*/
public final DoorState STAYOPEN = new DoorStayOpen(this);
/**
* Estado corrente
*/
private DoorState state = CLOSED;
/**
* Click action
*/
public void click()
{
this.state.click();
}
/**
* Complete action
*/
public void complete()
{
this.state.complete();
}
/**
* Alterar o status da porta
*
* @param state status da porta
*/
protected void setState(DoorState state)
{
this.state = state;
}
/**
* Retornar o status da porta
*
* @return status da porta
*/
public String status()
{
return this.state.status();
}
/**
* Timeout action
*/
public void timeout()
{
this.state.timeout();
}
}
/**
* Copyright (C) 2009/2025 - 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;
/**
* 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)
{
Door door = new Door();
System.out.println(door.status());
door.click();
System.out.println(door.status());
door.complete();
System.out.println(door.status());
door.timeout();
System.out.println(door.status());
door.click();
System.out.println(door.status());
}
}