设计模式剖析系列之静态工厂方法

By heiry on 2019-08-05 [ in 技术 ]

作用:在Think in java 系列书籍以及网上大量的文章都写了其各种各样的好处,我认为言过其实,我们用静态工厂方法,主要原因在于可以根据逻辑需求返回实例对象。可以方便实现工厂模式、单例模式。其它如,有语义方法名、返回其返回类型的任何子类型的对象,方便创建参数化类型实例等等都是无关痛痒的东西。

实现:

将创建实例封装在一个工厂类的静态方法中,静态方法不同的参数返回不同的实例。

示例:

如下所示,电脑工厂类,通过提供静态方法getComputer(String Brand)生产不同品牌的电脑(返回Computer实例对象),客户端只需要调用静态工厂方法传入不同参数即可获得目标对象。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package staticfactory;
public interface Computer {
public abstract void cal();
}
package staticfactory; public interface Computer { public abstract void cal(); }
package staticfactory;
public interface Computer {
  public abstract void cal();
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package staticfactory;
public class Mac implements Computer {
@Overrid
public void cal() {
System.out.println("苹果电脑启动。。。。");
}
}
package staticfactory; public class Mac implements Computer { @Overrid public void cal() { System.out.println("苹果电脑启动。。。。"); } }
package staticfactory;
public class Mac implements Computer {
  @Overrid
  public void cal() {
    System.out.println("苹果电脑启动。。。。");
  }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package staticfactory;
public class HP implements Computer {
@Override
public void cal() {
System.out.println("惠普电脑启动。。。。");
}
}
package staticfactory; public class HP implements Computer { @Override public void cal() { System.out.println("惠普电脑启动。。。。"); } }
package staticfactory;
public class HP implements Computer {
  @Override
  public void cal() {
    System.out.println("惠普电脑启动。。。。");

  }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package staticfactory;
public class ComputerFactory {
public static Computer getComputer(String computerBrand) {
switch (computerBrand) {
case "Mac":
return new Mac();
case "HP":
return new HP();
default:
return null;
}
}
}
package staticfactory; public class ComputerFactory { public static Computer getComputer(String computerBrand) { switch (computerBrand) { case "Mac": return new Mac(); case "HP": return new HP(); default: return null; } } }
package staticfactory;
public class ComputerFactory {	
  public static Computer getComputer(String computerBrand) {
    switch (computerBrand) {
    case "Mac":
      return new Mac();
    case "HP":
      return new HP();
    default:
      return null;
    }
  }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package staticfactory;
public class Client {
public static void main(String[] args) {
Computer computer = ComputerFactory.getComputer("Mac");
Computer computer2 = ComputerFactory.getComputer("HP");
computer.cal();
computer2.cal();
}
}
package staticfactory; public class Client { public static void main(String[] args) { Computer computer = ComputerFactory.getComputer("Mac"); Computer computer2 = ComputerFactory.getComputer("HP"); computer.cal(); computer2.cal(); } }
package staticfactory;

public class Client {
  public static void main(String[] args) {
    Computer computer = ComputerFactory.getComputer("Mac");
    Computer computer2 = ComputerFactory.getComputer("HP");
    computer.cal();
    computer2.cal();
  }
}

运行结果:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
苹果电脑启动。。。。
惠普电脑启动。。。。
苹果电脑启动。。。。 惠普电脑启动。。。。
苹果电脑启动。。。。
惠普电脑启动。。。。

 

 >>



© 2009-2024 MOSANG.NET DESIGNED BY HEIRY