Factory method is simple, the factory mode, the abstract factory pattern

A brief comparison

  • Simple factory method is a programming habit, not a design pattern: a computer factory, responsible for the production of different computers.
  • Factory pattern: Lenovo factory responsible for the production of Lenovo, Dell, Dell Computer factory responsible for the production (actual factory class), they have a computer method of production (realization of a common interface).
  • Abstract Factory: Lenovo factory to produce both computers, but also production of the mouse; Dell factory to produce both computers, but also production of the mouse (the actual factory class). They have a computer and mouse production methods (implement the common interface). Wherein the production of the mouse and the computer are used to achieve factory pattern (comprising a plurality of factory mode).
  • Advantage of the abstract factory pattern is the same plant can guarantee a return of the same product family, such as the use of plant will only produce Logitech Logitech mouse and keyboard. The disadvantage is that expansion more difficult, interface standards has increased by a method, all subclasses implement the interface needs to change.

Factory method is simple: to generate a set of objects into a class, strictly speaking, it is not a design pattern, but a programming practice. (Usually defined as a method of creating static)

Class Diagram

Examples

public interface Computer {
}

public class DellComputer implements Computer {
    public DellComputer(){
        System.out.println ( "Dell" );
    }
}

public class Lenovo implements Computer {
    public Lenovo(){
        System.out.println ( "Lenovo" );
    }
}

public class SimpleComputerFactory {
    static Computer createComputer(String type){
        Computer computer =null;
        if("dell".equalsIgnoreCase(type)){
            computer = new DellComputer();
        }else if("lenovo".equalsIgnoreCase(type)){
            computer = new Lenovo();
        }
        return  computer;
    }
}

run

public class SimpleFactoryTestDemo {
    public static void main(String[] args){
        SimpleComputerFactory.createComputer("dell");
        SimpleComputerFactory.createComputer("lenovo");
    }
}

result

 

 

 

The factory pattern: creating a definition of the object's interface is determined by the class to instantiate a subclass, the subclass is instantiated to delay.

Class Diagram

Examples

public interface Computer {
}

public class Lenovo implements Computer {
   public Lenovo(){
       System.out.println ( "Lenovo" );
   }
}

public class ThinkPad implements Computer {
    public ThinkPad(){
        System.out.println("ThinkPad");
    }
}

public interface  ComputerFactory {
     Computer createCompute(String type) ;
}

public class LenovoComputerFactory implements ComputerFactory{
    @Override
    public Computer createCompute(String type) {
        Computer computer = null;
        if("lenovo".equalsIgnoreCase(type)){
            computer = new Lenovo();
        }else  if("thinkpad".equalsIgnoreCase(type)){
            computer = new ThinkPad();
        }
        return computer;
    }
}

运行

public class ComputerFactoryTestDemo {
    public static void main(String[] args){
        ComputerFactory lenovoComputerFactory = new LenovoComputerFactory();
        lenovoComputerFactory.createCompute("lenovo");
        lenovoComputerFactory.createCompute("thinkpad");
    }
}

结果

抽象工厂模式:提供了一个接口,用来创建相关或依赖或依赖对象的家族,而不需要明确指定具体类。(常包含多个工厂模式,可以看做创建工厂的工厂)

类图

示例

//键盘接口
public interface KeyBoard {
}
public class LogitechKeyBoardA implements KeyBoard {
    public LogitechKeyBoardA(){
        System.out.println("罗技键盘A");
    }
}

public class LogitechKeyBoardB implements KeyBoard {
    public LogitechKeyBoardB(){
        System.out.println("罗技键盘B");
    }
}

public class RazerKeyBoardA implements KeyBoard {
    public RazerKeyBoardA(){
        System.out.println("雷蛇键盘A");
    }
}

public class RazerKeyBoardB implements KeyBoard {
    public RazerKeyBoardB(){
        System.out.println("雷蛇键盘B");
    }
}

//鼠标接口
public interface Mouse {
}

public class LogitechMouseA implements Mouse{
    public LogitechMouseA(){
        System.out.println("罗技鼠标A");
    }
}

public class LogitechMouseB implements Mouse{
    public LogitechMouseB(){
        System.out.println("罗技鼠标B");
    }
}

public class RazerMouseA implements Mouse{
    public RazerMouseA(){
        System.out.println("雷蛇鼠标A");
    }
}

public class RazerMouseB implements Mouse{
    public RazerMouseB(){
        System.out.println("雷蛇鼠标B");
    }
}

//外设工厂接口,负责生产键盘和鼠标
public interface PeripheralsFactory {
     Mouse createMouse(String type);
     KeyBoard createKeyBord(String type);
}

public class LogitechPeripheralsFactory implements PeripheralsFactory {
    @Override
    public Mouse createMouse(String type) {
        Mouse mouse = null;
        if ("A".equalsIgnoreCase(type)) {
            mouse = new LogitechMouseA();
        } else if ("B".equalsIgnoreCase(type)) {
            mouse = new LogitechMouseB();
        }
        return mouse;
    }

    @Override
    public KeyBoard createKeyBord(String type) {
        KeyBoard keyBoard = null;
        if ("A".equalsIgnoreCase(type)) {
            keyBoard = new LogitechKeyBoardA();
        } else if ("B".equalsIgnoreCase(type)) {
            keyBoard = new LogitechKeyBoardB();
        }
        return keyBoard;
    }
}

public class RazerPeripheralsFactory implements PeripheralsFactory {
    @Override
    public Mouse createMouse(String type) {
        Mouse mouse = null;
        if ("A".equalsIgnoreCase(type)) {
            mouse = new RazerMouseA();
        } else if ("B".equalsIgnoreCase(type)) {
            mouse = new RazerMouseB();
        }
        return mouse;
    }

    @Override
    public KeyBoard createKeyBord(String type) {
        KeyBoard keyBoard = null;
        if ("A".equalsIgnoreCase(type)) {
            keyBoard = new RazerKeyBoardA();
        } else if ("B".equalsIgnoreCase(type)) {
            keyBoard = new RazerKeyBoardB();
        }
        return keyBoard;
    }
}

 

运行

public class PeripheralsAbstractFactoryTestDemo {
    public static void main(String[] args){
        PeripheralsFactory razerFactory = new RazerPeripheralsFactory();
        razerFactory.createKeyBord("A");
        razerFactory.createKeyBord("B");
        razerFactory.createMouse("A");
        razerFactory.createMouse("B");
        PeripheralsFactory logitechFactory = new LogitechPeripheralsFactory();
        logitechFactory.createKeyBord("A");
        logitechFactory.createKeyBord("B");
        logitechFactory.createMouse("A");
        logitechFactory.createMouse("B");
    }
}

结果

 

Guess you like

Origin www.cnblogs.com/camcay/p/12355096.html