Compare commits

..

6 Commits

@ -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,10 +1,4 @@
package com.msb.model;
import com.msb.TankFrame;
import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.model.abstracts.GameObject;
package com.msb;
import java.awt.*;
@ -14,36 +8,34 @@ import java.awt.*;
* @Description:
* @Version: 1.0
*/
//public class Bullet extends BaseBullet {
public class Bullet extends GameObject {
public class Bullet {
// private int x, y;
private int x, y;
private DirEnum dir;
public static final int WIDTH = ResourcesMgr.bulletD.getWidth();
public static final int HEIGHT = ResourcesMgr.bulletD.getHeight();
public static final int SPEED = 8;
private boolean live = true;
private GroupEnum group = GroupEnum.BAD;
private TankFrame tankFrame;
private Rectangle rectangle = new Rectangle();
public Bullet(int x, int y, DirEnum dir, GroupEnum group) {
public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tankFrame = tankFrame;
rectangle.x = this.x;
rectangle.x = this.y;
rectangle.width = WIDTH;
rectangle.height = HEIGHT;
GameModel.getInstance().add(this);
}
@Override
public void paint(Graphics g) {
if( ! this.live) {
GameModel.getInstance().remove(this);
tankFrame.bullets.remove(this);
return;
}
@ -63,16 +55,6 @@ public class Bullet extends GameObject {
moving();
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
public void moving() {
switch (dir) {
@ -118,15 +100,7 @@ public class Bullet extends GameObject {
}
public void die() {
private void die() {
this.live = false;
}
public GroupEnum getGroup() {
return group;
}
public Rectangle getRectangle() {
return rectangle;
}
}

@ -1,4 +1,4 @@
package com.msb.enums;
package com.msb;
/**
* @Author bingor

@ -1,8 +1,4 @@
package com.msb.model;
import com.msb.base.ResourcesMgr;
import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.GameObject;
package com.msb;
import java.awt.*;
@ -12,28 +8,27 @@ import java.awt.*;
* @Description: com.msb
* @Version: 1.0
*/
//public class Explode extends BaseExplode {
public class Explode extends GameObject {
public class Explode {
// private int x,y;
private int x,y;
private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.explodeImages[0].getWidth();
public static final int HEIGHT = ResourcesMgr.explodeImages[0].getHeight();
// private boolean live = true;
private int step = 0;
public Explode(int x, int y) {
public Explode(int x, int y, TankFrame tankFrame) {
this.x = x;
this.y = y;
this.tankFrame = tankFrame;
}
@Override
public void paint(Graphics g) {
g.drawImage(ResourcesMgr.explodeImages[step++], x, y, null);
if(step >= ResourcesMgr.explodeImages.length) {
step = 0;
// gameModel.explodes.remove(this);
GameModel.getInstance().remove(this);
tankFrame.explodes.remove(this);
}
}
@ -46,14 +41,4 @@ public class Explode extends GameObject {
return y;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
}

@ -1,4 +1,4 @@
package com.msb.enums;
package com.msb;
/**
* @Author bingor

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

@ -1,6 +1,6 @@
package com.msb.base;
package com.msb;
import com.msb.utils.ImageUtil;
import com.msb.util.ImageUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

@ -1,12 +1,4 @@
package com.msb.model;
import com.msb.TankFrame;
import com.msb.base.PropertyMgr;
import com.msb.base.ResourcesMgr;
import com.msb.enums.DirEnum;
import com.msb.enums.GroupEnum;
import com.msb.inter.FireStrategy;
import com.msb.model.abstracts.GameObject;
package com.msb;
import java.awt.*;
import java.util.Random;
@ -17,53 +9,38 @@ import java.util.Random;
* @Description: com.msb
* @Version: 1.0
*/
//public class Tank extends BaseTank {
public class Tank extends GameObject {
public class Tank {
private int x,y;
private DirEnum dir;
private static final int SPEED = 4;
private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.goodTankU.getWidth();
public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight();
private boolean live = true;
private Random random = new Random();
private GroupEnum group = GroupEnum.BAD;
private Rectangle rectangle = new Rectangle();
private FireStrategy fireStrategy;
private int oldX, oldY;
public Tank(int x, int y, DirEnum dir, GroupEnum group) {
public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tankFrame = tankFrame;
rectangle.x = this.x;
rectangle.x = this.y;
rectangle.width = WIDTH;
rectangle.height = HEIGHT;
try {
if(group == GroupEnum.GOOD) {
fireStrategy = (FireStrategy) Class.forName(PropertyMgr.getString("goodFS")).newInstance();
} else {
fireStrategy = (FireStrategy) Class.forName(PropertyMgr.getString("badFS")).newInstance();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
if( ! live) {
GameModel.getInstance().remove(this);
tankFrame.tanks.remove(this);
return;
}
@ -90,9 +67,6 @@ public class Tank extends GameObject {
if( ! move) return;
this.oldX = this.x;
this.oldY = this.y;
switch (dir) {
case UP: y -= SPEED; break;
case DOWN: y += SPEED; break;
@ -122,10 +96,9 @@ public class Tank extends GameObject {
public void fire() {
//子弹在坦克每个方向上的中心点应该是不一样的. TODO
/*int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));*/ //换成策略模式
fireStrategy.fire(this);
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));
}
public void boundsCheck() {
@ -147,8 +120,7 @@ public class Tank extends GameObject {
this.live = false;
int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
GameModel.getInstance().add(new Explode(eX, eY));
// gameModel.explodes.add(gameModel.gameFactory.createExplode(eX, eY, gameModel));
tankFrame.explodes.add(new Explode(eX, eY, tankFrame));
}
public GroupEnum getGroup() {
@ -159,26 +131,4 @@ public class Tank extends GameObject {
return rectangle;
}
public int getOldX() {
return oldX;
}
public int getOldY() {
return oldY;
}
public void goBack() {
this.x = this.oldX;
this.y = this.oldY;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
}

@ -1,5 +1,7 @@
package com.msb;
import java.util.Random;
/**
* @Author bingor
* @Date 2022-09-29 11:02
@ -10,12 +12,21 @@ 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,13 +1,12 @@
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;
/**
@ -18,9 +17,14 @@ import java.util.Objects;
*/
public class TankFrame extends Frame {
public static final int GAME_WIDTH = 1400;
public static final int GAME_HEIGHT = 1050;
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 TankFrame() {
@ -45,7 +49,36 @@ public class TankFrame extends Frame {
@Override
public void paint(Graphics g) {
// super.paint(g);
GameModel.getInstance().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);
}
}
//双缓冲,解决闪烁现象
@ -81,8 +114,6 @@ 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();
@ -96,7 +127,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: GameModel.getInstance().getMainTank().fire(); break;
case KeyEvent.VK_SPACE: myTank.fire(); break;
default: break;
}
setDir();
@ -105,16 +136,16 @@ public class TankFrame extends Frame {
private void setDir() {
//当没有任何方向键按下的时候,那么让坦克停止
if( !bL && !bR && !bU && !bD ) {
GameModel.getInstance().getMainTank().setMove(false);
myTank.setMove(false);
} else {
GameModel.getInstance().getMainTank().setMove(true);
myTank.setMove(true);
}
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
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);
if(bL) myTank.setDir(DirEnum.LEFT);
if(bR) myTank.setDir(DirEnum.RIGHT);
if(bU) myTank.setDir(DirEnum.UP);
if(bD) myTank.setDir(DirEnum.DOWN);
}
}

@ -1,20 +0,0 @@
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……");
}
}

@ -1,19 +0,0 @@
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();
}

@ -1,20 +0,0 @@
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……");
}
}

@ -1,20 +0,0 @@
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……");
}
}

@ -1,17 +0,0 @@
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();
}

@ -1,25 +0,0 @@
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();
}
}

@ -1,30 +0,0 @@
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();
}
}

@ -1,17 +0,0 @@
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();
}

@ -1,17 +0,0 @@
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();
}

@ -1,20 +0,0 @@
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");
}
}

