Compare commits
38 Commits
master
...
DesignPatt
Author | SHA1 | Date |
---|---|---|
bingor | df100d6237 | 2 years ago |
bingor | e084821a98 | 2 years ago |
bingor | 07b55675f3 | 2 years ago |
bingor | db587a297d | 2 years ago |
bingor_yhj | b45b55167a | 2 years ago |
bingor_yhj | a568aa8241 | 2 years ago |
bingor | 2a3c1c5a3c | 2 years ago |
bingor | 79b5f7ecf5 | 2 years ago |
bingor | 7a789cda65 | 2 years ago |
bingor | e13be0a36b | 2 years ago |
bingor | 5e9a629668 | 2 years ago |
bingor | b90e6bab97 | 2 years ago |
bingor | cc02d545ff | 2 years ago |
bingor | 2cfe9813bd | 2 years ago |
bingor | 6b171063a4 | 2 years ago |
bingor | b5fc39183d | 2 years ago |
bingor | 411050d429 | 2 years ago |
bingor | 9e1e86d93a | 2 years ago |
bingor | a31873c566 | 2 years ago |
bingor | 98ae4fe17a | 2 years ago |
bingor | 32b0fa6c52 | 2 years ago |
bingor | 20198f66df | 2 years ago |
bingor | b4b9ac582b | 2 years ago |
bingor | 761d0c5c75 | 2 years ago |
bingor | ef41cce8ed | 2 years ago |
bingor | d09a6719d7 | 2 years ago |
bingor | 20e075db3c | 2 years ago |
bingor | 09d84ecaec | 2 years ago |
bingor | 0092ad544e | 2 years ago |
bingor | 98546b1759 | 2 years ago |
bingor | 7764e0b07e | 2 years ago |
bingor | bc16e1a196 | 2 years ago |
bingor | 609e8526aa | 2 years ago |
bingor | 17df97368e | 2 years ago |
bingor | 759af1b0c9 | 2 years ago |
bingor | bbd1a0eb5d | 2 years ago |
bingor_yhj | ee1a314d2a | 2 years ago |
bingor_yhj | f38dcc7a03 | 2 years ago |
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.msb</groupId>
|
||||
<artifactId>tank-designPatterns</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.23</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,6 +0,0 @@
|
||||
#tanks count at initialization
|
||||
initTankCount=10
|
||||
tankSpeed=5
|
||||
bulletSpeed=10
|
||||
gameWidth=1080
|
||||
gameHeight=720
|
@ -1,6 +1,6 @@
|
||||
package com.msb;
|
||||
package com.msb.base;
|
||||
|
||||
import com.msb.util.ImageUtil;
|
||||
import com.msb.utils.ImageUtil;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 15:40
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName AK47
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 15:40
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class AK47 extends Weapon {
|
||||
@Override
|
||||
public void shoot() {
|
||||
System.out.println("AK47 shoot……");
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:42
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName AbstractFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:42
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class AbstractFactory {
|
||||
public abstract Food createFood();
|
||||
public abstract Weapon createWeapon();
|
||||
public abstract Vehicle createVehicle();
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 15:41
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Bread
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 15:41
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Bread extends Food {
|
||||
@Override
|
||||
public void printName() {
|
||||
System.out.println("eat bread……");
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 15:39
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Car
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 15:39
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Car extends Vehicle {
|
||||
@Override
|
||||
public void go() {
|
||||
System.out.println("car go……");
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:43
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Food
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:43
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class Food {
|
||||
public abstract void printName();
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:23
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:23
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
AbstractFactory factory = new ModernFactory();
|
||||
Vehicle vehicle = factory.createVehicle();
|
||||
vehicle.go();
|
||||
Weapon weapon = factory.createWeapon();
|
||||
weapon.shoot();
|
||||
Food food = factory.createFood();
|
||||
food.printName();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:55
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName ModernFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:55
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ModernFactory extends AbstractFactory {
|
||||
@Override
|
||||
public Food createFood() {
|
||||
return new Bread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Weapon createWeapon() {
|
||||
return new AK47();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vehicle createVehicle() {
|
||||
return new Car();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:44
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Vehicle
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:44
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class Vehicle {
|
||||
public abstract void go();
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.msb.base.dp.abstractFactory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 16:43
|
||||
* @Description: com.msb.abstractFactory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Weapon
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 16:43
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class Weapon {
|
||||
public abstract void shoot();
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.factory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 14:49
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Car
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 14:49
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Car implements Moveable {
|
||||
@Override
|
||||
public void go() {
|
||||
System.out.println("car go");
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.factory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 15:08
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName CarFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 15:08
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class CarFactory {
|
||||
public Moveable create() {
|
||||
System.out.println("a car create");
|
||||
return new Car();
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.factory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 14:51
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 14:51
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Moveable m = new CarFactory().create(); //or new PlaneFactory().create()
|
||||
m.go();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.base.dp.factory;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 14:58
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Moveable {
|
||||
public void go();
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.msb.base.dp.factory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 14:49
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Car
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 14:49
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Plane implements Moveable {
|
||||
@Override
|
||||
public void go() {
|
||||
System.out.println("plane fly……");
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.msb.base.dp.factory;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 15:04
|
||||
* @Description: com.msb.factory
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName VehicleFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 15:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class SimpleVehicleFactory {
|
||||
|
||||
public Car createCar() {
|
||||
//before processing 例如权限,日志等处理
|
||||
return new Car();
|
||||
}
|
||||
|
||||
public Plane createPlane() {
|
||||
return new Plane();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:12
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Cat implements Comparable<Cat> {
|
||||
|
||||
private int weight, height;
|
||||
|
||||
public Cat(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat o) {
|
||||
if(this.weight < o.weight) return -1;
|
||||
else if(this.weight > o.weight) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cat{" +
|
||||
"weight=" + weight +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:12
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Cat2 {
|
||||
|
||||
private int weight, height;
|
||||
|
||||
public Cat2(int weight, int height) {
|
||||
this.weight = weight;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cat2{" +
|
||||
"weight=" + weight +
|
||||
", height=" + height +
|
||||
'}';
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:56
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class CatHeightComparator implements Comparator<Cat2> {
|
||||
@Override
|
||||
public int compare(Cat2 o1, Cat2 o2) {
|
||||
if(o1.getHeight() < o2.getHeight()) return -1;
|
||||
else if(o1.getHeight() > o2.getHeight()) return 1;
|
||||
else return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:56
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class CatWeightComparator implements Comparator<Cat2> {
|
||||
@Override
|
||||
public int compare(Cat2 o1, Cat2 o2) {
|
||||
if(o1.getWeight() < o2.getWeight()) return -1;
|
||||
else if(o1.getWeight() > o2.getWeight()) return 1;
|
||||
else return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:05
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Comparable<T> {
|
||||
int compareTo(T o);
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:50
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Comparator<T> {
|
||||
public int compare(T o1, T o2);
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:12
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Dog implements Comparable<Dog> {
|
||||
|
||||
private int food;
|
||||
|
||||
public Dog(int food) {
|
||||
this.food = food;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Dog o) {
|
||||
if(this.food < o.food) return -1;
|
||||
else if(this.food > o.food) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dog{" +
|
||||
"food=" + food +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:06
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Sorter {
|
||||
|
||||
public void sort(Comparable[] arr) {
|
||||
for(int i=0; i<arr.length-1; i++) {
|
||||
int minPos = i;
|
||||
for(int j=1; j<arr.length; j++) {
|
||||
minPos = arr[j].compareTo(arr[minPos])==-1 ? j : minPos;
|
||||
}
|
||||
swap(arr, i, minPos);
|
||||
}
|
||||
}
|
||||
|
||||
public void swap(Comparable[] arr, int i, int j) {
|
||||
Comparable temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Cat[] cats = new Cat[]{new Cat(5), new Cat(8), new Cat(3)};
|
||||
Dog[] dogs = new Dog[]{new Dog(6), new Dog(4), new Dog(9)};
|
||||
Sorter sorter = new Sorter();
|
||||
sorter.sort(dogs);
|
||||
System.out.println(Arrays.toString(dogs));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.msb.base.dp.strategy;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022-10-06 17:06
|
||||
* @Description: com.msb.strategy
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Sorter2<T> {
|
||||
|
||||
public void sort(T[] arr, Comparator<T> comparator) {
|
||||
for(int i=0; i<arr.length-1; i++) {
|
||||
int minPos = i;
|
||||
for(int j=1; j<arr.length; j++) {
|
||||
minPos = comparator.compare(arr[j], arr[minPos])==-1 ? j : minPos;
|
||||
}
|
||||
swap(arr, i, minPos);
|
||||
}
|
||||
}
|
||||
|
||||
public void swap(T[] arr, int i, int j) {
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Cat2[] cats = new Cat2[]{new Cat2(3, 5), new Cat2(5, 2), new Cat2(1, 4)};
|
||||
// CatWeightComparator comparator = new CatWeightComparator();
|
||||
CatHeightComparator comparator = new CatHeightComparator();
|
||||
Sorter2<Cat2> sorter = new Sorter2<Cat2>();
|
||||
sorter.sort(cats, comparator);
|
||||
System.out.println(Arrays.toString(cats));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.msb.builder;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 16:34
|
||||
* @Description: com.msb.builder
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName ComplexTerrainBuilder
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 16:34
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ComplexTerrainBuilder implements TerrainBuilder {
|
||||
|
||||
private Terrain terrain = new Terrain();
|
||||
|
||||
@Override
|
||||
public TerrainBuilder buildWall() {
|
||||
Wall wall = new Wall(10, 10, 50, 50);
|
||||
terrain.setWall(wall);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerrainBuilder buildFort() {
|
||||
Fort fort = new Fort(10, 10, 50, 50);
|
||||
terrain.setFort(fort);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerrainBuilder buildMine() {
|
||||
Mine mine = new Mine(10, 10, 50, 50);
|
||||
terrain.setMine(mine);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terrain build() {
|
||||
return terrain;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.msb.builder;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 16:42
|
||||
* @Description: com.msb.builder
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 16:42
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
TerrainBuilder terrainBuilder = new ComplexTerrainBuilder();
|
||||
Terrain terrain = terrainBuilder.buildWall().buildFort().buildMine().build();
|
||||
|
||||
Person person = new Person.PersonBuilder().basicInfo(1, "bingor")
|
||||
.age(33)
|
||||
.score(100)
|
||||
.location("机场路", "8号")
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.msb.builder;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 16:51
|
||||
* @Description: com.msb.builder
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Person
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 16:51
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Person {
|
||||
private int id;
|
||||
private int age;
|
||||
private String name;
|
||||
private int score;
|
||||
private Location location;
|
||||
|
||||
public static class PersonBuilder {
|
||||
Person person = new Person();
|
||||
public PersonBuilder basicInfo(int id, String name) {
|
||||
person.id = id;
|
||||
person.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PersonBuilder age(int age) {
|
||||
person.age = age;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PersonBuilder score(int score) {
|
||||
person.score = score;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PersonBuilder location(String street, String roomNo) {
|
||||
person.location = new Location(street, roomNo);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Person build() {
|
||||
return person;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Location {
|
||||
private String street;
|
||||
private String roomNo;
|
||||
|
||||
public Location(String street, String roomNo) {
|
||||
this.street = street;
|
||||
this.roomNo = roomNo;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.msb.builder;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 15:46
|
||||
* @Description: com.msb.builder
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Terrain
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 15:46
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Terrain {
|
||||
private Wall wall;
|
||||
private Fort fort;
|
||||
private Mine mine;
|
||||
|
||||
public void setWall(Wall wall) {
|
||||
this.wall = wall;
|
||||
}
|
||||
|
||||
public void setFort(Fort fort) {
|
||||
this.fort = fort;
|
||||
}
|
||||
|
||||
public void setMine(Mine mine) {
|
||||
this.mine = mine;
|
||||
}
|
||||
}
|
||||
|
||||
class Wall {
|
||||
private int x, y, width, height;
|
||||
public Wall(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
class Fort {
|
||||
private int x, y, width, height;
|
||||
public Fort(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
class Mine {
|
||||
private int x, y, width, height;
|
||||
public Mine(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.msb.builder;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 16:33
|
||||
* @Description: com.msb.builder
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface TerrainBuilder {
|
||||
public TerrainBuilder buildWall();
|
||||
public TerrainBuilder buildFort();
|
||||
public TerrainBuilder buildMine();
|
||||
public Terrain build();
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.msb.chain;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 17:55
|
||||
* @Description: com.msb.chain
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.inter.Collider;
|
||||
import com.msb.inter.impl.BulletTankCollider;
|
||||
import com.msb.inter.impl.BulletWallCollider;
|
||||
import com.msb.inter.impl.TankTankCollider;
|
||||
import com.msb.inter.impl.TankWallCollider;
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@ClassName ColliderChain
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/11 17:55
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ColliderChain implements Collider {
|
||||
List<Collider> colliders = new LinkedList<>();
|
||||
|
||||
public ColliderChain() {
|
||||
add(new BulletTankCollider());
|
||||
add(new TankTankCollider());
|
||||
add(new TankWallCollider());
|
||||
add(new BulletWallCollider());
|
||||
}
|
||||
|
||||
public void add(Collider collider) {
|
||||
colliders.add(collider);
|
||||
}
|
||||
|
||||
public List<Collider> getColliders() {
|
||||
return colliders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean collide(GameObject o1, GameObject o2) {
|
||||
for (Collider collider : colliders) {
|
||||
if( ! collider.collide(o1, o2)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.chain.servlet.v1;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Filter {
|
||||
public void doFilter(Request request, Response response);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:09
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@ClassName ChainFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:09
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class FilterChain implements Filter {
|
||||
private List<Filter> filters = new ArrayList<>();
|
||||
|
||||
public FilterChain addFilter(Filter filter) {
|
||||
filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(Request request, Response response) {
|
||||
for (Filter filter : filters) {
|
||||
filter.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:11
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:11
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FilterChain chain = new FilterChain();
|
||||
Request request = new Request();
|
||||
request.setStr("request:");
|
||||
Response response = new Response();
|
||||
response.setStr("response:");
|
||||
|
||||
chain.addFilter(new OneFilter()).addFilter(new TwoFilter());
|
||||
chain.doFilter(request, response);
|
||||
System.out.println(request.getStr());
|
||||
System.out.println(response.getStr());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName HtmlFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:05
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class OneFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response) {
|
||||
request.setStr(request.getStr() + "-OneFilter");
|
||||
response.setStr(response.getStr() + "-OneFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Request {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Response {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.msb.chain.servlet.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:08
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName TwoFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:08
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TwoFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response) {
|
||||
request.setStr(request.getStr() + "-TwoFilter");
|
||||
response.setStr(response.getStr() + "-TwoFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.chain.servlet.v3;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Filter {
|
||||
public void doFilter(Request request, Response response, FilterChain chain);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:09
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@ClassName ChainFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:09
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class FilterChain implements Filter {
|
||||
private List<Filter> filters = new ArrayList<>();
|
||||
private int index = 0;
|
||||
|
||||
public FilterChain addFilter(Filter filter) {
|
||||
filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(Request request, Response response, FilterChain chain) {
|
||||
if(index == filters.size()) return;
|
||||
Filter filter = filters.get(index);
|
||||
index++;
|
||||
filter.doFilter(request, response, chain);
|
||||
return;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:11
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:11
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FilterChain chain = new FilterChain();
|
||||
Request request = new Request();
|
||||
request.setStr("request:");
|
||||
Response response = new Response();
|
||||
response.setStr("response:");
|
||||
|
||||
chain.addFilter(new OneFilter()).addFilter(new TwoFilter());
|
||||
chain.doFilter(request, response, chain);
|
||||
System.out.println(request.getStr());
|
||||
System.out.println(response.getStr());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName HtmlFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:05
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class OneFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response, FilterChain chain) {
|
||||
request.setStr(request.getStr() + "-OneFilter");
|
||||
chain.doFilter(request, response, chain);
|
||||
response.setStr(response.getStr() + "-OneFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Request {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Response {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.msb.chain.servlet.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:08
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName TwoFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:08
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TwoFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response, FilterChain chain) {
|
||||
request.setStr(request.getStr() + "-TwoFilter");
|
||||
chain.doFilter(request, response, chain);
|
||||
response.setStr(response.getStr() + "-TwoFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.msb.chain.servlet.v4;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Filter {
|
||||
public void doFilter(Request request, Response response, FilterChain chain);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:09
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@ClassName ChainFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:09
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class FilterChain {
|
||||
private List<Filter> filters = new ArrayList<>();
|
||||
private int index = 0;
|
||||
|
||||
public FilterChain addFilter(Filter filter) {
|
||||
filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void doFilter(Request request, Response response) {
|
||||
if(index == filters.size()) return;
|
||||
Filter filter = filters.get(index);
|
||||
index++;
|
||||
filter.doFilter(request, response, this);
|
||||
return;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:11
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:11
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FilterChain chain = new FilterChain();
|
||||
Request request = new Request();
|
||||
request.setStr("request:");
|
||||
Response response = new Response();
|
||||
response.setStr("response:");
|
||||
|
||||
chain.addFilter(new OneFilter()).addFilter(new TwoFilter());
|
||||
chain.doFilter(request, response);
|
||||
System.out.println(request.getStr());
|
||||
System.out.println(response.getStr());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:05
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName HtmlFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:05
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class OneFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response, FilterChain chain) {
|
||||
request.setStr(request.getStr() + "-OneFilter");
|
||||
chain.doFilter(request, response);
|
||||
response.setStr(response.getStr() + "-OneFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Request {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:04
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Request
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:04
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Response {
|
||||
private String str;
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.msb.chain.servlet.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/18 10:08
|
||||
* @Description: com.msb.chain.servlet.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName TwoFilter
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/18 10:08
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TwoFilter implements Filter {
|
||||
@Override
|
||||
public void doFilter(Request request, Response response, FilterChain chain) {
|
||||
request.setStr(request.getStr() + "-TwoFilter");
|
||||
chain.doFilter(request, response);
|
||||
response.setStr(response.getStr() + "-TwoFilter");
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.msb.composite;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/14 18:49
|
||||
* @Description: com.msb.composite
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@ClassName BranchNode
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/14 18:49
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class BranchNode extends Node {
|
||||
|
||||
private String name;
|
||||
private List<Node> nodes = new ArrayList<>();
|
||||
|
||||
public BranchNode(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print() {
|
||||
System.out.println(this.name);
|
||||
}
|
||||
|
||||
public void addNode(Node node) {
|
||||
nodes.add(node);
|
||||
}
|
||||
|
||||
public List<Node> getNodes() {
|
||||
return this.nodes;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.msb.composite;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/14 16:22
|
||||
* @Description: com.msb.composite
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName LeafNode
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/14 16:22
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class LeafNode extends Node {
|
||||
|
||||
private String content;
|
||||
|
||||
public LeafNode(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print() {
|
||||
System.out.println(this.content);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.msb.composite;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/14 18:54
|
||||
* @Description: com.msb.composite
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/14 18:54
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BranchNode root = new BranchNode("root");
|
||||
BranchNode chapter1 = new BranchNode("chapter1");
|
||||
BranchNode chapter2 = new BranchNode("chapter2");
|
||||
LeafNode c11 = new LeafNode("c11");
|
||||
LeafNode c12 = new LeafNode("c12");
|
||||
BranchNode c2b1 = new BranchNode("c2b1");
|
||||
LeafNode c2b11 = new LeafNode("c2b11");
|
||||
LeafNode c2b12 = new LeafNode("c2b12");
|
||||
|
||||
root.addNode(chapter1);
|
||||
root.addNode(chapter2);
|
||||
chapter1.addNode(c11);
|
||||
chapter1.addNode(c12);
|
||||
chapter2.addNode(c2b1);
|
||||
c2b1.addNode(c2b11);
|
||||
c2b1.addNode(c2b12);
|
||||
|
||||
tree(root, 0);
|
||||
}
|
||||
|
||||
private static void tree(Node root, int depth) {
|
||||
for (int i=0; i<depth; i++) {
|
||||
System.out.print("--");
|
||||
}
|
||||
root.print();
|
||||
if(root instanceof BranchNode) {
|
||||
for(Node node : ((BranchNode) root).getNodes()) {
|
||||
tree(node, depth+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.msb.composite;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/14 16:22
|
||||
* @Description: com.msb.composite
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Node
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/14 16:22
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class Node {
|
||||
public abstract void print();
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.msb.decorator;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/13 10:19
|
||||
* @Description: com.msb.decorator
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
*@ClassName GODecoratory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/13 10:19
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class GODecoratory extends GameObject {
|
||||
|
||||
protected GameObject gameObject;
|
||||
|
||||
public GODecoratory(GameObject gameObject) {
|
||||
this.gameObject = gameObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void paint(Graphics g);
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.msb.decorator;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/13 10:23
|
||||
* @Description: com.msb.decorator
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
*@ClassName RectDecorator
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/13 10:23
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class RectDecorator extends GODecoratory {
|
||||
|
||||
public RectDecorator(GameObject gameObject) {
|
||||
super(gameObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
/*this.x = gameObject.x;
|
||||
this.y = gameObject.y;*/
|
||||
this.x = super.gameObject.x;
|
||||
this.y = super.gameObject.y;
|
||||
gameObject.paint(g); //原来的样式
|
||||
|
||||
//需要装饰的样式
|
||||
Color color = g.getColor();
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawRect(super.gameObject.x, super.gameObject.y, super.gameObject.getWidth()+2, super.gameObject.getHeight()+2);
|
||||
// g.drawRect(this.x, this.y, getWidth()+2, getHeight()+2);
|
||||
g.setColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return super.gameObject.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return super.gameObject.getHeight();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.msb.decorator;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/13 10:23
|
||||
* @Description: com.msb.decorator
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
*@ClassName RectDecorator
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/13 10:23
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TailDecorator extends GODecoratory {
|
||||
|
||||
public TailDecorator(GameObject gameObject) {
|
||||
super(gameObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
/*this.x = gameObject.x;
|
||||
this.y = gameObject.y;*/
|
||||
this.x = super.gameObject.x;
|
||||
this.y = super.gameObject.y;
|
||||
gameObject.paint(g); //原来的样式
|
||||
|
||||
//需要装饰的样式
|
||||
Color color = g.getColor();
|
||||
g.setColor(Color.YELLOW);
|
||||
// g.drawLine(super.gameObject.x, super.gameObject.y, super.gameObject.x+getWidth(), super.gameObject.y+getHeight());
|
||||
// g.drawLine(gameObject.x, gameObject.y, gameObject.x+getWidth(), gameObject.y+getHeight());
|
||||
g.drawLine(this.x, this.y, this.x+getWidth(), this.y+getHeight());
|
||||
g.setColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return super.gameObject.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return super.gameObject.getHeight();
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.msb;
|
||||
package com.msb.enums;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
@ -1,4 +1,4 @@
|
||||
package com.msb;
|
||||
package com.msb.enums;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
@ -0,0 +1,37 @@
|
||||
package com.msb.factorys;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/9 16:42
|
||||
* @Description: com.msb.factorys.abstracts
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.factorys.abstracts.GameFactory;
|
||||
import com.msb.model.CircleExplode;
|
||||
import com.msb.model.GameModel;
|
||||
import com.msb.model.abstracts.BaseBullet;
|
||||
import com.msb.model.abstracts.BaseExplode;
|
||||
import com.msb.model.abstracts.BaseTank;
|
||||
|
||||
/**
|
||||
*@ClassName DefautFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/9 16:42
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class CircleFactory extends GameFactory {
|
||||
@Override
|
||||
public BaseTank createTank(int x, int y, GameModel gameModel) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBullet createBullet(int x, int y, GameModel gameModel) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseExplode createExplode(int x, int y, GameModel gameModel) {
|
||||
return new CircleExplode(x, y, gameModel);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.msb.factorys;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/9 16:42
|
||||
* @Description: com.msb.factorys.abstracts
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.factorys.abstracts.GameFactory;
|
||||
import com.msb.model.Explode;
|
||||
import com.msb.model.GameModel;
|
||||
import com.msb.model.abstracts.BaseBullet;
|
||||
import com.msb.model.abstracts.BaseExplode;
|
||||
import com.msb.model.abstracts.BaseTank;
|
||||
|
||||
/**
|
||||
*@ClassName DefautFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/9 16:42
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class DefaultFactory extends GameFactory {
|
||||
@Override
|
||||
public BaseTank createTank(int x, int y, GameModel gameModel) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseBullet createBullet(int x, int y, GameModel gameModel) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseExplode createExplode(int x, int y, GameModel gameModel) {
|
||||
// return new Explode(x, y, gameModel);
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.msb.factorys.abstracts;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/9 16:36
|
||||
* @Description: com.msb.model.abstracts
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.model.GameModel;
|
||||
import com.msb.model.abstracts.BaseBullet;
|
||||
import com.msb.model.abstracts.BaseExplode;
|
||||
import com.msb.model.abstracts.BaseTank;
|
||||
|
||||
/**
|
||||
*@ClassName AbstractFactory
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/9 16:36
|
||||
*@Version 3.0
|
||||
*/
|
||||
public abstract class GameFactory {
|
||||
public abstract BaseTank createTank(int x, int y, GameModel gameModel);
|
||||
public abstract BaseBullet createBullet(int x, int y, GameModel gameModel);
|
||||
public abstract BaseExplode createExplode(int x, int y, GameModel gameModel);
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.msb.inter;
|
||||
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 9:39
|
||||
* @Description: com.msb.inter
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Collider {
|
||||
public boolean collide(GameObject o1, GameObject o2);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.msb.inter;
|
||||
|
||||
import com.msb.model.Tank;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 11:21
|
||||
* @Description: com.msb
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface FireStrategy extends Serializable {
|
||||
public void fire(Tank tank);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.msb.inter;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/9 16:31
|
||||
* @Description: com.msb.inter
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Paintable {
|
||||
public void paint(Graphics g);
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 9:41
|
||||
* @Description: com.msb.inter.impl
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.inter.Collider;
|
||||
import com.msb.model.Bullet;
|
||||
import com.msb.model.Tank;
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
/**
|
||||
*@ClassName BulletTankCollider
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/11 9:41
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class BulletTankCollider implements Collider {
|
||||
@Override
|
||||
public boolean collide(GameObject o1, GameObject o2) {
|
||||
if(o1 instanceof Bullet && o2 instanceof Tank) {
|
||||
Bullet bullet = (Bullet) o1;
|
||||
Tank tank = (Tank) o2;
|
||||
if(bullet.getGroup() == tank.getGroup()) return true; //没有撞继续执行
|
||||
|
||||
if(bullet.getRectangle().intersects(tank.getRectangle())) {
|
||||
bullet.die();
|
||||
tank.die();
|
||||
}
|
||||
return false; //撞上就不再向下执行了
|
||||
} else if (o1 instanceof Tank && o2 instanceof Bullet) {
|
||||
return collide(o2, o1);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 9:41
|
||||
* @Description: com.msb.inter.impl
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.inter.Collider;
|
||||
import com.msb.model.Bullet;
|
||||
import com.msb.model.Wall;
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
/**
|
||||
*@ClassName BulletWallCollider
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/11 9:41
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class BulletWallCollider implements Collider {
|
||||
@Override
|
||||
public boolean collide(GameObject o1, GameObject o2) {
|
||||
if(o1 instanceof Bullet && o2 instanceof Wall) {
|
||||
Bullet bullet = (Bullet) o1;
|
||||
Wall wall = (Wall) o2;
|
||||
|
||||
if(bullet.getRectangle().intersects(wall.getRectangle())) {
|
||||
bullet.die();
|
||||
}
|
||||
return true; //子弹碰墙,只有子弹消亡,但是墙还是好的,所以责任链还是需要继续
|
||||
} else if (o1 instanceof Wall && o2 instanceof Bullet) {
|
||||
return collide(o2, o1);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 11:21
|
||||
* @Description: com.msb
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.decorator.RectDecorator;
|
||||
import com.msb.decorator.TailDecorator;
|
||||
import com.msb.model.Bullet;
|
||||
import com.msb.model.GameModel;
|
||||
import com.msb.model.Tank;
|
||||
import com.msb.inter.FireStrategy;
|
||||
|
||||
/**
|
||||
*@ClassName DefaultFireStrategy
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 11:21
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class DefaultFireStrategy implements FireStrategy {
|
||||
@Override
|
||||
public void fire(Tank tank) {
|
||||
int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
|
||||
int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2;
|
||||
// new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel());
|
||||
new Bullet(bX, bY, tank.getDir(), tank.getGroup());
|
||||
/*GameModel.INSTANCE.add(
|
||||
new RectDecorator(
|
||||
new TailDecorator(
|
||||
new Bullet(bX, bY, tank.getDir(), tank.getGroup())
|
||||
)
|
||||
)
|
||||
);*/
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/8 11:21
|
||||
* @Description: com.msb
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.model.Bullet;
|
||||
import com.msb.model.GameModel;
|
||||
import com.msb.model.Tank;
|
||||
import com.msb.enums.DirEnum;
|
||||
import com.msb.inter.FireStrategy;
|
||||
|
||||
/**
|
||||
*@ClassName DefaultFireStrategy
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/8 11:21
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class FourFireStrategy implements FireStrategy {
|
||||
@Override
|
||||
public void fire(Tank tank) {
|
||||
int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
|
||||
int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2;
|
||||
for (DirEnum value : DirEnum.values()) {
|
||||
// new Bullet(bX, bY, value, tank.getGroup(), tank.getGameModel());
|
||||
new Bullet(bX, bY, value, tank.getGroup());
|
||||
// GameModel.INSTANCE.add(new Bullet(bX, bY, value, tank.getGroup()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 9:41
|
||||
* @Description: com.msb.inter.impl
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.inter.Collider;
|
||||
import com.msb.model.Tank;
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
/**
|
||||
*@ClassName BulletTankCollider
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/11 9:41
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TankTankCollider implements Collider {
|
||||
@Override
|
||||
public boolean collide(GameObject o1, GameObject o2) {
|
||||
if(o1 instanceof Tank && o2 instanceof Tank) {
|
||||
Tank tank1 = (Tank) o1;
|
||||
Tank tank2 = (Tank) o2;
|
||||
if(tank1.getRectangle().intersects(tank2.getRectangle())) {
|
||||
tank1.goBack();
|
||||
tank2.goBack();
|
||||
}
|
||||
return true; //坦克与坦克相撞,坦克不会消失,所以责任链应该继续
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.msb.inter.impl;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/11 9:41
|
||||
* @Description: com.msb.inter.impl
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import com.msb.inter.Collider;
|
||||
import com.msb.model.Tank;
|
||||
import com.msb.model.Wall;
|
||||
import com.msb.model.abstracts.GameObject;
|
||||
|
||||
/**
|
||||
*@ClassName BulletTankCollider
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/11 9:41
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class TankWallCollider implements Collider {
|
||||
@Override
|
||||
public boolean collide(GameObject o1, GameObject o2) {
|
||||
if(o1 instanceof Tank && o2 instanceof Wall) {
|
||||
Tank tank = (Tank) o1;
|
||||
Wall wall = (Wall) o2;
|
||||
if(tank.getRectangle().intersects(wall.getRectangle())) {
|
||||
tank.goBack();
|
||||
}
|
||||
return true; //坦克与墙相撞,坦克和墙不会消失,所以责任链应该继续
|
||||
} else if((o1 instanceof Wall && o2 instanceof Tank)) {
|
||||
return collide(o2, o1);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.msb.iterator.v1;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:06
|
||||
* @Description: com.msb.iterator.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName ArrayList_
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:06
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ArrayList_ {
|
||||
|
||||
private Object[] objects = new Object[10];
|
||||
private int index = 0;
|
||||
|
||||
public void add(Object o) {
|
||||
if(index == objects.length) {
|
||||
Object[] newObjects = new Object[2*objects.length];
|
||||
System.arraycopy(objects, 0, newObjects, 0, objects.length);
|
||||
objects = newObjects;
|
||||
}
|
||||
objects[index] = o;
|
||||
index ++;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
ArrayList_ list = new ArrayList_();
|
||||
for (int i=0; i<15; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
System.out.println(list.size());
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.msb.iterator.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:06
|
||||
* @Description: com.msb.iterator.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName ArrayList_
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:06
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ArrayList_ implements Collection_ {
|
||||
|
||||
private Object[] objects = new Object[10];
|
||||
private int index = 0;
|
||||
|
||||
@Override
|
||||
public void add(Object o) {
|
||||
if(index == objects.length) {
|
||||
Object[] newObjects = new Object[2*objects.length];
|
||||
System.arraycopy(objects, 0, newObjects, 0, objects.length);
|
||||
objects = newObjects;
|
||||
}
|
||||
objects[index] = o;
|
||||
index ++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return index;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.msb.iterator.v3;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:53
|
||||
* @Description: com.msb.iterator.v3
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Collection_ {
|
||||
public void add(Object o);
|
||||
public int size();
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.msb.iterator.v3;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:54
|
||||
* @Description: com.msb.iterator.v3
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:54
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Collection_ list = new LinkedList_();
|
||||
for (int i=0; i<15; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
System.out.println(list.size());
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.msb.iterator.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:06
|
||||
* @Description: com.msb.iterator.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName ArrayList_
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:06
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ArrayList_ implements Collection_ {
|
||||
|
||||
private Object[] objects = new Object[10];
|
||||
private int index = 0;
|
||||
private int step = 0;
|
||||
|
||||
@Override
|
||||
public void add(Object o) {
|
||||
if(index == objects.length) {
|
||||
Object[] newObjects = new Object[2*objects.length];
|
||||
System.arraycopy(objects, 0, newObjects, 0, objects.length);
|
||||
objects = newObjects;
|
||||
}
|
||||
objects[index] = o;
|
||||
index ++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator_ iterator() {
|
||||
return new ArrayListIterator();
|
||||
}
|
||||
|
||||
private class ArrayListIterator implements Iterator_ {
|
||||
private int currentIndex = 0;
|
||||
|
||||
@Override
|
||||
public boolean hashNext() {
|
||||
if(this.currentIndex >= objects.length) return false;
|
||||
if(objects[this.currentIndex] == null) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object next() {
|
||||
return objects[this.currentIndex++];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.msb.iterator.v4;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:53
|
||||
* @Description: com.msb.iterator.v3
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Collection_ {
|
||||
public void add(Object o);
|
||||
public int size();
|
||||
public Iterator_ iterator();
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.msb.iterator.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 18:59
|
||||
* @Description: com.msb.iterator.v4
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Dog
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 18:59
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Dog {
|
||||
private int id;
|
||||
public Dog(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dog{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.msb.iterator.v4;
|
||||
|
||||
/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 18:00
|
||||
* @Description: com.msb.iterator.v4
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface Iterator_ {
|
||||
public boolean hashNext();
|
||||
public Object next();
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.msb.iterator.v4;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:54
|
||||
* @Description: com.msb.iterator.v3
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
/**
|
||||
*@ClassName Main
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:54
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Collection_ list = new LinkedList_();
|
||||
for (int i=0; i<15; i++) {
|
||||
list.add(new Dog(i));
|
||||
}
|
||||
|
||||
Iterator_ iterator = list.iterator();
|
||||
while (iterator.hashNext()) {
|
||||
Dog dog = (Dog) iterator.next();
|
||||
System.out.println(dog);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.msb.iterator.v5;/**
|
||||
* @Author bingor
|
||||
* @Date 2022/10/17 17:06
|
||||
* @Description: com.msb.iterator.v1
|
||||
* @Version: 1.0
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*@ClassName ArrayList_
|
||||
*@Description TODO
|
||||
*@Author bingor
|
||||
*@Date 2022/10/17 17:06
|
||||
*@Version 3.0
|
||||
*/
|
||||
public class ArrayList_<T> implements Collection_<T> {
|
||||
|
||||
private T[] objects = (T[]) new Object[10];
|
||||
private int index = 0;
|
||||
private int step = 0;
|
||||
|
||||
@Override
|
||||
public void add(T o) {
|
||||
if(index == objects.length) {
|
||||
T[] newObjects = (T[]) new Object[2*objects.length];
|
||||
System.arraycopy(objects, 0, newObjects, 0, objects.length);
|
||||
objects = newObjects;
|
||||
}
|
||||
objects[index] = o;
|
||||
index ++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator_ iterator() {
|
||||
return new ArrayListIterator();
|
||||
}
|
||||
|
||||
private class ArrayListIterator implements Iterator_<T> {
|
||||
private int currentIndex = 0;
|
||||
|
||||
@Override
|
||||
public boolean hashNext() {
|
||||
if(this.currentIndex >= objects.length) return false;
|
||||
if(objects[this.currentIndex] == null) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
return objects[this.currentIndex++];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue