You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tank/src/main/java/com/msb/flyweight/Main.java

67 lines
1.2 KiB

package com.msb.flyweight;/**
* @Author bingor
* @Date 2022/10/14 19:40
* @Description: com.msb.flyweight
* @Version: 1.0
*/
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/14 19:40
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
BulletPool pool = new BulletPool();
for (int i=0; i<10 ; i++) {
Bullet bullet = pool.getBullet();
System.out.println(bullet);
}
}
}
class Bullet {
private UUID id = UUID.randomUUID();
private boolean live = false;
@Override
public String toString() {
return "Bullet{" +
"id=" + id +
'}';
}
public boolean isLive() {
return live;
}
}
class BulletPool {
private List<Bullet> bullets = new ArrayList<>();
{
for (int i=0; i<5; i++) {
bullets.add(new Bullet());
}
}
public Bullet getBullet() {
for (Bullet bullet : bullets) {
if( ! bullet.isLive()) return bullet;
}
return new Bullet();
}
}