淘先锋技术网

首页 1 2 3 4 5 6 7

1.简单工厂模式

❤场景辅助理解

        一个面条工厂生产不同的面条

❤模式分析

        简单工厂模式是由一个工厂对象利用if或者 switch等逻辑判断创建不同的产品,当需要增加产品时,需要修改工厂方法的代码,即新增一个逻辑条件,违背开闭原则

❤源码撑腰

public interface Noodles {
	void getNoodles();
}
public class LaNoodles implements Noodles{
	@Override
	public void getNoodles() {
		System.out.println("一碗拉面");
	}	
}
public class PaoNoodles implements Noodles {
	@Override
	public void getNoodles() {
		System.out.println("一碗泡面");
	}
}
public class DaoNoodles implements Noodles {
	@Override
	public void getNoodles() {
		System.out.println("一碗刀削面");
	}
}
public class NoodlesFactory {
	public Noodles noodlesType(String name) {
		if ("拉面".equals(name)) {
			return new LaNoodles();
		} else if ("泡面".equals(name)) {
			return new PaoNoodles();
		} else if ("刀削面".equals(name)) {
			return new DaoNoodles();
		} else {
			return null;
		}
	}
}
public class Client {
	public static void main(String[] args) {
		NoodlesFactory noodlesFactory = new NoodlesFactory();
		noodlesFactory.noodlesType("拉面").getNoodles();
		noodlesFactory.noodlesType("泡面").getNoodles();
		noodlesFactory.noodlesType("刀削面").getNoodles();
	}
}

❤输出

一碗拉面

一碗泡面

一碗刀削面

2.工厂方法模式

❤场景辅助理解

        这个面条工厂不生产面条了,转而让旗下的A、B、C三个饭店去生产面条,A主营拉面,B主营泡面,C主营刀削面

❤模式分析

        工厂方法模式是定义一个用于创建产品的抽象类或接口,利用java多态让子类或实现类去决定实例化哪一个类,使一个类的实例化延迟到其子类,当需求变化,只需要增删相应的类即可,不需要修改已有的类。

        例NoodlesFactory laFactory = new LaFactory();

        接口为面条工厂NoodlesFactory,直接去new它的实现类拉面工厂LaFactory,然后在拉面工厂中才去实例化产品拉面new LaNoodles();当增加新的面条种类时,只需要增加一个XXFactory类即可

❤源码撑腰

public interface NoodlesFactory {
	public Noodles noodlesType();
}
public class LaFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {
		return new LaNoodles();
	}
}
public class PaoFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {
		return new PaoNoodles();
	}
}
public class DaoFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {	
		return new DaoNoodles();
	}
}
public class Client {
	public static void main(String[] args) {
		NoodlesFactory laFactory = new LaFactory();
		NoodlesFactory paoFactory = new PaoFactory();
		NoodlesFactory daoFactory = new DaoFactory();
		laFactory.noodlesType().getNoodles();
		paoFactory.noodlesType().getNoodles();
		daoFactory.noodlesType().getNoodles();
	}
}

❤输出

一碗拉面

一碗泡面

一碗刀削面

3. 抽象工厂模式

❤场景辅助理解

        这家面条场发现应该转型一站式服务,给客户同时提供饮品,这样也可以增加营业额。所以又增添了新的产品族饮品,同时旗下的ABC三个饭店也会增加相应的产品

❤模式分析

        抽象工厂相对于工厂方法,相当于是把业务进行了拓展,工厂方法是单一的产品族,抽象工厂是多个产品族

❤源码撑腰

public interface Drinks {
	void getDrinks();
}
public class LaDrinks implements Drinks{
	@Override
	public void getDrinks() {
		System.out.println("拉面饮品");
	}
}
public class PaoDrinks implements Drinks{
	@Override
	public void getDrinks() {
		System.out.println("泡面饮品");
	}
}
public class DaoDrinks implements Drinks{
	@Override
	public void getDrinks() {
		System.out.println("刀削面饮品");
	}
}
public interface NoodlesFactory {
	public Noodles noodlesType();
	public Drinks drinksType();
}
public class LaFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {
		return new LaNoodles();
	}
	@Override
	public Drinks drinksType() {
		return new LaDrinks();
	}
}
public class PaoFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {
		return new PaoNoodles();
	}
	@Override
	public Drinks drinksType() {
		return new PaoDrinks();
	}
}
public class DaoFactory implements NoodlesFactory{
	@Override
	public Noodles noodlesType() {	
		return new DaoNoodles();
	}
	@Override
	public Drinks drinksType() {		
		return new DaoDrinks();
	}
}
public class Client {
	public static void main(String[] args) {
		NoodlesFactory laFactory = new LaFactory();
		NoodlesFactory paoFactory = new PaoFactory();
		NoodlesFactory daoFactory = new DaoFactory();
		laFactory.noodlesType().getNoodles();
		laFactory.drinksType().getDrinks();
		
		paoFactory.noodlesType().getNoodles();
		paoFactory.drinksType().getDrinks();
		
		daoFactory.noodlesType().getNoodles();
		daoFactory.drinksType().getDrinks();
	}
}

❤输出

一碗拉面

拉面饮品

一碗泡面

泡面饮品

一碗刀削面

刀削面饮品

总结:三种工厂模式优点

1.  抽象,抽象了对象创建的具体细节

2.  降低耦合,降低了客户端和产品的耦合,比如客户想吃一碗面条,此时不是由客户直接new一个面条的实例,而是交给工厂类