工厂方法

优点:在产品维度扩展方便
想加产品-Bike,只需要Bike实现Moveable接口,然后重写go()

  1. public class CarFactory {
  2. public Moveable create() {
  3. System.out.println("a car created!");
  4. return new Car();
  5. }
  6. }
  1. public interface Moveable {
  2. void go();
  3. }
  1. public class Car implements Moveable {
  2. public void go() {
  3. System.out.println("Car go wuwuwuwuw....");
  4. }
  5. }
  1. public class Broom implements Moveable{
  2. @Override
  3. public void go() {
  4. System.out.println("broom flying chuachuachua .....");
  5. }
  6. }
  1. public class Plane implements Moveable {
  2. public void go() {
  3. System.out.println("plane flying shushua....");
  4. }
  5. }
  1. public class Main {
  2. public static void main(String[] args) {
  3. Moveable m = new CarFactory().create();
  4. m.go();
  5. }
  6. }

抽象工厂

优点:在产品族维度扩展方便,在产品维度扩展麻烦。如下:如果再加一个产品族-火星工厂,只需要创建一个MarsFactory继承AbastractFactory,然后实现各个抽象方法就行了;但是如果在AbastractFactory中再加一个产品-Cloth,那么之前的所有工厂都需要加Cloth的实现。

  1. public abstract class AbastractFactory {
  2. abstract Food createFood();
  3. abstract Vehicle createVehicle();
  4. abstract Weapon createWeapon();
  5. }
  1. //魔法工厂
  2. public class MagicFactory extends AbastractFactory {
  3. @Override
  4. Food createFood() {
  5. return new MushRoom();
  6. }
  7. @Override
  8. Vehicle createVehicle() {
  9. return new Broom();
  10. }
  11. @Override
  12. Weapon createWeapon() {
  13. return new MagicStick();
  14. }
  15. }
  1. //现代工厂
  2. public class ModernFactory extends AbastractFactory {
  3. @Override
  4. Food createFood() {
  5. return new Bread();
  6. }
  7. @Override
  8. Vehicle createVehicle() {
  9. return new Car();
  10. }
  11. @Override
  12. Weapon createWeapon() {
  13. return new AK47();
  14. }
  15. }
  1. public abstract class Food {
  2. abstract void printName();
  3. }
  1. public abstract class Vehicle { //interface
  2. abstract void go();
  3. }
  1. public abstract class Weapon {
  2. abstract void shoot();
  3. }
  1. public class Bread extends Food{
  2. public void printName() {
  3. System.out.println("wdm");
  4. }
  5. }
  1. public class AK47 extends Weapon{
  2. public void shoot() {
  3. System.out.println("tututututu....");
  4. }
  5. }
  1. public class Broom extends Vehicle{
  2. public void go() {
  3. System.out.println("Car go wuwuwuwuw....");
  4. }
  5. }
  1. public class Car extends Vehicle{
  2. public void go() {
  3. System.out.println("Car go wuwuwuwuw....");
  4. }
  5. }
  1. public class MagicStick extends Weapon{
  2. public void shoot() {
  3. System.out.println("diandian....");
  4. }
  5. }
  1. public class MushRoom extends Food{
  2. public void printName() {
  3. System.out.println("dmg");
  4. }
  5. }
  1. public class Main {
  2. public static void main(String[] args) {
  3. AbastractFactory f = new ModernFactory();
  4. Vehicle c = f.createVehicle();
  5. c.go();
  6. Weapon w = f.createWeapon();
  7. w.shoot();
  8. Food b = f.createFood();
  9. b.printName();
  10. }
  11. }