提供了一个创建一系列相关或者相互依赖对象的接口,无需指定它们具体的类
客户端(应用层)不依赖于产品类实例如何被创建、实现等细节
强调一系列相关的产品对象(属于同一产品族)一起适用创建对象不需要大量的重复代码
提供一个产品类的库,所有的产品以同样的接口出现,从而使得客户端不依赖于具体的实现
优点
具体产品在应用层的代码隔离,无需关心创建的细节
将一个系列的产品统一到一起创建
缺点
规定了所有可能被创建的产品聚合,产品簇中扩展新的产品困难
增加了系统的抽象性和理解难度
package com.li.factory.abstract1;
//小米手机
public class XiaomiPhone implements IphoneProduct {
@Override
public void start() {
System.out.println("开启小米手机");
}
@Override
public void shutdown() {
System.out.println("关闭小米手机");
}
@Override
public void callup() {
System.out.println("小米打电话");
}
@Override
public void sendSMS() {
System.out.println("小米发短信");
}
}
HuaweiPhone.java
package com.li.factory.abstract1;
public class HuaweiPhone implements IphoneProduct {
@Override
public void start() {
System.out.println("开启华为手机");
}
@Override
public void shutdown() {
System.out.println("关闭华为手机");
}
@Override
public void callup() {
System.out.println("华为打电话");
}
@Override
public void sendSMS() {
System.out.println("华为发短信");
}
}
IphoneProduct.java
package com.li.factory.abstract1;
//手机产品
public interface IphoneProduct {
void start();
void shutdown();
void callup();
void sendSMS();
}
IRouteProduct.java
package com.li.factory.abstract1;
//路由器产品接口
public interface IRouteProduct {
void start();
void shutdown();
void openwifi();
void setting();
}
IProductFactory.java
package com.li.factory.abstract1;
//抽象产品工厂
public interface IProductFactory {
//生产手机
IphoneProduct iphoneProduct();
//生产路由器
IRouteProduct routeProduce();
}
XiaomiRouter.java
package com.li.factory.abstract1;
public class XiaomiRouter implements IRouteProduct {
@Override
public void start() {
System.out.println("开启小米路由器");
}
@Override
public void shutdown() {
System.out.println("关闭小米路由器");
}
@Override
public void openwifi() {
System.out.println("打开小米Wifi");
}
@Override
public void setting() {
System.out.println("小米设置");
}
}
HuaweiRouter.java
package com.li.factory.abstract1;
public class HuaweiRouter implements IRouteProduct {
@Override
public void start() {
System.out.println("开启华为路由器");
}
@Override
public void shutdown() {
System.out.println("关闭华为路由器");
}
@Override
public void openwifi() {
System.out.println("打开华为Wifi");
}
@Override
public void setting() {
System.out.println("华为设置");
}
}
XiaomiFactory.java
package com.li.factory.abstract1;
public class XiaomiFactory implements IProductFactory {
@Override
public IphoneProduct iphoneProduct() {
return new XiaomiPhone();
}
@Override
public IRouteProduct routeProduce() {
return new XiaomiRouter();
}
}
HuaweiFactory.java
package com.li.factory.abstract1;
public class HuaweiFactory implements IProductFactory {
@Override
public IphoneProduct iphoneProduct() {
return new HuaweiPhone();
}
@Override
public IRouteProduct routeProduce() {
return new HuaweiRouter();
}
}
测试
Client.java
package com.li.factory.abstract1;
public class Client {
public static void main(String[] args) {
System.out.println("--------小米系列产品--------");
//小米工厂
XiaomiFactory xiaomiFactory = new XiaomiFactory();
IphoneProduct iphoneProduct = xiaomiFactory.iphoneProduct();
iphoneProduct.callup();
iphoneProduct.sendSMS();
IRouteProduct iRouteProduct =xiaomiFactory.routeProduce();
iRouteProduct.openwifi();
iRouteProduct.setting();
System.out.println("--------华为系列产品--------");
//小米工厂
HuaweiFactory huaweiFactory = new HuaweiFactory();
iphoneProduct = huaweiFactory.iphoneProduct();
iphoneProduct.callup();
iphoneProduct.sendSMS();
iRouteProduct =huaweiFactory.routeProduce();
iRouteProduct.openwifi();
iRouteProduct.setting();
}
}