设计模式2-简单工厂模式
Posted yuec1998
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式2-简单工厂模式相关的知识,希望对你有一定的参考价值。
工厂模式的意义:
将实例化对象的代码提取出来,放到一个类中统一维护和管理,达到和主项目的依赖关系的解耦,从而提高项目的扩展和维护性。
例子1
「反例:」
String medalType = "guest";
if ("guest".equals(medalType)) {
System.out.println("嘉宾勋章");
} else if ("vip".equals(medalType)) {
System.out.println("会员勋章");
} else if ("guard".equals(medalType)) {
System.out.println("展示守护勋章");
}
…
首先,我们把每个条件逻辑代码块,抽象成一个公共的接口,我们根据每个逻辑条件,定义相对应的策略实现类,可得以下代码:
//勋章接口
public interface IMedalService {
void showMedal();
}
//守护勋章策略实现类
public class GuardMedalServiceImpl implements IMedalService {
@Override
public void showMedal() {
System.out.println("展示守护勋章");
}
}
//嘉宾勋章策略实现类
public class GuestMedalServiceImpl implements IMedalService {
@Override
public void showMedal() {
System.out.println("嘉宾勋章");
}
}
//VIP勋章策略实现类
public class VipMedalServiceImpl implements IMedalService {
@Override
public void showMedal() {
System.out.println("会员勋章");
}
}
接下来,我们再定义策略工厂类,用来管理这些勋章实现策略类,如下:
//勋章服务工产类
public class MedalServicesFactory {
private static final Map<String, IMedalService> map = new HashMap<>();
static {
map.put("guard", new GuardMedalServiceImpl());
map.put("vip", new VipMedalServiceImpl());
map.put("guest", new GuestMedalServiceImpl());
}
public static IMedalService getMedalService(String medalType) {
return map.get(medalType);
}
}
优化后,正例如下:
ublic class Test {
public static void main(String[] args) {
String medalType = “guest”;
IMedalService medalService = MedalServicesFactory.getMedalService(medalType);
medalService.showMedal();
}
}
例子2
public class Factory {
public static void main(String[] args) {
new OrderPizza(new SimpleFactory());
}
}
//订购pizza
class OrderPizza{
SimpleFactory simpleFactory;
Pizza pizza;
public OrderPizza (SimpleFactory simpleFactory){
setFactory(simpleFactory);
}
public void setFactory(SimpleFactory simpleFactory){
String orderType="";//用户输入
this.simpleFactory = simpleFactory;
do {
orderType=getType();
pizza=this.simpleFactory.createPizza(orderType);
if (pizza!=null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("订购失败");
}
}while(true);
}
public String getType(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要订购的pizza:");
return scanner.nextLine();
}
}
//简单工厂
class SimpleFactory {
Pizza pizza;
public Pizza createPizza(String orderType) {
if (orderType.equals("cheese")) {
pizza = new CheesePizza();
pizza.setName("奶酪披萨");
}else if(orderType.equals("pepper")){
pizza = new PepperPizza();
pizza.setName("胡椒披萨");
}
return pizza;
}
}
//芝士披萨
class CheesePizza extends Pizza {
@Override
public void prepare() {
System.out.println("准备芝士");
}
}
//胡椒披萨
class PepperPizza extends Pizza {
@Override
public void prepare() {
System.out.println("准备胡椒");
}
}
//pizza
abstract class Pizza {
protected String name;
//准备原材料不同的pizza,抽象方法
public abstract void prepare();
//烘烤
public void bake() {
System.out.println(name + "baking");
}
//切割
public void cut() {
System.out.println(name + "cuting");
}
//打包
public void box() {
System.out.println(name + "boxing");
}
public void setName(String name) {
this.name = name;
}
}
以上是关于设计模式2-简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章