在下面的代码中,我希望根据另一个类的类型返回派生类,这是一种更优雅的方式.
if (option_ is Rectangle)
{
modelInputs = new Foo();
}
else if (option_ is Circle)
{
modelInputs = new Bar();
}
else if (option_ is Triangle)
{
modelInputs = new Bar2();
}
Run Code Online (Sandbox Code Playgroud) 是否有意义默认使用您的域对象工厂的接口,或应接口预留,只有当你需要他们的工厂类?
public IUserFactory
{
User CreateNewUser();
}
public UserFactory : IUserFactory
{
public User CreateNewUser()
{
return new User();
}
}
Run Code Online (Sandbox Code Playgroud) oop design-patterns domain-driven-design factory domain-object
当然,以下内容在Java中不起作用(没有抽象的静态方法)......
public abstract class Animal {
public abstract static Animal getInstance(byte[] b);
}
public class Dog extends Animal {
@Override
public static Dog getInstance(byte[] b) {
// Woof.
return new Dog(...);
}
}
public class Cat extends Animal {
@Override
public static Cat getInstance(byte[] b) {
// Meow.
return new Cat(...);
}
}
Run Code Online (Sandbox Code Playgroud)
要求Animal类具有getInstance实例化自身的静态方法的正确方法是什么?这种方法应该是静态的; 一个"正常"的抽象方法在这里没有意义.
我要创建一个(类)工厂类,接受可变数量的参数并将它们传递给它将调用的类
<?php
class A {
private $a;
private $b;
private $c;
public function __construct($a=1, $b=2, $c=3){
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
class B {
private $foo;
public function __construct(){
$args = func_get_args();
$this->foo = call_user_func_array(array('A', '__construct'), $args);
}
public function getObject(){
return $this->foo;
}
}
$b = new B(10, 20, 30);
var_dump($b->getObject()); // should return equivalent of new A(10,20,30);
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, non-static method A::__construct() …Run Code Online (Sandbox Code Playgroud) 我正在使用工厂模式.它基本上允许类在编译时注册并存储在映射中.然后可以使用BaseFactory :: createInstance()返回一个实例
我不确定地图在编译时如何持有类名!如何在运行时有效的编译时分配内存?
在这种情况下,所有类都是从父类Bump_BaseObject派生的
//C++ STL used for adding Reflection
#include <string>
#include <map>
class Bump_BaseObject;
/**
* Derived Base objects creation factory
*/
template<typename T>
Bump_BaseObject* createT(void)
{
#pragma message("createT instantiated")
return new T();
}
struct BaseFactory {
typedef std::map<std::string, Bump_BaseObject*(*)()> map_type;
//return an instance of the class type 's'
static Bump_BaseObject* createInstance(const std::string& s) {
map_type::iterator it = getMap()->find(s);
if(it == getMap()->end())
return 0;
//this is where we instatiate and allocate memory for the object(it must NOT …Run Code Online (Sandbox Code Playgroud) 尝试使用ninject将泛型参数传递给装饰器链时遇到了问题.也许最好用代码解释一下:
public interface IConnector
{
void Connect();
}
public class CoreConnector : IConnector
{
public void Connect()
{
Console.WriteLine("core connector");
}
}
public class LoggingConnector : IConnector
{
private readonly IConnector conn;
private string id;
public LoggingConnector(IConnector conn, string id)
{
this.conn = conn;
this.id = id;
}
public void Connect()
{
Console.WriteLine("logging conn id : {0}",id);
conn.Connect();
}
}
public class AuditingConnector : IConnector
{
private readonly IConnector conn;
private string id;
public AuditingConnector(IConnector conn, string id)
{
this.conn …Run Code Online (Sandbox Code Playgroud) 在大多数情况下,当我们编写工厂方法时,它是一堆if可以继续增长的条件.编写这种方法的最有效方法是什么(if条件最少)?
public A createA(final String id) {
if (id.equals("A1")) {
return new A1();
}
else if (id.equals("A2")) {
return new A2();
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个AddressBook Angular应用程序的简单示例.我有一个工厂,它返回一个记录数组,它使用List控制器显示在列表视图中
angular.module('abModule', ['ngRoute'])
.factory('AddressBook', function() {
var address_book = [
{
"id": 1,
"first_name": "John",
"last_name": "Doe",
"age": 29
},
{
"id": 2,
"first_name": "Anna",
"last_name": " Smith",
"age": 24
},
{
"id": 3,
"first_name": "Peter",
"last_name": " Jones",
"age": 39
}
];
alert('inside factory function');
return {
get: function() {
return address_book
}
};
})
.config(function($routeProvider) {
$routeProvider
.when('/', {
controller:'list_controller',
templateUrl:'list.html'
})
.when('/add', {
controller:'add_controller',
templateUrl:'add.html'
})
.otherwise({
redirectTo:'/'
});
})
.controller('list_controller',['$scope', function ($scope, AddressBook) {
$scope.address_book …Run Code Online (Sandbox Code Playgroud) 我正在创建一个企业应用程序,即使用户执行了工厂重置,我也需要将其保留在手机上.
如果我将我的Android应用程序设置为系统应用程序,在恢复出厂设置后,它是否会从手机中删除?
如果不可能,那我该怎么办?
谢谢...
我在openstack中子读了下面的代码.
class APIRouter(wsgi.Router):
@classmethod
def factory(cls, global_config, **local_config):
return cls(**local_config)
def __init__(self, **local_config):
# do something. Not using local_config
Run Code Online (Sandbox Code Playgroud)
我这里有两个问题.
从工厂代码我们可以知道它用于创建APIRouter实例.但为什么我们需要呢?为什么我们不只是api_router = ApiRouter()用来获取实例?
在__init__和工厂,local_config并global_config没有使用.为什么我们在功能中定义它?
我想使用工厂而不是构造函数应该有一些优势.就像JAVA中的设计模式一样.我希望答案可以说明优势或原因.更好的一些exapmle