在很多C++ API(基于COM的那些)中为你创造了一些东西,指向构造对象的指针通常需要作为**指针(并且函数将为你构造和初始化)
您通常会看到以下签名:
HRESULT createAnObject( int howbig, Object **objectYouWantMeToInitialize ) ;
Run Code Online (Sandbox Code Playgroud)
- 但您很少看到新对象作为返回值传递.
除了想要查看错误代码的人,这是什么原因?是否更好地使用**模式而不是返回的指针来实现更简单的操作,例如:
wchar_t* getUnicode( const char* src ) ;
Run Code Online (Sandbox Code Playgroud)
或者这更好地写成:
void getUnicode( const char* src, wchar_t** dst ) ;
Run Code Online (Sandbox Code Playgroud)
我能想到的最重要的事情就是记住释放它,并且由于**某种原因,这种方式往往会提醒我,我也必须解除它.
我目前正在使用我的工厂:
public class AbstractFactory
{
public static AbstractHeader parseHeader(File file)
{
if(AFactory.canRead(file))return AFactory.parseHeader(file);
if(BFactory.canRead(file))return BFactory.parseHeader(file);
throw new UnsupportedOperationException("File ["+file+"] not supported");
}
public static AbstractContent parseContent(AbstractHeader h)
{
if(h instanceof AHeader){
return AFactory.parseContent((AHeader) h);
}
if(h instanceof BHeader){
return BFactory.parseContent((BHeader) h);
}
throw new UnsupportedOperationException("Header not supported");
}
}
Run Code Online (Sandbox Code Playgroud)
parseHeader()将返回AHeader或BHeader的实例,并在稍后的时间内请求AbstractContent.有一个更好的方法吗 ?躲开检查实例?
这是我之前关于如何隐藏继承的构造函数的问题的变体.你如何隐藏继承的方法:
在Delphi允许您构造COM对象的方式之后进行建模:
CoDOMDocument = class
class function Create: IXMLDOMDocument2;
end;
Run Code Online (Sandbox Code Playgroud)
我有一个工厂,创建一个实现接口的对象:
CoCondition = class
public
class function Create: ICondition;
end;
Run Code Online (Sandbox Code Playgroud)
这很好用.虽然在祖先中有一种叫做方法,但它工作正常Create.它的工作原理是因为我没有overload关键字存在.只要我添加overload关键字:Delphi将允许继承的Create方法"闪耀":
CoCondition = class
public
class function Create: ICondition; overload;
end;
Run Code Online (Sandbox Code Playgroud)
所以现在CoCondition有两种Create方法可用:
class function CoCondition.Create: ICondition;
constructor TObject.Create;
Run Code Online (Sandbox Code Playgroud)
你要打电话给哪一个是模棱两可的.修复,显然是没有overload关键字(为什么你,你没有重载任何东西?).好吧,事实证明我正在超载的东西:
CoCondition = class
public
class function Create: ICondition; overload;
class function Create(const ConditionType: TConditionType): ICondition; overload;
class function Create(const PropertyName: string; …Run Code Online (Sandbox Code Playgroud) 我遇到了这个getInstance方法的问题,我尝试的任何方式:
Cannot make a static reference to the non-static type T.
我怎么能做这个工作?任何代码审查也将是一个很好的帮助.我检查了几个相关的帖子,但这似乎是一个特殊的情况.
我的代码:
public class Data<Template>
{
private Template[] data;
private int id;
public Data(Template[] data)
{
this.data = data;
id = Arrays.hashCode(data);
log();
}
public Template[] getData()
{
return data;
}
public Integer getId()
{
return id;
}
private void log()
{
System.out.println(this.toString());
}
@Override
public String toString()
{
StringBuilder tString = new …Run Code Online (Sandbox Code Playgroud) 当decorator工厂接受参数并仍然装饰一个函数时,装饰器不带参数会让人感到困惑
在描述时使用它会很有帮助.
编辑:混淆是一个例子:
def before_run(func):
print "hello from before run"
def handle_arg(a,b):
if(a>0):
a= 100
return func(a,b)
return handle_arg
@before_run
def running_func(a,b):
print "a",a,"b", b
return a+b
Run Code Online (Sandbox Code Playgroud)
编辑:有没有办法通过添加日志选项(true或false)使用装饰工厂来做到这一点?
调用此$ http请求后(带server.refresh();)
MinecraftServer.prototype.refresh = function(){
return $http.get("http://mcping.net/api/" + this.ip).then(this.acceptData);
}
Run Code Online (Sandbox Code Playgroud)
这个函数this是window对象,而不是MinecraftServer对象:
MinecraftServer.prototype.acceptData = function(data){
data = data.data
if(data && data.online){
this.online = data.online;
//do more stuff
} else { // do more stuff }
}
Run Code Online (Sandbox Code Playgroud)
因此,不是让MinecraftServer对象更新它的属性,而是window获取属性.
如果这有帮助,这是我的工厂代码:
.factory('MinecraftServer',function($http){
function MinecraftServer(name, ip) { //does stuff }
MinecraftServer.prototype.acceptData = function(data){
data = data.data
if(data && data.online){
this.online = data.online;
//do more stuff
} else { // do more stuff } …Run Code Online (Sandbox Code Playgroud) 我是Swift编程的新手,我在构建Factory设计模式方面遇到了阻碍.以下是我的代码:
protocol IInterface {
func InterfaceMethod() -> Void
}
public class SubClass: IInterface {
init() {}
func InterfaceMethod() { }
public func SubClassMethod() { }
}
public class Factory() {
class func createFactory() -> IInterface {
return SubClass()
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我试图像下面这样访问它
let mgr = Factory.createFactory()
//calling Interface Method
mgr.InterfaceMethod() -- This works, when i keep a dot (mgr.) it shows the method name
//calling subclass method
mgr.SubClassMethod() -- This doesn't work, when i keep a dot (mgr.) it doesnt even …Run Code Online (Sandbox Code Playgroud) 我想防止除类的工厂之外的任何东西构造类类型的对象。我需要该类具有一个公共接口,但我想将其创建仅限于其工厂。我怎么做?
让我们称之为班级Car和工厂CarFactory。这是我在不使用friend所有私有成员并将其暴露给工厂的情况下如何执行此操作的最初想法:
class Car {
private:
Car();
Car(Car& ref);
friend class CarFactory;
};
class CarFactory {
public:
Car * makeCar();
};
Run Code Online (Sandbox Code Playgroud)
我发现了一个与Java有关的问题:如何使构造函数仅对工厂类可用?
上面的代码按原样工作。澄清一下,我想知道是否有一种方法可以只与工厂共享构造函数,而不能与所有私有成员共享?
Java 9的出现为Java的Collections API带来了许多新功能,其中一个是集合工厂方法.
它们是什么以及如何正确实施?
我想支持Json和Parquet文件格式。客户端不必关心其实现,但是必须传递一种类型来标识格式。
到目前为止,我有两个具有此类签名的类:
class ParquetFileWriter[T](val path: String)(implicit val writer: ParquetWriter[T]) extends FileWriter[T]
Run Code Online (Sandbox Code Playgroud)
和
class JsonFileWriter[T](val path: String)(implicit writer: JsonWriter[T]) extends FileWriter[T]
Run Code Online (Sandbox Code Playgroud)
他们扩展的特征:
trait FileWriter[T] {
def write(items: Seq[T]): Unit
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个工厂类来通过参数构建类:
class Factory {
def of[T](format: Format): FileWriter[T] = {
format match {
case ParquetSpark =>
new ParquetFileWriter[T](defaultPath)
case Json =>
new JsonFileWriter[T](defaultPath)
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是ParquetFileWriter和JsonFileWriter需要隐式变量(当它们从spray.json和com.github.mjakubowski84.parquet4s库到达时,它们不在我的控制范围之内。
如果格式依赖于不同的隐式函数,如何实现工厂格式?我在编译时收到“找不到隐式值”。
factory ×10
java ×3
c++ ×2
angularjs ×1
collections ×1
construction ×1
decorator ×1
delphi ×1
friend ×1
generics ×1
implicit ×1
inheritance ×1
ios ×1
java-9 ×1
javascript ×1
literals ×1
python ×1
python-2.7 ×1
scala ×1
swift ×1