@ -1,20 +0,0 @@
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();
}
}

@ -1,20 +0,0 @@
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();
}
}

@ -1,11 +0,0 @@
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();
}

@ -1,20 +0,0 @@
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……");
}
}

@ -1,26 +0,0 @@
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();
}
}

@ -1,30 +0,0 @@
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);
}
}

@ -1,29 +0,0 @@
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);
}
}

@ -1,39 +0,0 @@
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();
}
}
}

@ -1,40 +0,0 @@
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();
}
}
}

@ -1,43 +0,0 @@
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();
}
}
}

@ -1,45 +0,0 @@
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();
}
}
}

@ -1,32 +0,0 @@
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();
}
}
}

@ -1,19 +0,0 @@
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();
}
}
}

@ -1,30 +0,0 @@
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 +
'}';
}
}

@ -1,34 +0,0 @@
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;
}
}

@ -1,16 +0,0 @@
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;
}
}

@ -1,16 +0,0 @@
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;
}
}

@ -1,11 +0,0 @@
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);
}

@ -1,11 +0,0 @@
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);
}

@ -1,30 +0,0 @@
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 +
'}';
}
}

@ -1,37 +0,0 @@
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));
}
}

@ -1,38 +0,0 @@
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));
}
}

@ -1,44 +0,0 @@
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;
}
}

