Understanding java-13.5 from scratch Building complex models using generics

Original Link: https://my.oschina.net/u/2325575/blog/543705

In this chapter, we will show you how to build complex models using generics.

1. Tuple List

As we have said before, tuples are a complex model that can return multiple objects.

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;
	}
}


The code above uses tuples to implement a more complex model.

Let's take another example, a store.


2. Stores

This store consists of an office area, a reception area and a sales area. The sales area consists of several shelves on which a variety of goods need to be placed.

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 {
}

You may understand that the code above is a bit more complicated, let me explain:

1. The first difficulty is the generator, which may be a little easier if you read the previous chapter.In fact, the main purpose of using generator here is to abstract a more general generator. If it is normal code, we can return a produceList directly in the product, which may look much better.

2.Generators, essentially abstracts the generic code that fills the container with data.

3. There are several classes that inherit ArrayList directly. This is to call the add method directly at the time of the constructor, instead of constructing an ArrayList once. If you follow your usual practice, maybe we will build an ArrayList by ourselves and fill it with data.

4. Use anonymous internal classes to create generators in product s.


Summary: This chapter focuses on how to use generics to build complex models.


Thank you for this chapter.

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

Catalog




Reprinted at: https://my.oschina.net/u/2325575/blog/543705

Tags: Java

Posted on Fri, 13 Sep 2019 15:51:27 -0700 by geekette