了解工厂方法模式(Understanding the factory method pattern)
我正在阅读有关工厂方法模式的内容。
我可以理解什么时候有一个工厂类,即
StoreFactory#getStore()
,它返回一个基于某个运行时或其他状态的Store
实现。但是,从阅读(例如这个链接 ),似乎有一个人们创建抽象工厂类的一般模式,其他工厂类扩展到:
public abstract class AbstractFactory { public abstract Store getStore(int store); } public class StoreFactoryA extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplA(); } if(Store == 2) { return new MyStoreImplB(); } } } public class StoreFactoryB extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplC(); } if(Store == 2) { return new MyStoreImplD(); } } } public class Runner { public static void main(String[] args) { AbstractFactory storeFactory = new StoreFactoryA(); Store myStore = storeFactory.getStore(1); } }
我的例子是人为的,但模仿上述链接的模型。
这个实现对我来说似乎有点鸡蛋。 使用Factory Method模式消除了客户端代码指定类类型的需要,但现在客户端代码需要有选择地选择要使用的正确工厂,即
StoreFactoryA
,StoreFactoryB
?在这里使用抽象类的原因是什么?
I'm reading about the Factory Method pattern.
I can understand when there is a single factory class, i.e.
StoreFactory#getStore()
, that returns aStore
implementation based on some runtime or other state.But, from reading (e.g. this link), there seems to be a general pattern of people creating an abstract factory class, to which other factory classes extend:
public abstract class AbstractFactory { public abstract Store getStore(int store); } public class StoreFactoryA extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplA(); } if(Store == 2) { return new MyStoreImplB(); } } } public class StoreFactoryB extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplC(); } if(Store == 2) { return new MyStoreImplD(); } } } public class Runner { public static void main(String[] args) { AbstractFactory storeFactory = new StoreFactoryA(); Store myStore = storeFactory.getStore(1); } }
My example is contrived, but models that of the aforementioned link.
This implementation seems kind of chicken-egg to me. Use the Factory Method pattern to eliminate the need for the client code to specify a class type, yet now the client code needs to selectively choose the correct factory to use, i.e.
StoreFactoryA
,StoreFactoryB
?What is the reasoning behind using the abstract class here?
原文:https://stackoverflow.com/questions/12293427
最满意答案
我很确定你的意思是:
window.setInterval(function() {displayTime(timeLeft);}, 1000);
这传递了一个函数引用。
您的原始调用调用
displayTime
然后将其返回值(undefined
)传递给window.setInterval
。I'm pretty sure you mean this:
window.setInterval(function() {displayTime(timeLeft);}, 1000);
That passes a function reference.
Your original call invoked
displayTime
then passed its return value (undefined
) towindow.setInterval
.
相关问答
更多-
你没有在你的清单文件中添加你的HTML文件为什么你的项目不工作,在你的manifest.json中进行一些更改,如下所示: - ....... "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, ....some code here// 另外,您也可以在这里看到铬的工作扩展 https://github.com/MrPardeep/Angular2_typescript_C ...
-
Javascript:window.setInterval没有调用函数(Javascript: window.setInterval not invoking function)[2021-05-16]
我重构了我的代码,使用匿名函数来解决这个问题的setInterval和setTimeout方法。 (-> MYAPP.utilities2 = playAudio: (times, interval) -> audio_url = $(".play-audio").data("audio") myAudio = new Audio(audio_url) return myAudio.play() if times ... -
如果你原谅一个回避你的问题的答案,那么你应该确实不应该使用setInterval (参见本节和本节 )。 关于时间间隔的保证强度是OS和浏览器相关的,因此不是很强。 你最好使用setTimeout来安排一个在必要时使用setTimeout重新安排自己的函数。 如果您担心间隔的准确性,您可以随时根据需要将时间间隔调整为实时。 If you'll excuse an answer that kind of sidesteps your question, there are arguments that you ...
-
someFuncObject.call(o2)实际上调用了该函数。 这意味着,传递给setInterval是从someFuncObject()返回的someFuncObject() 。 setInterval期望的参数是函数本身,而不是函数返回的函数。 如果要在someFuncObject控制此值,可以使用匿名函数,如: window.setInterval(function() { someFuncObject.call(o2); }, 2000); someFuncObject.call(o2 ...
-
用这个 intervalId = window.setInterval(UpdateProgress, 1500); 带有文字参数的setInterval将在无法访问UpdateProgress的全局范围内对此进行eval 。 Use this intervalId = window.setInterval(UpdateProgress, 1500); setInterval with a literal argument will eval this in the global scope whe ...
-
window.setInterval jQuery函数在IE8上不起作用(window.setInterval jQuery function does not work on IE8)[2022-05-15]
你应该使用类似的东西 setInterval("myFunc()",5000); function myFunc() { $('#Notify-Bar').html('a new notification'); } You should use something like setInterval("myFunc()",5000); function myFunc() { $('#Notify-Bar').html('a new notification'); } -
我也遇到了几乎相同的问题。 我试图从我的popup.html启动webauthflow,但是一旦auth流程开始,popup.html将关闭,中止在成功返回令牌时执行的代码。 我的建议是改为在options.html中进行身份验证。 ( https://developer.chrome.com/extensions/optionsV2 )这将启动弹出窗口模式,即使在您的认证流程打开后仍保持打开状态(与popup失去焦点时的popup.html关闭相反),这意味着您的其余部分代码将执行。 希望这可以帮助。 I ...
-
window.setinterval无法使用我的chrome扩展程序(window.setinterval isn't working with my chrome extension)[2023-12-27]
我很确定你的意思是: window.setInterval(function() {displayTime(timeLeft);}, 1000); 这传递了一个函数引用。 您的原始调用调用displayTime然后将其返回值( undefined )传递给window.setInterval 。 I'm pretty sure you mean this: window.setInterval(function() {displayTime(timeLeft);}, 1000); That passes ... -
您的内容脚本与页面脚本(网页中已存在的脚本)的上下文/范围不同。 为了在页面脚本上下文中执行代码,您必须创建