假设我有这样一个类:
class MonkeyFish
{
MonkeyFish( GlobalObjectA & a, GlobalObjectB & b, GlobalObjectC & c);
private:
GlobalObjectA & m_a;
GlobalObjectB & m_b;
GlobalObjectC & m_c;
}
没有工厂,我需要执行以下操作才能实例化a MonkeyFish.
GlobalObjectA a;
GlobalObjectB b;
GlobalObjectC c;
int main()
{
MonkeyFish * monkey_fish = new MonkeyFish(a, b, c);
monkey_fish->go();
}
另一方面,如果我有MonkeyFishFactory,似乎我必须这样做:
GlobalObjectA a;
GlobalObjectB b;
GlobalObjectC c;
int main()
{
MonkeyFishFactory mf_factory(a, b, c);
MonkeyFish * monkey_fish = mf_factory.buildMonkeyFish("Bob");
monkey_fish->go();
}
我还有全局对象.
即使MonkeyFishFactory本身在GlobalObjects内部创建(因此它们现在在MonkeyFishFactory而不是真正的全局内),似乎MonkeyFishFactory 本身仍然需要是一个全局对象,以便我可以随时访问它来创建一个MonkeyFish. …
和工厂有什么区别Activator.CreateInstance?它们可以互换使用吗?或者我们还需要工厂模式吗?
Smalltalk中是否使用了工厂方法,如果是这样,那么应该如何编写一个,而不是像Java中那样?谢谢.
有人可以解释为什么工厂比测试期间的简单实例更有用吗?更清楚的是,我没有看到以下区别:
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
Run Code Online (Sandbox Code Playgroud)
还有这个
before(:each) do
@user = Factory(:user)
end
Run Code Online (Sandbox Code Playgroud)
其中有以下工厂:
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
Run Code Online (Sandbox Code Playgroud) 我正在使用服务从API中获取一些数据:
angular.module('myApp', [])
.factory('myService', function($q, $timeout) {
var getMessages = function() {
var deferred = $q.defer();
$timeout(function() {
deferred.resolve('Hello world!');
}, 2000);
return deferred.promise;
};
return {
getMessages: getMessages
};
});
Run Code Online (Sandbox Code Playgroud)
我在多个控制器中使用这些数据.
function ControllerA($scope, myService) {
$scope.message = myService.getMessages();
$scope.updateMessage = function(){
$scope.message = 'Hello Max';
};
}
function ControllerB($scope, myService) {
$scope.message = myService.getMessages();
$scope.$watch('message', function(){
// 'Hello Max'
}, true);
}
Run Code Online (Sandbox Code Playgroud)
我想更新每个控制器中的数据,但是当我更改ControllerA中的$ scope.message时,它不会触发ControllerB中的更改.
编辑:问题是我想避免使用"$ broadcast"和"$ on".
有任何想法吗?
这是一个jsfiddle:http://jsfiddle.net/Victa/McLQD/
我正在考虑Angular的CRUD通用工厂(我目前优先使用服务):
app.factory('dataFactory', ['$http', function ($http) {
var urlBase = '/odata/ContentTypes';
// The _object_ defined in the factory is returned to the caller, rather than as with a service,
// where the _function_ defined in the service is returned to the caller
var dataFactory = {};
dataFactory.getContentTypes = function () {
var contentTypes = $http.get(urlBase);
return contentTypes;
};
dataFactory.getContentType = function (id) {
return $http.get(urlBase + '/' + id);
};
dataFactory.insertContentType = function (contentType) {
return $http.post(urlBase, contentType);
};
dataFactory.updateContentType = …Run Code Online (Sandbox Code Playgroud) 我试图在与我的服务文件相同的目录中加载本地json文件.
没有JS错误,在Net选项卡下,我可以看到json文件已加载.
然而,将响应txt设置为var"static_obj"不会加载JSON数据.当我有JSON数据硬编码var WEBTEST = {}时,它加载得很好.为什么?
'use strict';
webTestApp.factory('webtest', function ($q, $timeout) {
var Webtest = {
//Methods exposed for the class
fetch: function(callback){
//fetch the data from below hard coded
var deferred = $q.defer();
var static_obj = [];
var promised = deferred.promise;
var xobj = new XMLHttpRequest();
//giving an artificial delay of .03 seconds..
//controller will render the page & will show the service data after .03 seconds
$timeout(function(){
xobj.overrideMimeType("application/json");
xobj.open('GET', 'angular/scripts/services/webtest.json', true);
xobj.onreadystatechange = function () {
if …Run Code Online (Sandbox Code Playgroud) 此模式使用抽象工厂,然后是工厂的实现。
我确信这两个类有一个标准的命名约定,但我不知道它是什么。
例如:
public abstract class ChocolateFactory { };
public class MyChocolateFactory { } : ChocolateFactory
这里的标准约定是什么?
我在想 ChocolateFactoryBase 或 ConcreteChocolateFactory,但也许还有别的东西(很像 Enums 往往以 Enum 为后缀,例如PetTypeEnum这样你就可以做PetTypeEnum PetType;
希望这不是主观的。
我一直在研究DDD并看到很多代码能够在我目前的工作中构建一个新的api.
我们假设以下分层架构:
该应用服务使用Automapper创建一个域模型对象,并将其传递给域名服务层.那是对的吗?
如果我已经准备好在那里使用域模型对象,我为什么要使用工厂?忽略工厂我会错吗?
由于我使用Automapper映射视图模型对象,以领域模型对象,并在工厂出现?我有一种感觉,我在这里错过了一些大事.
假设我有Foo课:
struct Resource {
void block();
void unblock();
};
struct Foo {
static Foo create() {
Resource resource;
resource.block();
return Foo{resource};
}
~Foo() { resource.unblock(); }
void f() {}
private:
Resource resource;
Foo(Resource resource): resource(resource) {}
};
Run Code Online (Sandbox Code Playgroud)
我是对的并且不能保证~Foo在这样的块中只会被调用一次吗?
{
Foo foo = Foo::create();
foo.f();
}
Run Code Online (Sandbox Code Playgroud)
如果没有保证,如果使用 c++11 和移动语义是否有可能以某种方式修复?例如,不调用unblock_resource移动的 foo,但我不确定是否保证使用移动构造函数/运算符 = 来返回Foo::create?
factory ×10
angularjs ×3
c# ×3
javascript ×3
c++ ×2
abstract ×1
automapper ×1
c++11 ×1
c++17 ×1
copy-elision ×1
global ×1
json ×1
promise ×1
service ×1
smalltalk ×1