[Ajuda] - Fluent Interface

11 respostas
acidotherwise
package Principal;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;

public class TestaGráfico {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		Map<String,Double> mapa  = new HashMap<String, Double>();
		mapa.put("Ásia", 4.57);
		mapa.put("África", 1.18);
		mapa.put("Europa", 0.705);
		mapa.put("America", 1.03);
		mapa.put("Oceania", 0.038);
		
		ChartBuilder g =  new ChartBuilder(mapa)
							.criaGrafico("População Mundial en 2020")
							.salvar(new FileOutputStream("grafico.png"));
		
		
		
	}

}
Bom galera no codigo acima o eclipse ta dando erro onde eu estou usando fluent interface, alguem sabe me dizer oque estou fazendo de errado?

11 Respostas

T

Entschuldigung - há algum import para a classe ChartBuilder? Ela está no mesmo package?

acidotherwise

Ela esta no mesmo package não precisa de import.

T

Como é que é a definição dos métodos “criaGrafico” e “salvar”? Eles têm de ter o tipo de retorno “ChartBuilder” e devem retornar “this”.

acidotherwise
@SuppressWarnings("deprecation")
public void criaGrafico(String titulo){
	
	this.grafico = ChartFactory.createPieChart3D(titulo, this.dataSet, true, false, true);
	this.grafico.getPlot().setForegroundAlpha(0.5f);
	this.grafico.setBackgroundPaint(Color.cyan);
	PiePlot plotagem = (PiePlot) this.grafico.getPlot();  
	plotagem.setSectionPaint(0, Color.black);
	
	
	
  
}
public void salvar(OutputStream out) throws IOException{
	ChartUtilities.writeChartAsPNG(out, this.grafico, 500, 400);
}
public void processChart(Object obj, Map map) {  
	   
	         JFreeChart chart = (JFreeChart)obj;  
	   
	         //Altero as cores das series de meus itens  
	   
	         CategoryPlot categoryPlot = chart.getCategoryPlot();  
	   
	         CategoryItemRenderer renderer = categoryPlot.getRenderer();  
	   
	         renderer.setSeriesPaint( 0, Color.BLUE );  
	   
	         renderer.setSeriesPaint( 1, Color.RED );  
	   
	     } 
}

estes são os metodos que comentou

T

Pois é, fluent interface não pode usar métodos "void". Eles sempre devem retornar o próprio objeto. OK?

public ChartBuilder criaGrafico(String titulo){  
       
     this.grafico = ChartFactory.createPieChart3D(titulo, this.dataSet, true, false, true);  
     this.grafico.getPlot().setForegroundAlpha(0.5f);  
     this.grafico.setBackgroundPaint(Color.cyan);  
     PiePlot plotagem = (PiePlot) this.grafico.getPlot();    
     plotagem.setSectionPaint(0, Color.black);  
       
       
       
     return this; // <----
}
acidotherwise

agora sim matou todas certinhu, deu bem certinhu como tu falou! valeow!

Filipe_A

Ola thingol,

Gostaria de mais dicas sobre fluent Interfaces, se possivel…

Percebi no topico que os metodos devem retornar o mesmo objeto, existem outras regras?

Valeu!

T


Filipe_A

OK , obrigado.

Estou com uma dúvida , existe diferença entre:

ChartBuilder g =  new ChartBuilder(mapa).criaGrafico("População Mundial en 2020").salvar(new FileOutputStream("grafico.png"));

e partindo do new direto como este:

new ChartBuilder(mapa).criaGrafico("População Mundial en 2020").salvar(new FileOutputStream("grafico.png"));

Valeu!

B

A diferença é que você não grava o resultado numa variável g do tipo ChartBuilder. Talvez nem precise.

Filipe_A

Ah sim entendi.

obrigado. :smiley:

Criado 9 de abril de 2009
Ultima resposta 28 de abr. de 2009
Respostas 11
Participantes 4