问题的由来:
首先我们来模拟生产车间,车间是生产衣服的,分别生产Adidas和Nike两种品牌;
下面我们先定义Clothes接口,然后有一个show方法。
package factory.normal.clothes;
public interface Clothes {
void show();
}
然后创建两个实现Clothes接口的实现类,Adidas和Nike。
package factory.normal.clothes;
public class Adidas implements Clothes {
@Override
public void show() {
System.out.println("这是Adidas的衣服");
}
}
package factory.normal.clothes;
public class Nike implements Clothes {
@Override
public void show() {
System.out.println("这是Nike的衣服");
}
}
之后模拟顾客买衣服的过程
package factory.normal;
import factory.normal.clothes.Adidas;
public class Customer {
public static void main(String[] args) {
Adidas adidas = new Adidas();
adidas.show();
}
}
可能会出现的问题:如果当我们在项目中分别new了Adidas100次, 一年后由于业务逻辑变更,为类增加了一个属性,构造方法多了一个参数,那么我们就必须找到这100个对象new的地方,用新的构造方法来创建对象,会造成你重复劳动100次,可见这种代码设计虽然简单易懂,但是会存在很大的问题,下面就通过简单工厂模式进行代码优化。
简单工厂模式
我们继续来模拟生产车间,车间是生产衣服的,分别生产Adidas和Nike两种品牌;
下面我们先定义Clothes接口,然后有一个show方法。
package factory.simplefactory.clothes;
public interface Clothes {
void show();
}
然后创建两个实现Clothes接口的实现类,Adidas和Nike。
package factory.simplefactory.clothes;
public class Adidas implements Clothes {
@Override
public void show() {
System.out.println("这是Adidas的衣服");
}
}
package factory.simplefactory.clothes;
public class Nike implements Clothes {
@Override
public void show() {
System.out.println("这是Nike的衣服");
}
}
相比于不使用工厂模式,简单工厂模式它会额外创建一个factory类
package factory.simplefactory.factory;
import factory.simplefactory.clothes.Adidas;
import factory.simplefactory.clothes.Clothes;
import factory.simplefactory.clothes.Nike;
public class Factory {
public Clothes buyClothes(String type) {
Clothes clothes = null;
if (type == "Adidas") {
return new Adidas();
} else if (type == "Nike") {
return new Nike();
}
return null;
}
}
最后模拟顾客买衣服的过程
package factory.simplefactory;
import factory.simplefactory.clothes.Adidas;
import factory.simplefactory.clothes.Clothes;
import factory.simplefactory.factory.Factory;
public class Customer {
public static void main(String[] args) {
Clothes clothe = new Factory().buyClothes("Nike");
clothe.show();
}
}
简单工厂模式相比于不使用工厂模式,它将对象的创建统一起来,便于维护和整体把控, 即使对象的属性发生变化,只要修改下factory中的方法就行了。不过还是违背了开闭原则(如果不清楚开闭原则的小伙伴,可以去看看我的这篇文章 https://www.jianshu.com/p/ceec87adc09f),每次增加新品牌的衣服,都得修改factory中的方法,下面我们通过工厂方法模式来优化设计。
工厂方法模式
首先我们来模拟生产车间,车间是生产衣服的,分别生产Adidas和Nike两种品牌;
下面我们先定义Clothes接口,然后有一个show方法。
package factory.factorymethod.clothes;
public interface Clothes {
void show();
}
然后创建两个实现Clothes接口的实现类,Adidas和Nike。
package factory.factorymethod.clothes;
public class Adidas implements Clothes {
@Override
public void show() {
System.out.println("这是Adidas的衣服");
}
}
package factory.factorymethod.clothes;
public class Nike implements Clothes {
@Override
public void show() {
System.out.println("这是Nike的衣服");
}
}
相比较于简单工厂模式,工厂方法模式会将factory抽象成一个接口,然后由具体品牌衣服具体实现,就是每一种品牌的衣服都有它具体的生产车间
创建一个factory接口
package factory.factorymethod.factory;
import factory.simplefactory.clothes.Clothes;
public interface Factotry {
public Clothes buyClothes();
}
之后创建两个实现factory接口的实现类,AdidasFactory和NikeFactory。
package factory.factorymethod.factory;
import factory.simplefactory.clothes.Clothes;
import factory.simplefactory.clothes.Nike;
public class NikeFactory implements Factotry{
@Override
public Clothes buyClothes() {
return new Nike();
}
}
package factory.factorymethod.factory;
import factory.simplefactory.clothes.Adidas;
import factory.simplefactory.clothes.Clothes;
public class AdidasFactory implements Factotry {
@Override
public Clothes buyClothes() {
return new Adidas();
}
}
最后模拟顾客买衣服的过程
package factory.factorymethod;
import factory.factorymethod.clothes.Clothes;
import factory.factorymethod.factory.NikeFactory;
public class Customer {
public static void main(String[] args) {
Clothes clothe = (Clothes) new NikeFactory().buyClothes();
clothe.show();
}
}
工厂方法模式相比较于简单工厂模式,每种品牌的衣服都有对应的生产车间,生产新的品牌衣服就需要增加新的生产车间,无需修改源代码,符合开闭原则,但是如果衣服品牌过多,就会造成类爆炸。
网友评论