Compare commits

..

38 Commits

Author SHA1 Message Date
bingor df100d6237 坦克大战(一期)-设计模式-templateMethod
2 years ago
bingor e084821a98 坦克大战(一期)-设计模式-备忘录模式1.2
2 years ago
bingor 07b55675f3 坦克大战(一期)-设计模式-备忘录模式1.1
2 years ago
bingor db587a297d 坦克大战(一期)-设计模式-备忘录模式
2 years ago
bingor_yhj b45b55167a 坦克大战(一期)-设计模式版-prototype模型(不完整版)
2 years ago
bingor_yhj a568aa8241 坦克大战(一期)-设计模式版-Builder模式1.1
2 years ago
bingor 2a3c1c5a3c 坦克大战(一期)-设计模式-builder模式
2 years ago
bingor 79b5f7ecf5 坦克大战(一期)-设计模式-责任链模式之模放servlet的调用顺序
2 years ago
bingor 7a789cda65 坦克大战(一期)-设计模式-Visitor访问者模式1.0
2 years ago
bingor e13be0a36b 坦克大战(一期)-设计模式-Iterator迭代器-添加泛型1.1
2 years ago
bingor 5e9a629668 坦克大战(一期)-设计模式-Iterator迭代器-添加泛型
2 years ago
bingor b90e6bab97 坦克大战(一期)-设计模式-Iterator迭代器1.0
2 years ago
bingor cc02d545ff 坦克大战(一期)-设计模式-SpringAop
2 years ago
bingor 2cfe9813bd 坦克大战(一期)-设计模式-Cglib动态代理
2 years ago
bingor 6b171063a4 坦克大战(一期)-设计模式-JDK动态代理
2 years ago
bingor b5fc39183d 坦克大战(一期)-设计模式-静态代理
2 years ago
bingor 411050d429 坦克大战(一期)-设计模式-享元模式
2 years ago
bingor 9e1e86d93a 坦克大战(一期)-设计模式-组合composite(树状结构专用模式)
2 years ago
bingor a31873c566 坦克大战(一期)-设计模式-观察者模式联系
2 years ago
bingor 98ae4fe17a 坦克大战(一期)-设计模式-观察者模式1.0
2 years ago
bingor 32b0fa6c52 坦克大战(一期)-设计模式-装饰者模式应用到子弹上-有bug版1.1
2 years ago
bingor 20198f66df 坦克大战(一期)-设计模式-装饰者模式应用到子弹上-有bug版
2 years ago
bingor b4b9ac582b 坦克大战(一期)-设计模式-添加子弹与墙、坦克与墙的碰撞
2 years ago
bingor 761d0c5c75 坦克大战(一期)-设计模式-添加4块墙
2 years ago
bingor ef41cce8ed 坦克大战(一期)-设计模式-把GameModel做成单例(饿汉式)
2 years ago
bingor d09a6719d7 坦克大战(一期)-设计模式-责任链应用1.2
2 years ago
bingor 20e075db3c 坦克大战(一期)-设计模式-责任链应用1.1
2 years ago
bingor 09d84ecaec 坦克大战(一期)-设计模式-将调停者模式使用到项目中
2 years ago
bingor 0092ad544e 坦克大战(一期)-设计模式-将门面模型使用到项目中,使用GameModel作为facade与Frame打交道
2 years ago
bingor 98546b1759 坦克大战(一期)-设计模式-工厂模式-理解SpringIOC
2 years ago
bingor 7764e0b07e 坦克大战(一期)-设计模式-将抽象工厂模式运用到项目中
2 years ago
bingor bc16e1a196 坦克大战(一期)-设计模式-调整项目结构
2 years ago
bingor 609e8526aa 坦克大战(一期)-设计模式-添加spring的依赖
2 years ago
bingor 17df97368e 坦克大战(一期)-设计模式-项目添加对maven的支持
2 years ago
bingor 759af1b0c9 坦克大战(一期)-设计模式-工厂模式
2 years ago
bingor bbd1a0eb5d 坦克大战(一期)-设计模式-坦克大战子弹发射用策略模式
2 years ago
bingor_yhj ee1a314d2a 坦克大战(一期)-设计模式版-策略模式
2 years ago
bingor_yhj f38dcc7a03 坦克大战(一期)-设计模式版-单例8种设计模式
2 years ago

@ -10,11 +10,11 @@
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<!--<dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>-->
</dependency>
<dependency>
<groupId>org.aspectj</groupId>

