利用集合和泛型“创建商店”

interface Generator<T> {
    T next();
}

class Generators{
    public static <T> Collection<T> fill(Collection<T> coll, Generator<T> gen, int n){
        for(int i = 0; i < n; i++){
            coll.add(gen.next());
        }
        return coll;
    }
}

class Product{
    private final int id;
    private String description;
    private double price;
    public Product(int IDnumber, String descr, double price){
        id = IDnumber;
        description = descr;
        this.price = price;
        System.out.println(toString());
    }

    public String toString(){
        return id + " : " + description + ",price: $" + price;
    }

    public void priceChange(double change){
        price += change;
    }

    public static Generator<Product> generator =
            new Generator<Product>() {
                private Random rand = new Random(47);
                @Override
                public Product next() {
                    return new Product(rand.nextInt(1000), "Test",
                            Math.round(rand.nextDouble()*1000.0) + 0.99);
                }
            };
}

class Shelf extends ArrayList<Product>{
    public Shelf(int nProducts){
        Generators.fill(this, Product.generator, nProducts);
    }
}

class Aisle extends ArrayList<Shelf>{
    public Aisle(int nShelves, int nProducts){
        for(int i = 0; i < nShelves; i++){
            add(new Shelf(nProducts));
        }
    }
}

class CheckoutStand{}
class Office{}

class Store extends ArrayList<Aisle>{
    private ArrayList<CheckoutStand> checkouts = new ArrayList<CheckoutStand>();
    private Office office = new Office();
    public Store(int nAisles, int nShelves, int nProducts){
        for(int i = 0; i < nAisles; i++){
            add(new Aisle(nShelves, nProducts));
        }
    }

    public String toString(){
        StringBuilder result = new StringBuilder();
        for(Aisle a : this)
            for(Shelf s :  a)
                for(Product p: s){
                    result.append(p);
                    result.append("\n");
                }

        return result.toString();
    }
}



public class Main{

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        System.out.println(new Store(2, 2, 2));
    }
}
运行结果:
258 : Test,price: $400.99
861 : Test,price: $160.99
868 : Test,price: $417.99
207 : Test,price: $268.99
551 : Test,price: $114.99
278 : Test,price: $804.99
520 : Test,price: $554.99
140 : Test,price: $530.99
258 : Test,price: $400.99
861 : Test,price: $160.99
868 : Test,price: $417.99
207 : Test,price: $268.99
551 : Test,price: $114.99
278 : Test,price: $804.99
520 : Test,price: $554.99

140 : Test,price: $530.99

    例子中为商店模型,Product为商品,Shelf为货架,Aisle为走廊,Store为商店

为什么我们要使用通用DAO接口呢,因为我们的数据库操作无非是增删改查,CRUD操作,我们不需要为每个实体去编写一个dao接口,对于相似的实体操作可以只编写一个通用接口,然后采用不同的实现! DAO已经成为持久层的标准模式,DAO使结构清晰,面向接口编程为代码提供了规范。而泛型DAO是一个类型安全的,代码精简的设计模式(相对于传统DAO),尤其在DAO组件数量庞大的时候,代码量的减少更加明显。 泛型DAO的核心是定义一个GenericDao接口,声明基本的CRUD操作: 用hibernate作为持久化解决方案的GenericHibernateDao实现类,被定义为抽象类,它提取了CRUD操作,这就是简化代码的关键,以便于更好的重用,这个就不给例子了,增删改都好写,查就需要各种条件了。 然后是各个领域对象的dao接口,这些dao接口都继承GenericDao接口,这样各个领域对象的dao接口就和传统dao接口具有一样的功能了。 下一步是实现类了,个自领域对象去实现各自的接口,还要集成上面的抽象类,这样就实现了代码复用的最大化,实现类中只需要写出额外的查询操作就可以了。当然还要获得域对象的Class实例,这就要在构造方法中传入Class实例。用spring提供的HibernateTemplate注入到GenericHibernateDao中,这样在各个实现类就可以直接调用HibernateTemplate来实现额外的查询操作了。 如果在实现类中不想调用某个方法(例如:update()),就可以覆盖它,方法中抛出UnsupportedOperationException()异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值