abstract factory

master
Jian Hu 3 years ago
parent 10c0b22f85
commit ffa0db8b4b

@ -0,0 +1,7 @@
package abstractFactory;
public abstract class AbstractFactory {
abstract Food createFood();
abstract Vehicles createVehicle();
abstract Weapon createWeapon();
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Airship extends Vehicles{
@Override
void move() {
System.out.println("Alien move with airship, shu shu shu...");
}
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Ak47 extends Weapon{
@Override
void shoot() {
System.out.println("human shoot with bullet, biu biu biu...");
}
}

@ -0,0 +1,18 @@
package abstractFactory;
public class AlienFactory extends AbstractFactory{
@Override
Food createFood() {
return new Mushroom();
}
@Override
Vehicles createVehicle() {
return new Airship();
}
@Override
Weapon createWeapon() {
return new Thunder();
}
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Bread extends Food{
@Override
void printName() {
System.out.println("human eat bread");
}
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Car extends Vehicles{
@Override
void move() {
System.out.println("human move with car, tu tu tu...");
}
}

@ -0,0 +1,5 @@
package abstractFactory;
public abstract class Food {
abstract void printName();
}

@ -0,0 +1,18 @@
package abstractFactory;
public class HumanFactory extends AbstractFactory{
@Override
Food createFood() {
return new Bread();
}
@Override
Vehicles createVehicle() {
return new Car();
}
@Override
Weapon createWeapon() {
return new Ak47();
}
}

@ -0,0 +1,13 @@
package abstractFactory;
public class Main {
public static void main(String[] args) {
AbstractFactory factory = new HumanFactory();
Food f = factory.createFood();
f.printName();
Vehicles v = factory.createVehicle();
v.move();
Weapon w = factory.createWeapon();
w.shoot();
}
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Mushroom extends Food{
@Override
void printName() {
System.out.println("alien eat mushroom");
}
}

@ -0,0 +1,8 @@
package abstractFactory;
public class Thunder extends Weapon{
@Override
void shoot() {
System.out.println("alien shoot with thunder, zi zi zi...");
}
}

@ -0,0 +1,5 @@
package abstractFactory;
public abstract class Vehicles {
abstract void move();
}

@ -0,0 +1,5 @@
package abstractFactory;
public abstract class Weapon {
abstract void shoot();
}

@ -1,6 +1,6 @@
package factoryMethod;
public class Car implements Vehicle{
public class Car implements Moveable {
@Override
public void go() {
System.out.println("Car starting... go to park...");

@ -1,7 +1,7 @@
package factoryMethod;
public class CarFactory {
public Vehicle createCar(){
public Moveable createCar(){
System.out.println("car created...");
return new Car();
}

@ -1,5 +1,5 @@
package factoryMethod;
public interface Vehicle {
public interface Moveable {
void go();
}

@ -1,6 +1,6 @@
package factoryMethod;
public class Plane implements Vehicle{
public class Plane implements Moveable{
@Override
public void go() {
System.out.println("plant moving.. fly fly fly...");

Loading…
Cancel
Save