Difference between revisions of "Adapter Pattern (padrão de desenho)/Exercício 01: Cat Stream"

From Wiki**3

< Adapter Pattern (padrão de desenho)
(Created page with "{{TOCright}} = Problema = Um canal de saída binário ('''DataOutputStream''') permite escrever valores correspondentes a atributos de instâncias da classe '''Cat''' (gatos)...")
 
Line 6: Line 6:
 
A classe '''Cat''' é como se indica de seguida:
 
A classe '''Cat''' é como se indica de seguida:
  
<java5>
+
<source lang="java">
public class Cat {  
+
public class Cat {
     private int _age = 0;  
+
     private int _age = 0;
     private float _weight = 0;  
+
     private float _weight = 0;
     private String _name = "";  
+
     private String _name = "";
  
     public Cat(int age, float weight, String name) { _age = age; _weight = weight; _name = name; }  
+
     public Cat(int age, float weight, String name) { _age = age; _weight = weight; _name = name; }
  
     public int getAge() { return _age; }  
+
     public int getAge() { return _age; }
     public void setAge(int age) { _age = age; }  
+
     public void setAge(int age) { _age = age; }
  
     public float getWeight() { return _weight; }  
+
     public float getWeight() { return _weight; }
     public void setWeight(float weight) { _weight = weight; }  
+
     public void setWeight(float weight) { _weight = weight; }
  
     public String getName() { return _name; }  
+
     public String getName() { return _name; }
     public void setName(String name) { _name = name; }  
+
     public void setName(String name) { _name = name; }
  
     public String toString() { return "Cat [_age=" + _age + ", _weight=" + _weight + ", _name=" + _name + "]"; }  
+
     public String toString() { return "Cat [_age=" + _age + ", _weight=" + _weight + ", _name=" + _name + "]"; }
 
}
 
}
</java5>
+
</source>
  
 
= Solução =
 
= Solução =

Revision as of 20:51, 8 November 2018

Problema

Um canal de saída binário (DataOutputStream) permite escrever valores correspondentes a atributos de instâncias da classe Cat (gatos): respectivamente e em grupos de 3, a idade (writeInt), o peso (writeFloat) e o nome (writeUTF). Interessa, contudo, que as aplicações que trabalham com gatos não tenham contacto directo com o canal binário, sendo apenas colocadas em contacto com o "canal de gatos" (CatOutputChannel). O canal de gatos deve aceitar como parâmetro do construtor o canal binário e deve providenciar um método put (com um argumento Cat). As excepções de entradas e saídas produzidas pelo canal binário devem ser propagadas. O canal deve ainda fornecer uma função close que fecha todos os canais (o de gatos e o binário). Implemente o canal de gatos e uma aplicação que demonstre a utilização.

A classe Cat é como se indica de seguida:

public class Cat {
    private int _age = 0;
    private float _weight = 0;
    private String _name = "";

    public Cat(int age, float weight, String name) { _age = age; _weight = weight; _name = name; }

    public int getAge() { return _age; }
    public void setAge(int age) { _age = age; }

    public float getWeight() { return _weight; }
    public void setWeight(float weight) { _weight = weight; }

    public String getName() { return _name; }
    public void setName(String name) { _name = name; }

    public String toString() { return "Cat [_age=" + _age + ", _weight=" + _weight + ", _name=" + _name + "]"; }
}

Solução

Diagrama UML

Classes

Compiling and Running