坦克大战(一期)-设计模式-工厂模式

DesignPatterns
bingor 2 years ago
parent bbd1a0eb5d
commit 759af1b0c9

@ -0,0 +1,20 @@
package com.msb.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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();
}
}
Loading…
Cancel
Save