• 从头认识java-13.5 利用泛型构建复杂模型


    这一章节我们来展示一下如何利用泛型构建复杂模型?

    1.元组列表

    我们之前已经说过元组是一个复杂的模型,能够返回多对象。

    package com.ray.ch11;
    
    import java.util.ArrayList;
    
    public class Test {
    	public ArrayList<Tuple<A, B, C>> test() {
    		ArrayList<Tuple<A, B, C>> list = new ArrayList<Tuple<A, B, C>>();
    		for (int i = 0; i < 10; i++) {
    			list.add(new Tuple<A, B, C>(new A(), new B(), new C()));
    		}
    		return list;
    	}
    
    	public static void main(String[] args) {
    		new Test().test();
    	}
    }
    
    class A {
    }
    
    class B {
    }
    
    class C {
    }
    
    @SuppressWarnings("hiding")
    class Tuple<A, B, C> {
    	public final A a;
    	public final B b;
    	public final C c;
    
    	public Tuple(A a, B b, C c) {
    		this.a = a;
    		this.b = b;
    		this.c = c;
    	}
    }


    上面的代码我们通过元组来实现一个比較复杂的模型。

    我们以下再引用另外一个样例。一个商店。


    2.商店

    这个商店由办公区、前台、销售区组成,并且销售区由若干货架组成,货架上面又须要放置多种货物。


    package com.ray.ch11;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Random;
    
    public class Store extends ArrayList<SaleZone> {
    	private Office office = new Office();
    	private CheckOut checkOut = new CheckOut();
    
    	public Store(int saleZoneNum, int shelfNum, int produceNum) {
    		for (int i = 0; i < saleZoneNum; i++) {
    			add(new SaleZone(shelfNum, produceNum));
    		}
    	}
    
    	public static void main(String[] args) {
    		new Store(1, 2, 5);
    	}
    }
    
    class Product {
    	private int id = 0;
    	private String name = "";
    	private double price = 0.0;
    
    	public Product(int id, String name, double price) {
    		this.id = id;
    		this.name = name;
    		this.price = price;
    		System.out.println(toString());
    	}
    
    	public static Generator<Product> generator = new Generator<Product>() {
    		@Override
    		public Product next() {
    			Random random = new Random();
    			int id = random.nextInt();
    			return new Product(id, "test-" + id, random.nextDouble());
    		}
    	};
    
    	@Override
    	public String toString() {
    		return "produce id: " + id + " name: " + name + " price: " + price;
    	}
    }
    
    interface Generator<T> {
    	public T next();
    }
    
    class Generators {
    	public static <T> Collection<T> fill(Collection<T> collection,
    			Generator<T> generator, int num) {
    		for (int i = 0; i < num; i++) {
    			collection.add(generator.next());
    		}
    		return collection;
    	}
    }
    
    class Shelf extends ArrayList<Product> {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public Shelf(int produceNum) {
    		Generators.fill(this, Product.generator, produceNum);
    	}
    }
    
    class SaleZone extends ArrayList<Shelf> {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public SaleZone(int shelfNum, int produceNum) {
    		for (int i = 0; i < shelfNum; i++) {
    			add(new Shelf(produceNum));
    		}
    	}
    }
    
    class Office {
    }
    
    class CheckOut {
    }
    

    大家可能理解上面的代码会比較复杂一点,我解释一下:

    1.第一个难度在于生成器,假设读了前面章节或许会简单一点。

    事实上这里使用生成器,主要是为了抽象出一个比較通用的生成器。假设是一般的代码,我们能够在product里面直接返回一个produceList,这种代码看上去或许会好非常多。

    2.Generators,主要是抽象出往容器填充数据的通用性代码。

    3.里面有几个类都直接继承了ArrayList,这里是为了在构造器的时候就能够直接调用add方法,不用在构造一次ArrayList。假设依照寻常的习惯,或许我们会自己建立一个ArrayList,然后往里面填充数据就算了

    4.使用匿名内部类在product里面创建生成器。


    总结:这一章节主要是展示一下如何利用泛型构建复杂模型。


    这一章节就到这里,谢谢。

    -----------------------------------

    文件夹




  • 相关阅读:
    xcodebuild changed some of the values
    ar和nm命令的使用(转载)
    POJ 3678 Katu Puzzle (2SAT)
    ZOJ 3664 Split the Rectangle 第37届ACM/ICPC长春赛区现场赛 J 题(模拟建树,暴力 求LCA)
    HDU 4115 Eliminate the Conflict (2SAT)
    ZOJ 3665 Yukari's Birthday 第37届ACM/ICPC长春赛区现场赛K题 (水题,枚举,二分)
    ZOJ 3662 Math Magic 第37届ACM/ICPC长春赛区H题(DP)
    【原创】2012ACM长春赛区现场赛总结
    ZOJ 3656 Bit Magic 第37届ACM/ICPC长春赛区现场赛B题 (2SAT)
    ZOJ 3657 The Little Girl who Picks Mushrooms 第37届ACM/ICPC长春赛区现场赛C题(水题)
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7088868.html
Copyright © 2020-2023  润新知