@ -1,7 +1,5 @@
package com.msb;
import java.util.Random;
/**
* @Author bingor
* @Date 2022-09-29 11:02
@ -12,21 +10,12 @@ public class TankDemo {
public static void main(String[] args) throws InterruptedException {
TankFrame tankFrame = new TankFrame();
Random random = new Random();
int initTankCount = Integer.parseInt(PropertyMgr.getString("initTankCount"));
//创建5个敌方坦克
for (int i=0; i<initTankCount; i++) {
// tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(30)%4), GroupEnum.BAD, tankFrame));
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame));
}
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑
while (true) {
Thread.sleep(50);
// Thread.sleep(500);
tankFrame.repaint();
}
}
}
}

@ -1,12 +1,13 @@
package com.msb;
import com.msb.enums.DirEnum;
import com.msb.model.GameModel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
@ -17,14 +18,9 @@ import java.util.Objects;
*/
public class TankFrame extends Frame {
Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this);
// Explode explode = new Explode(100, 100, this);
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>(); //敌方坦克
List<Explode> explodes = new ArrayList<>(); //爆炸效果
public static final int GAME_WIDTH = 1000;
public static final int GAME_HEIGHT = 800;
public static final int GAME_WIDTH = 1400;
public static final int GAME_HEIGHT = 1050;
public TankFrame() {
@ -49,36 +45,7 @@ public class TankFrame extends Frame {
@Override
public void paint(Graphics g) {
// super.paint(g);
Color color = g.getColor();
g.setColor(Color.WHITE);
g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.drawString("爆炸的数量:" + explodes.size(), 10, 120);
g.setColor(color);
myTank.paint(g);
// explode.paint(g);
//画出敌方坦克
for (int i=0; i<tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i=0; i<bullets.size(); i++) {
for (int j=0; j<tanks.size(); j++) {
bullets.get(i).collide(tanks.get(j));
}
}
//爆炸效果
for (int i=0; i<explodes.size(); i++) {
explodes.get(i).paint(g);
}
GameModel.getInstance().paint(g);
}
//双缓冲,解决闪烁现象
@ -114,6 +81,8 @@ public class TankFrame extends Frame {
case KeyEvent.VK_RIGHT : bR = true; break;
case KeyEvent.VK_UP : bU = true; break;
case KeyEvent.VK_DOWN : bD = true; break;
case KeyEvent.VK_S: GameModel.getInstance().save();
case KeyEvent.VK_L: GameModel.getInstance().load();
default: break;
}
setDir();
@ -127,7 +96,7 @@ public class TankFrame extends Frame {
case KeyEvent.VK_RIGHT : bR = false; break;
case KeyEvent.VK_UP : bU = false; break;
case KeyEvent.VK_DOWN : bD = false; break;
case KeyEvent.VK_SPACE: myTank.fire(); break;
case KeyEvent.VK_SPACE: GameModel.getInstance().getMainTank().fire(); break;
default: break;
}
setDir();
@ -136,16 +105,16 @@ public class TankFrame extends Frame {
private void setDir() {
//当没有任何方向键按下的时候,那么让坦克停止
if( !bL && !bR && !bU && !bD ) {
myTank.setMove(false);
GameModel.getInstance().getMainTank().setMove(false);
} else {
myTank.setMove(true);
GameModel.getInstance().getMainTank().setMove(true);
}
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
if(bL) myTank.setDir(DirEnum.LEFT);
if(bR) myTank.setDir(DirEnum.RIGHT);
if(bU) myTank.setDir(DirEnum.UP);
if(bD) myTank.setDir(DirEnum.DOWN);
if(bL) GameModel.getInstance().getMainTank().setDir(DirEnum.LEFT);
if(bR) GameModel.getInstance().getMainTank().setDir(DirEnum.RIGHT);
if(bU) GameModel.getInstance().getMainTank().setDir(DirEnum.UP);
if(bD) GameModel.getInstance().getMainTank().setDir(DirEnum.DOWN);
}
}

@ -1,7 +1,6 @@
package com.msb;
package com.msb.base;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
/**
@ -12,7 +11,9 @@ import java.util.Properties;
*/
public class PropertyMgr {
private static Properties props = new Properties();
private static final Properties props = new Properties();
private PropertyMgr() {}
static {
try {

@ -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.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
* 饿
* JVM线
* 使
*
*
* @Version: 1.0
*/
public class Singleton01 {
private static final Singleton01 INSTANCE = new Singleton01();
private Singleton01() {}
public static Singleton01 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton01 singleton1 = Singleton01.getInstance();
Singleton01 singleton2 = Singleton01.getInstance();
System.out.println(singleton1 == singleton2);
}
}

@ -0,0 +1,29 @@
package com.msb.base.dp.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description: 01使
* @Version: 1.0
*/
public class Singleton02 {
private static final Singleton02 INSTANCE;
static {
INSTANCE = new Singleton02();
}
private Singleton02() {}
public static Singleton02 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton02 singleton1 = Singleton02.getInstance();
Singleton02 singleton2 = Singleton02.getInstance();
System.out.println(singleton1 == singleton2);
}
}

@ -0,0 +1,39 @@
package com.msb.base.dp.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* @Version: 1.0
*/
public class Singleton03 {
private static Singleton03 INSTANCE;
private Singleton03() {}
public static Singleton03 getInstance() {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton03();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,40 @@
package com.msb.base.dp.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton04 {
private static Singleton04 INSTANCE;
private Singleton04() {}
public static synchronized Singleton04 getInstance() {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton04();
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,43 @@
package com.msb.base.dp.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton05 {
private static Singleton05 INSTANCE;
private Singleton05() {}
public static Singleton05 getInstance() {
if(Objects.isNull(INSTANCE)) {
//试图减少同步代码块的方式来提高效率,然而不可行,仍然会有线程不安全问题
synchronized (Singleton05.class) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton05();
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton05.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,45 @@
package com.msb.base.dp.singleton;
import java.util.Objects;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* 线
* synchronized
* @Version: 1.0
*/
public class Singleton06 {
private static Singleton06 INSTANCE;
private Singleton06() {}
public static Singleton06 getInstance() {
if(Objects.isNull(INSTANCE)) {
//试图减少同步代码块的方式来提高效率,然而不可行,仍然会有线程不安全问题
synchronized (Singleton06.class) {
if(Objects.isNull(INSTANCE)) {
try { //这里是为了测试线程
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
INSTANCE = new Singleton06();
}
}
}
return INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton06.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,32 @@
package com.msb.base.dp.singleton;
/**
* @Author bingor
* @Date 2022-10-06 10:25
* @Description:
*
* JVM
*
* @Version: 1.0
*/
public class Singleton07 {
private Singleton07() {}
private static class Singleton07Holder {
private static final Singleton07 INSTANCE = new Singleton07();
}
public static Singleton07 getInstance() {
return Singleton07Holder.INSTANCE;
}
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton07.getInstance().hashCode());
}).start();
}
}
}

@ -0,0 +1,19 @@
package com.msb.base.dp.singleton;
/**
* @Author bingor
* @Date 2022-10-06 11:20
* @Description: 线
* @Version: 1.0
*/
public enum Singleton08 {
INSTANCE;
public static void main(String[] args) {
for (int i=0; i<100; i++) {
new Thread(() -> { //同样的哈希码,可能是不同的对象。但不同的对象,有不同的哈希码
System.out.println(Singleton08.INSTANCE.hashCode());
}).start();
}
}
}

@ -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,60 @@
package com.msb.iterator.v2;/**
* @Author bingor
* @Date 2022/10/17 17:14
* @Description: com.msb.iterator.v2
* @Version: 1.0
*/
import java.util.Arrays;
/**
*@ClassName LinkedList
*@Description
*@Author bingor
*@Date 2022/10/17 17:14
*@Version 3.0
*/
public class LinkedList_ {
private int size = 0;
private Node head = null;
private Node tail = null;
public void add(Object o) {
Node node = new Node(o);
node.next = null;
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
size++;
}
public int size() {
return size;
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}
class Main {
public static void main(String[] args) {
LinkedList_ list = new LinkedList_();
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,51 @@
package com.msb.iterator.v3;/**
* @Author bingor
* @Date 2022/10/17 17:14
* @Description: com.msb.iterator.v2
* @Version: 1.0
*/
/**
*@ClassName LinkedList
*@Description
*@Author bingor
*@Date 2022/10/17 17:14
*@Version 3.0
*/
public class LinkedList_ implements Collection_ {
private int size = 0;
private Node head = null;
private Node tail = null;
@Override
public void add(Object o) {
Node node = new Node(o);
node.next = null;
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
size++;
}
@Override
public int size() {
return size;
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}

@ -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,74 @@
package com.msb.iterator.v4;/**
* @Author bingor
* @Date 2022/10/17 17:14
* @Description: com.msb.iterator.v2
* @Version: 1.0
*/
/**
*@ClassName LinkedList
*@Description
*@Author bingor
*@Date 2022/10/17 17:14
*@Version 3.0
*/
public class LinkedList_ implements Collection_ {
private int size = 0;
private Node head = null;
private Node tail = null;
@Override
public void add(Object o) {
Node node = new Node(o);
node.next = null;
if(head == null) {
head = node;
tail = node;
}
tail.next = node;
tail = node;
size++;
}
@Override
public int size() {
return size;
}
@Override
public Iterator_ iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator_ {
private Node currentNode = head;
@Override
public boolean hashNext() {
if(null == currentNode) return false;
return true;
}
@Override
public Object next() {
Object o = currentNode.o;
currentNode = currentNode.next;
return o;
}
}
private class Node {
private Object o;
private Node next;
public Node(Object o) {
this.o = o;
}
}
}

@ -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++];
}
}
}

@ -0,0 +1,13 @@
package com.msb.iterator.v5;
/**
* @Author bingor
* @Date 2022/10/17 17:53
* @Description: com.msb.iterator.v3
* @Version: 1.0
*/
public interface Collection_<T> {
public void add(T o);
public int size();
public Iterator_<T> iterator();
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save