1. 程式人生 > >從頭認識java-13.5 利用泛型構建復雜模型

從頭認識java-13.5 利用泛型構建復雜模型

引用 ucid http lee private 這一 數據 pack ora

這一章節我們來展示一下如何利用泛型構建復雜模型?

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裏面創建生成器。


總結:這一章節主要是展示一下如何利用泛型構建復雜模型。


這一章節就到這裏,謝謝。

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

文件夾




從頭認識java-13.5 利用泛型構建復雜模型