根据此视频输出blog 【IT老齐192】抽象工厂模式在项目开发中的应用
顾名思义即为⼯⼚的⼯⼚,通过构建顶层的抽象⼯⼚和抽象的产品,屏蔽系列产品的构建过程。
抽象⼯⼚也可以称作其他⼯⼚的⼯⼚,它可以在抽象⼯⼚中创建出其他⼯⼚,与⼯⼚模式⼀样,都是⽤来解决接⼝选择的问题,同样都属于创建型模式。
需求
公司早期接⼊七⽜云OSS(对象存储服务)上传图⽚与视频,后因业务调整,公司要求额外⽀持阿⾥云、腾讯云等其他云服务商,并且可以对客户提供外界访问。
设计要求为:允许在不破坏原有代码逻辑情况下,实现对任意三⽅云⼚商的接⼊。
演示代码
抽象工厂与抽象的系列产品接口
public interface OssVideo { public String get720P(); public String get1080P(); } public interface OssImage { public String getThumb() ; public String getWatermark() ; public String getEnhance(); } public interface AbstractOssFactory { public OssImage uploadImage(byte[] bytes); public OssVideo uploadVideo(byte[] bytes); }
//工厂实现类: public class QiniuOssFactory implements AbstractOssFactory { @Override public OssImage uploadImage(byte[] bytes) { return new QiniuOssImage(bytes,"飞冰"); } @Override public OssVideo uploadVideo(byte[] bytes) { return new QiniuOssVideo(bytes,"飞冰"); } } //七牛图片产品: public class QiniuOssImage implements OssImage { private byte[] bytes; public QiniuOssImage(byte[] bytes,String watermark){ this.bytes = bytes; System.out.println("[七⽜云]图⽚已上传⾄七⽜云OSS,URL:http://oss.qiniu.com/xxxxxxx.jpg"); System.out.println("[七⽜云]已⽣成缩略图,尺⼨800X600像素"); System.out.println("[七⽜云]已为图⽚新增⽔印,⽔印⽂本:" + watermark + ",⽂本颜⾊#cccccc"); System.out.println("[七⽜云]已将图⽚AI增强为1080P⾼清画质"); } @Override public String getThumb() { return "http://oss.qiniu.com/xxxxxxx_thumb.jpg"; } @Override public String getWatermark() { return "http://oss.qiniu.com/xxxxxxx_watermark.jpg"; } @Override public String getEnhance() { return "http://oss.qiniu.com/xxxxxxx_enhance.jpg"; } } //七牛视频产品: public class QiniuOssVideo implements OssVideo { private byte[] bytes; public QiniuOssVideo(byte[] bytes, String watermark) { this.bytes = bytes; System.out.println("[七⽜云]视频已上传⾄阿⾥云OSS,URL:http://oss.qini u.com/xxx.mp4"); System.out.println("[七⽜云]1080P转码成功,码率:3500K"); System.out.println("[七⽜云]720P转码成功,码率:2500K"); } @Override public String get720P() { return "http://oss.qiniu.com/xxx_720p_2500.mp4"; } @Override public String get1080P() { return "http://oss.qiniu.com/xxx_1080p_3500.mp4"; } }
//阿里云工厂 public class AliyunOssFactory implements AbstractOssFactory { @Override public OssImage uploadImage(byte[] bytes) { return new AliyunOssImage(bytes,"飞冰",true); } @Override public OssVideo uploadVideo(byte[] bytes) { return new AliyunOssVideo(bytes,"飞冰"); } } //阿里云图片产品 public class AliyunOssImage implements OssImage { private byte[] bytes; public AliyunOssImage(byte[] bytes, String watermark,boolean transparent){ this.bytes = bytes; System.out.println("[阿⾥云]图⽚已上传⾄阿⾥云OSS,URL:http://oss.aliyun.com/xxxxxxx.jpg"); System.out.println("[阿⾥云]已⽣成缩略图,尺⼨640X480像素"); System.out.println("[阿⾥云]已为图⽚新增⽔印,⽔印⽂本:" + watermark + ",⽂本颜⾊:#aaaaaa,背景透明:" + transparent); System.out.println("[阿⾥云]已将图⽚AI增强为4K极清画质"); } @Override public String getThumb() { return "http://oss.aliyun.com/xxxxxxx_thumb.jpg"; } @Override public String getWatermark() { return "http://oss.aliyun.com/xxxxxxx_watermark.jpg"; } @Override public String getEnhance() { return "http://oss.aliyun.com/xxxxxxx_enhance.jpg"; } } //阿里云视频产品 public class AliyunOssVideo implements OssVideo { private byte[] bytes; public AliyunOssVideo(byte[] bytes, String watermark) { this.bytes = bytes; System.out.println("[阿⾥云]视频已上传⾄阿⾥云OSS,URL:http://oss.aliyun.com/xxx.mp4"); System.out.println("[阿⾥云]720P转码成功,码率:5000K"); System.out.println("[阿⾥云]1080P转码成功,码率:7000K"); } @Override public String get720P() { return "http://oss.aliyun.com/xxx_720p.mp4"; } @Override public String get1080P() { return "http://oss.aliyun.com/xxx_1080p.mp4"; } }
⽤户端根本不需要了解阿⾥云/七⽜云的处理细节,直接创建⼯⼚对象就好啦。你建哪个⼯⼚,就采⽤哪个⼯⼚⽣产品,产品的特质也不同。
同时,未来接⼊腾讯云也不需要修改现在的代码,按上⾯的套路实现⼀个⼯⼚和图⽚、视频对象即可
public class Client { public static void main(String[] args) { // AbstractOssFactory factory = new AliyunOssFactory(); AbstractOssFactory factory = new AliyunOssFactory(); OssImage ossImage = factory.uploadImage(new byte[1024]); OssVideo ossVideo = factory.uploadVideo(new byte[1024]); System.out.println(ossImage.getThumb()); System.out.println(ossImage.getWatermark()); System.out.println(ossImage.getEnhance()); System.out.println(ossVideo.get720P()); System.out.println(ossVideo.get1080P()); } }
转载请注明:西门飞冰的博客 » 设计模式—抽象工厂模式