0
Good afternoon I’m studying by object orientation, and in the middle of the studies appeared various patterns and with them a lot of confusion
My doubt is it’s possible to have a generic FACADE. or nothing to see that I’m talking about?
I was watching these videos lessons
Where he creates
- Bookmaker
- Interfacelivrodao
- Bookoid
- Livrocontroller
- Book (model)
In his example, it was a register of books, however, I’m making a register of products, where there will be PRODUCTS and BRANDS
I already created the iDao interface using Generic
package br.com.jcom.dao;
import java.util.List;
public interface IDao<T> {
String getNomeTabela();
int insert(T... elementos);
int update(T... elementos);
int delete(T... elementos);
//T selectCodigo(String codigo);
//T select(String sql, String... paramentros);
List<T> selectAll();
//List<T> selectAll(String sql, String... paramentros);
}
I have created the models PRODUCTS AND BRANDS
Also tagged
package br.com.jcom.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.jcom.factory.Conexao;
import br.com.jcom.modelo.Marca;
public class MarcaDao implements IDao<Marca> {
private String nomeTabela = "Marcas";
private Connection connection;
public MarcaDao() {
this.connection = new Conexao().getConnection();
}
@Override
public String getNomeTabela() {
return nomeTabela;
}
@Override
public int insert(Marca... elementos) {
String sql = "INSERT INTO " + nomeTabela + " (marca) VALUES (?)";
PreparedStatement stmt = null;
int result = 0;
for (Marca marca : elementos) {
try {
stmt = connection.prepareStatement(sql);
stmt.setString(1, marca.getMarca());
result = stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally{
Conexao.close(connection, stmt, null);
}
}
return result;
}
@Override
public int update(Marca... elementos) {
String sql = "UPDATE " + nomeTabela + "SET marca = ? WHERE MarcaID = ?";
PreparedStatement stmt = null;
int result = 0;
for (Marca marca : elementos) {
try {
stmt = connection.prepareStatement(sql);
stmt.setString(1, marca.getMarca());
stmt.setLong(2, marca.getMarcaID());
result = stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
Conexao.close(connection, stmt, null);
}
}
return result;
}
@Override
public int delete(Marca... elementos) {
String sql = "DELETE FROM " + nomeTabela + "WHERE MarcaID = ?";
PreparedStatement stmt = null;
int result = 0;
for (Marca marca : elementos) {
try {
stmt = connection.prepareStatement(sql);
stmt.setLong(1, marca.getMarcaID());
result = stmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
Conexao.close(connection, stmt, null);
}
}
return result;
}
@Override
public List<Marca> selectAll() {
String sql = "SELECT * FROM " + nomeTabela;
PreparedStatement stmt = null;
List<Marca> marcas = new ArrayList<Marca>();
ResultSet rs = null;
try {
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Marca marca = new Marca();
marca.setMarcaID(rs.getLong("marcaID"));
marca.setMarca(rs.getString("marca"));
marcas.add(marca);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
Conexao.close(connection, stmt, rs);
}
return marcas;
}
}
And the score
package br.com.jcom.controller;
import java.util.List;
import br.com.jcom.facade.DaoFacade;
import br.com.jcom.modelo.Marca;
public class MarcaController {
private DaoFacade marcaFacade;
public MarcaController() {
this.marcaFacade = new DaoFacade<Marca>();
}
public int addMarca(Marca marca){
return marcaFacade.insert(marca);
}
public int updateMarca(Marca marca){
return marcaFacade.update(marca);
}
public int deleteMarca(Marca marca){
return marcaFacade.delete(marca);
}
public List<Marca> selectMarcas(){
return marcaFacade.selectAll();
}
}
Good now in FACADE is that I am not getting very well the logic
package br.com.jcom.facade;
import java.util.List;
import br.com.jcom.dao.IDao;
import br.com.jcom.dao.MarcaDao;
public class DaoFacade<T> {
@SuppressWarnings("rawtypes")
private IDao dao;
public DaoFacade() {
this.dao = new MarcaDao();
}
@SuppressWarnings("unchecked")
public int insert(T... elementos) {
return dao.insert(elementos);
}
@SuppressWarnings("unchecked")
public int update(T... elementos) {
return dao.update(elementos);
}
@SuppressWarnings("unchecked")
public int delete(T... elementos) {
return dao.delete(elementos);
}
@SuppressWarnings("unchecked")
public List<T> selectAll() {
return dao.selectAll();
}
}
When I create the builder
public DaoFacade() {
this.dao = new MarcaDao();
}
Instead of putting (= new Marcadao();) there is the possibility of passing a generic class to when I = new Productodao, I am confusing everything??
Marcagui > btnSalvar
btnSalvar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Marca marca = new Marca();
if (Funcoes.validaCampos(panel) == true) {
marca.setMarca(tfMarca.getText());
} else {
JOptionPane.showMessageDialog(null,
"Preenchimento de campos (*) obrigatórios");
return;
}
int result = 0;
if (idMarca == null) {
result = new MarcaController().addMarca(marca);
} else {
marca.setMarcaID(idMarca);
result = new MarcaController().updateMarca(marca);
idMarca = null;
}
if (result == 1) {
JOptionPane.showMessageDialog(null, "Concluido");
} else {
JOptionPane.showMessageDialog(null, "Erro ao salvar");
}
refreshTable();
}
});
}
That class
DaoFacade
in the example code is not actually a "facade" as it is not simplifying access to a more complex API. I would recommend reading the full description of the "Pattern" Facade in the original book (Design Patterns).– Rogério