工厂方法模式与组合(Factory Method pattern vs composition)
从关于工厂方法模式的GoF章节:
定义创建对象的接口,但让子类决定实例化哪个类 。 工厂方法让类将实例化推迟到子类。
这个想法是让子类决定实例化哪个类,并且GoF实现这个想法,是在超类中提供一个抽象方法,该子类需要重写,从而提供它们自己的实例化逻辑。
我不明白的是,为什么使用抽象方法实现它,而不是使用工厂的类成员。 我会解释。
这是GoF的方式:
public abstract class Creator { public void doStuff() { Product product = createProduct(); /* etc. */ } public abstract Product createProduct(); }
用法:
MyCreator myCreator = new Creator() { @Override public Product createProduct() { return new MyProduct(); } } myCreator.doStuff();
但是为什么要打扰子类等呢? 我建议这种方法:
public class Creator { private Factory factory; /* constructor */ public Creator(Factory factory) { this.factory = factory; } public void doStuff() { Product product = factory.createProduct(); /* etc. */ } }
这就是你使用它的方式:
MyFactory myFactory = new MyFactory(); Creator creator = new Creator(myFactory); creator.doStuff();
那么GoF的方法有什么好处? 为什么不把工厂组成为Creator类而不是用抽象方法表达它呢?
From GoF chapter about the Factory Method pattern:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
The idea is to let subclasses decided which class to instantiate, and GoF's implementation of the idea, is to provide an abstract method in the superclass, that subclasses need to override, and thus providing their own instantiation logic.
What I don't understand is, why implementing it using an abstract method, and not using a class member which is the factory. I'll explain.
This is the GoF way:
public abstract class Creator { public void doStuff() { Product product = createProduct(); /* etc. */ } public abstract Product createProduct(); }
Usage:
MyCreator myCreator = new Creator() { @Override public Product createProduct() { return new MyProduct(); } } myCreator.doStuff();
But why bother with the subclassing, etc.? I suggest this approach:
public class Creator { private Factory factory; /* constructor */ public Creator(Factory factory) { this.factory = factory; } public void doStuff() { Product product = factory.createProduct(); /* etc. */ } }
And this is how you use it:
MyFactory myFactory = new MyFactory(); Creator creator = new Creator(myFactory); creator.doStuff();
So what is the benefit in GoF's approach? Why not composing the Factory into the Creator class instead of expressing it using an abstract method?
原文:https://stackoverflow.com/questions/28767667
相关问答
更多-
ffmpeg - 连续将网络摄像头传输到单个.jpg文件(覆盖)(ffmpeg - Continuously stream webcam to single .jpg file (overwrite))[2023-10-10]
通过使用可以实现我想要的: ./mjpg_streamer -i "input_uvc.so -r 1280×1024 -d /dev/video0 -y" -o "output_http.so -p 8080 -w ./www" ...来自mjpg_streamer的目录。 它将通过在使用地址时在浏览器中显示流来为您完成所有令人讨厌的工作:http:// {IP-OF-THE-SERVER}:8080 /它的重量也足以在Raspberry Pi上运行。 这是一个设置它的好教程。 谢谢您的帮助! It i ... -
根据我的记忆,编写一个DeviceSource类非常简单,你所描述的问题肯定是在live555邮件列表中经常讨论的问题 - 如果你想做的话,你需要尽快获得批准。与rtsp开发有关的任何事情。 您似乎遇到的问题与以下事实有关:某些视频格式是以流媒体编写的,而rtsp服务器可以轻松地传输某些格式,因为它们包含“同步字节”和其他可用于确定的“标记”框架边界结束的地方。 您可以使用的最简单的解决方案是开始使用相机的SDK,并使用它来从相机请求数据。 有许多不同的库和工具包可供您从相机访问数据 - 其中一个是Dire ...
-
我不确切知道,但s16be是整数(16位),而kAudioFormatLinearPCM是float(32位)。 如果我在你的鞋子里,我将只使用s16be和kAudioFormatLinearPCM格式,这意味着修复AudioCodecCtx->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)等。 然后在ffmpeg - > iOS数据流之间插入PCM格式转换步骤。 这篇文章看起来非常有用: iOS Core Audio:在kAudioFormatF ...
-
无法在linux中使用ffmpeg从网络摄像头复用两个实时流(Unable to mux two live streams from webcam using ffmpeg in linux)[2023-08-30]
最近的构建显示: [flv @ 0x28e4d40] at most one video stream is supported in flv 尝试更灵活的输出容器,如Matroska(.mkv)并使用最近的构建,因为0.5.9是古老的。 您只需下载ffmpeg的Linux版本,或者您可以按照分步指南编译ffmpeg 。 A recent build shows: [flv @ 0x28e4d40] at most one video stream is supported in flv Try a m ... -
linux ffmpeg获取rtmp流并转换为实时网络摄像头v4l2(linux ffmpeg get rtmp stream and convert into a live webcam v4l2)[2022-07-11]
要创建虚拟视频设备,您需要安装并启用v4l2loopback 。 “无此类文件或目录”消息表示您未启用该设备。 sudo modprobe v4l2loopback video_nr=2 这将创建/dev/video2设备。 更新 :响应umläute的注释,确保v4l2loopback模块是持久的,以防您的服务器重新启动。 正常运行时间可能很长。 CentOS示例: 持久模块加载 To create virtual video devices you need to install and enable ... -
假设你的ffmpeg是最近的版本,请使用 ffmpeg -f dshow -rtbufsize 64M -i "video=Integrated Webcam" -s 1280x720 -f sdl2 - 将弹出一个带有视频输出的窗口。 对于不太新的版本,您可能需要使用sdl而不是sdl2 。 从http://ffmpeg.zeranoe.com/builds/获取最新版本 Assuming your ffmpeg is a recent build, use ffmpeg -f dshow -rtbuf ...
-
您将需要root权限才能在Web服务器上安装Wowza。 没有为您设置Wowza的cpanel机制(或嵌入式安装程序)。 您可以按照与本地相同的方式进行设置 。 谢谢,马特 You will need root privileges to install Wowza on your web server. There isn't a cpanel mechanism (or embedded installer) that will setup Wowza for you. You would set it ...
-
使用Android设备将实时视频流发送到wowza流引擎(Sending live video stream to wowza streaming engine with Android devices)[2022-07-06]
我发现这个小(但非常有用)的库有三个例子: libstreaming它就像一个魅力! 易于安装和开发。 I found this little (but very useful) library with three examples: libstreaming It works like a charm! Easy to install and develop. -
使用FFMPEG将我的WebCam视频流式传输到YouTube [关闭](Using FFMPEG to stream my WebCam's video to YouTube [closed])[2022-06-29]
您的命令有一些问题: 您没有音频流。 使用dshow您还需要指定audio ,如此处所述。 ffmpeg -f dshow -i video="Integrated Camera":audio="Microphone name here" 如果您不打算捕获音频,请放置无声音频轨道。 -b:a与-ab相同 - 音频比特率。 您必须使用-b:v指定视频比特率。 600k对于1280x720px @ 25fps H.264视频流来说太低了。 YouTube 建议 : 720P 视频比特率范围:1,500 - 4, ... -
您可以使用通用流分段器或特定Apple HTTP直播流(HLS)分段器 。 Linux上的流分段器示例: ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -an -f segment -segment_time 10 -segment_format_options movflags=+faststart out%03d.mp4 您可以使用GOP大小选项( -g )设置常量关键帧间隔,即: -c:v libx264 -r 25 - ...