Skip to content

Instantly share code, notes, and snippets.

@cmendesce
Created November 20, 2012 02:38
Show Gist options
  • Select an option

  • Save cmendesce/4115573 to your computer and use it in GitHub Desktop.

Select an option

Save cmendesce/4115573 to your computer and use it in GitHub Desktop.
Implementação básica do padrão repository (http://martinfowler.com/eaaCatalog/repository.html) no Android.
package br.edu.fa7.exemplo.domain.entities;
import java.io.Serializable;
public class Entity implements Serializable {
private long id;
public Entity() {
this.id = 0;
}
Entity(long id) {
this.id = id;
}
public void setId(int id) {
this.id = id;
}
public long getId() {
return id;
}
public boolean isPersisted() {
return this.id > 0;
}
}
package br.edu.fa7.exemplo.domain.entities;
import java.io.Serializable;
public class Exemplo extends Entity implements Serializable {
private String nome;
private String telefone;
public Exemplo(long id, String nome, String telefone) {
this.nome = nome;
this.telefone = telefone;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
package br.edu.fa7.exemplo.domain.repositories;
import br.edu.fa7.exemplo.domain.entities.Fugitivo;
public interface ExemploRepository extends Repository<Exemplo> {
}
package br.edu.fa7.exemplo.infrastructure.repositories;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import br.edu.fa7.exemplo.domain.entities.Fugitivo;
import br.edu.fa7.exemplo.domain.repositories.FugitivoRepository;
public class ExemploRepositoryImpl extends SQLiteRepository<Exemplo> implements ExemploRepository {
private final static String NOME = "nome";
private final static String TELEFONE = "telefone";
private String[] columns;
public ExemploRepositoryImpl(Context context) {
super(context);
}
@Override
protected String[] getColumns() {
if (columns == null) {
columns = new String[] { ID, NOME, TELEFONE };
}
return columns;
}
@Override
protected Exemplo fill(Cursor c) {
long id = c.getLong(c.getColumnIndex(ID));
String telefone = c.getString(c.getColumnIndex(TELEFONE));
String nome = c.getString(c.getColumnIndex(NOME));
return new Fugitivo(id, nome, telefone);
}
@Override
protected ContentValues getValues(Exemplo entity) {
ContentValues values = new ContentValues();
values.put(TELEFONE, entity.getTelefone());
values.put(NOME, entity.getNome());
return values;
}
}
package br.edu.fa7.exemplo.domain.repositories;
import java.util.List;
import br.edu.fa7.exemplo.domain.entities.Entity;
public interface Repository<E extends Entity> {
void add(E entity);
int remove(E entity);
int remove(long id);
void update(E entity);
List<E> list();
E get(long id);
}
package br.edu.fa7.exemplo.infrastructure.repositories;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import br.edu.fa7.exemplo.domain.entities.Entity;
import br.edu.fa7.exemplo.domain.repositories.Repository;
public abstract class SQLiteRepository<E extends Entity> implements Repository<E> {
protected final static String ID = "_id";
private SQLiteDatabase database;
private DatabaseExemploHelper helper;
private Context context;
private String tableName;
public SQLiteRepository(Context context) {
this.tableName = getEntityName();
this.context = context;
buildDatabase();
}
private String getEntityName() {
ParameterizedType p = (ParameterizedType)getClass().getGenericSuperclass();
String txt = ((Class<E>)p.getActualTypeArguments()[0]).getSimpleName();
return txt.toLowerCase();
}
private void buildDatabase() {
if (helper == null) {
helper = new DatabaseExemploHelper(context);
database = helper.getWritableDatabase();
}
}
protected SQLiteDatabase getDatabase() {
if (database == null) {
buildDatabase();
}
return database;
}
@Override
public void add(E entity) {
ContentValues values = getValues(entity);
long id = database.insert(tableName, null, values);
entity.setId(id);
}
@Override
public void update(E entity) {
ContentValues values = getValues(entity);
int rows = database.update(tableName, values, "_id=?", new String[] { String.valueOf(entity.getId()) });
}
@Override
public int remove(E entity) {
return remove(entity.getId());
}
@Override
public int remove(long id) {
int rows = database.delete(tableName, "_id=?", new String[] { String.valueOf(id) });
return rows;
}
@Override
public List<E> list() {
List<E> list = new ArrayList<E>();
Cursor c = getDatabase().query(tableName, getColumns(), null, null, null, null, null);
c.moveToFirst();
while (c.moveToNext()) {
E entity = fill(c);
list.add(entity);
}
c.close();
return list;
}
@Override
public E get(long id) {
Cursor c = getDatabase().query(tableName, getColumns(), "_id=?", new String[] { String.valueOf(id) }, null, null, null);
return fill(c);
}
protected abstract E fill(Cursor c);
protected abstract ContentValues getValues(E entity);
protected abstract String[] getColumns();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment