Understanding java-13.5 from the ground up using generics to build complex models
This section describes how to use generics to build complex models?
1. List of tuples
We have already said that tuples are a complex model that can return multiple objects.
Package com. ray. ch11; import java. util. ArrayList; public class Test {public ArrayList
The above Code uses tuples to implement a complicated model.
Next we will introduce another example, a store.
2. Store
The store is composed of office area, front desk, and sales area, and the sales area is composed of several shelves. There are multiple types of goods to be placed on the shelves.
package com.ray.ch11;import java.util.ArrayList;import java.util.Collection;import java.util.Random;public class Store extends ArrayList
{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
generator = new Generator
() {@Overridepublic Product next() {Random random = new Random();int id = random.nextInt();return new Product(id, test- + id, random.nextDouble());}};@Overridepublic String toString() {return produce id: + id + name: + name + price: + price;}}interface Generator
{public T next();}class Generators {public static
Collection
fill(Collection
collection,Generator
generator, int num) {for (int i = 0; i < num; i++) {collection.add(generator.next());}return collection;}}class Shelf extends ArrayList
{/** * */private static final long serialVersionUID = 1L;public Shelf(int produceNum) {Generators.fill(this, Product.generator, produceNum);}}class SaleZone extends ArrayList
{/** * */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 {}
You may understand that the above Code is more complex. I will explain it as follows:
1. The first difficulty lies in the generator. If you read the previous chapter, it may be simpler. In fact, the generator is used here to abstract a more common generator. For general code, we can directly return a produceList in the product. Such code may look much better.
2. Generators: It mainly abstracts universal code for filling data into containers.
3. there are several classes that inherit the ArrayList directly. This is to directly call the add method when constructing the constructor. You do not need to construct an ArrayList, maybe we will create an ArrayList ourselves and fill in the data in it.
4. Use an anonymous internal class to create a generator in the product.
Summary: This section describes how to use generics to build complex models.