@ -1,26 +0,0 @@
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();
}
}

@ -1,59 +0,0 @@
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;
}
}

@ -1,61 +0,0 @@
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;
}
}

@ -1,14 +0,0 @@
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();
}

@ -1,50 +0,0 @@
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;
}
}

@ -1,11 +0,0 @@
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);
}

@ -1,32 +0,0 @@
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);
}
}
}

@ -1,28 +0,0 @@
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());
}
}

@ -1,21 +0,0 @@
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");
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,21 +0,0 @@
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");
}
}

@ -1,11 +0,0 @@
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);
}

@ -1,35 +0,0 @@
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;
}
}

@ -1,28 +0,0 @@
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());
}
}

@ -1,22 +0,0 @@
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");
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,22 +0,0 @@
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");
}
}

@ -1,11 +0,0 @@
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);
}

@ -1,34 +0,0 @@
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;
}
}

@ -1,28 +0,0 @@
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());
}
}

@ -1,22 +0,0 @@
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");
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,25 +0,0 @@
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;
}
}

@ -1,22 +0,0 @@
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");
}
}

@ -1,40 +0,0 @@
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;
}
}

@ -1,27 +0,0 @@
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);
}
}

@ -1,50 +0,0 @@
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);
}
}
}
}

@ -1,17 +0,0 @@
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();
}

@ -1,29 +0,0 @@
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);
}

@ -1,51 +0,0 @@
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();
}
}

@ -1,52 +0,0 @@
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,37 +0,0 @@
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);
}
}

@ -1,38 +0,0 @@
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;
}
}

@ -1,24 +0,0 @@
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);
}

@ -1,66 +0,0 @@
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();
}
}

@ -1,13 +0,0 @@
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);
}

@ -1,15 +0,0 @@
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);
}

@ -1,13 +0,0 @@
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);
}

@ -1,39 +0,0 @@
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;
}
}
}

@ -1,37 +0,0 @@
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;
}
}
}

@ -1,37 +0,0 @@
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())
)
)
);*/
}
}

@ -1,32 +0,0 @@
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()));
}
}
}

@ -1,34 +0,0 @@
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;
}
}
}

@ -1,36 +0,0 @@
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;
}
}
}

@ -1,44 +0,0 @@
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());
}
}

@ -1,60 +0,0 @@
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());
}
}

@ -1,36 +0,0 @@
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;
}
}

@ -1,12 +0,0 @@
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();
}

@ -1,51 +0,0 @@
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;
}
}
}

@ -1,23 +0,0 @@
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());
}
}

@ -1,60 +0,0 @@
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++];
}
}
}

@ -1,13 +0,0 @@
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();
}

@ -1,27 +0,0 @@
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 +
'}';
}
}

@ -1,12 +0,0 @@
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();
}

@ -1,74 +0,0 @@
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;
}
}
}

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

Loading…
Cancel
Save