Реализация репозитория
import java.util.HashMap; import java.util.Map; /** * Для инстационирования и временного сохранения репозитория * @author oleksandr.zakharov
*/ public final class RepositoryFactory { private Repository repository; private static Map<String,Repository> repositoryMap = new HashMap<String,Repository>(); private RepositoryFactory(){ repository = new Repository(); } public static RepositoryFactory createRepository(){ RepositoryFactory repositoryFactory = new RepositoryFactory(); return repositoryFactory; } public RepositoryFactory addName(String name){ repository.setName(name); return this; } public RepositoryFactory addSpecification(Specification s){ repository.getSpecifications().add(s); return this; } public static Repository findRepository(String name){ return repositoryMap.get(name); } public Repository buildAll(){ repositoryMap.put(repository.getName(), repository); return repository; } }
Определяем спецификацию репозитория
package repository; /** * * @author oleksandr.zakharov */ public interface Specification { boolean validate(IEntity entity); }
package repository; import java.util.ArrayList; import java.util.List; /** * * @author oleksandr.zakharov * @param <T> */ public class Repository<T extends IEntity> { public Repository() { ts = new ArrayList<>(); specifications = new ArrayList<>(); } private boolean validate(IEntity entity){ for(Specification s: specifications){ if(!s.validate(entity)) return false; } return true; } private String name; private List<Specification> specifications; private List<T> ts; List<Specification> getSpecifications() { return specifications; } String getName() { return name; } void setName(String name) { this.name = name; } public boolean save(T entity){ if(validate(entity)){ ts.add(entity); return true; } return false; } }
интерфейс сущности
package repository; /** * * @author oleksandr.zakharov */ public interface IEntity { Long getId(); }
Небольшая реализация(счет продукта не должен быть больше 40)
package repository.impl; import repository.IEntity; import repository.Specification; /** * * @author oleksandr.zakharov */ public class AmountSpec implements Specification{ @Override public boolean validate(IEntity entity) { Product p = (Product) entity; return p.getAmount() > 40; } }
package repository.impl; import repository.IEntity; /** * * @author oleksandr.zakharov */ public class Product implements IEntity{ private Long id; private double amount; @Override public Long getId() { return id; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } }
package repository.impl; import repository.Repository; import repository.RepositoryFactory; /** * * @author oleksandr.zakharov Simple example */ public class TestMain { public static void main(String[] str) { RepositoryFactory .createRepository() .addName("Repository") .addSpecification(new AmountSpec()) // i added for specific repository .buildAll(); Repository r = RepositoryFactory.findRepository("Repository"); Product product = new Product(); product.setAmount(40); System.out.print(r.save(product)); // если меньше сорока не сохраниться } }