抽象工厂模式应用场景
举个常见的例子,我们都知道在家电工厂中,很少会有比如只生产冰箱.一个工厂可能会有多种产品,多个工厂生产多种产品的例子。
抽象工厂模式代码
工厂接口
package com.demo.test.abstractFectory.iml;
import com.demo.test.abstractFectory.abs.TVElectric;
import com.demo.test.abstractFectory.abs.WashingElectric;
public interface Factory {
public TVElectric createTVElectric();
public WashingElectric createWashingElectric();
}
杭州工厂具体类
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.TVElectric;
import com.demo.test.abstractFectory.abs.WashingElectric;
import com.demo.test.abstractFectory.iml.Factory;
public class HangZhouFactory implements Factory {
@Override
public TVElectric createTVElectric() {
System.out.println("杭州工厂生产的电视:");
return new TVXiaomiElectric();
}
@Override
public WashingElectric createWashingElectric() {
System.out.println("杭州工厂生产的洗衣机:");
return new WashingLGElectric();
}
}
武汉工厂具体类
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.TVElectric;
import com.demo.test.abstractFectory.abs.WashingElectric;
import com.demo.test.abstractFectory.iml.Factory;
public class WuHanFactory implements Factory {
@Override
public TVElectric createTVElectric() {
System.out.println("武汉工厂生产的电视:");
return new TVLGElectric();
}
@Override
public WashingElectric createWashingElectric() {
System.out.println("武汉工厂生产的洗衣机:");
return new WashingXiaomiElectric();
}
}
电器接口
package com.demo.test.abstractFectory.iml;
public interface Electric{
public void show();
}
洗衣机抽象类
package com.demo.test.abstractFectory.abs;
import com.demo.test.abstractFectory.iml.Electric;
public abstract class WashingElectric implements Electric {
}
彩电抽象类
package com.demo.test.abstractFectory.abs;
import com.demo.test.abstractFectory.iml.Electric;
public abstract class TVElectric implements Electric {
}
LG彩电具体类(要生产的产品)
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.TVElectric;
public class TVLGElectric extends TVElectric {
public void show(){
System.out.println("制造了LG的彩电");
}
}
小米彩电具体类(要生产的产品)
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.TVElectric;
public class TVXiaomiElectric extends TVElectric {
@Override
public void show() {
System.out.println("制造小米彩电");
}
}
LG洗衣机具体类(要生产的产品)
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.WashingElectric;
public class WashingLGElectric extends WashingElectric {
@Override
public void show() {
System.out.println("制造LG洗衣机。。");
}
}
小米洗衣机具体类(要生产的产品)
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.WashingElectric;
import com.demo.test.abstractFectory.iml.Electric;
public class WashingXiaomiElectric extends WashingElectric {
@Override
public void show() {
System.out.println("制造小米洗衣机!!");
}
}
客户端
package com.demo.test.abstractFectory;
import com.demo.test.abstractFectory.abs.TVElectric;
import com.demo.test.abstractFectory.abs.WashingElectric;
import com.demo.test.abstractFectory.iml.Factory;
public class Client {
public static void main(String[] agr){
Factory factory1 = new WuHanFactory();
Factory factory2 = new HangZhouFactory();
TVElectric tvElectric1 = factory1.createTVElectric();
tvElectric1.show();
TVElectric tvElectric2 = factory2.createTVElectric();
tvElectric2.show();
WashingElectric washingElectric1 = factory1.createWashingElectric();
washingElectric1.show();
WashingElectric washingElectric2 = factory2.createWashingElectric();
washingElectric2.show();
}
}
总结
抽象工厂模式,对于新增的产品,产品线(factory)只要负责自己的部分,不用修改以前的产品内容。