更新我已经更新了示例以更好地说明我的问题.我意识到它缺少一个特定点 - 即该CreateLabel()方法总是采用标签类型,因此工厂可以决定要创建的标签类型.事实是,它可能需要获得更多或更少的信息,具体取决于它想要返回的标签类型.
我有一个工厂类,它返回表示要发送到打印机的标签的对象.
工厂类看起来像这样:
public class LargeLabel : ILabel
{
public string TrackingReference { get; private set; }
public LargeLabel(string trackingReference)
{
TrackingReference = trackingReference;
}
}
public class SmallLabel : ILabel
{
public string TrackingReference { get; private set; }
public SmallLabel(string trackingReference)
{
TrackingReference = trackingReference;
}
}
public class LabelFactory
{
public ILabel CreateLabel(LabelType labelType, string trackingReference)
{
switch (labelType)
{
case LabelType.Small:
return new SmallLabel(trackingReference);
case LabelType.Large:
return new LargeLabel(trackingReference);
}
} …Run Code Online (Sandbox Code Playgroud) 假设我有一个TModel:
TModelClass = class of TModel;
TModel = class
procedure DoSomeStuff;
end;
Run Code Online (Sandbox Code Playgroud)
和2个后代:
TModel_A = class(TModel);
TModel_B = class(TModel);
Run Code Online (Sandbox Code Playgroud)
和工厂:
TModelFactory = class
class function CreateModel_A: TModel_A;
class function CreateModel_B: TModel_B;
end;
Run Code Online (Sandbox Code Playgroud)
现在我想重构一下:
TModelFactory = class
class function CreateGenericModel(Model: TModelClass) : TModel
end;
class function TModelFactory.CreateGenericModel(Model: TModelClass) : TModel
begin
...
case Model of
TModel_A: Result := TModel_A.Create;
TModel_B: Result := TModel_B.Create;
end;
...
end;
Run Code Online (Sandbox Code Playgroud)
到目前为止没关系,但每次创建TModel后代时,我都要修改工厂case语句.
我的问题:这可能为我的所有TModel后代创建一个100%的通用工厂,所以每次创建TModel后代我都不需要修改TModelFactory吗?
我尝试使用Delphi 2009泛型,但没有找到有价值的信息,所有都与基本用法TList<T> …
我正在尝试编写一个工厂方法,该方法将创建抽象泛型集合类的派生实例.这是基类......
abstract class ItemBase { }
abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { }
Run Code Online (Sandbox Code Playgroud)
......及其派生类......
class Item : ItemBase { }
class ItemCollection : CollectionBase<Item> {}
Run Code Online (Sandbox Code Playgroud)
现在,我想要一个将创建ItemCollection的工厂方法.但请注意,派生类Item和ItemCollection对于包含此工厂方法的类是未知的.这就是我想象的应该......
static T CreateItemCollection<T>() where T : CollectionBase<ItemBase>, new()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
......我想像这样调用它......
var collection = CreateItemCollection<ItemCollection>();
Run Code Online (Sandbox Code Playgroud)
但是工厂方法不会编译,因为ItemBase必须具有无参数构造函数.并且invokation调用拒绝相信ItemCollection来自CollectionBase<ItemBase>.
有人可以指点我正确的方向吗?谢谢.
我为一个程序集写了一个类,它只能由这个程序集中的工厂实例化.
大会外:
MyClass myClass = factoryInstance.GetInstanceOfMyClass();
Run Code Online (Sandbox Code Playgroud)
应该禁止在外面实施这门课程
禁止:
MyClass myClass = new MyClass();
Run Code Online (Sandbox Code Playgroud)
我见过一些复杂的方法.有一种我想念的简单方法吗?
我尝试使用此代码来实现城堡windsor中的Factory,但它会抛出异常,如本期标题所示.我在这里关注文档的例子.请指出我出错的地方.
using System;
using System.Windows.Forms;
using Castle.Facilities.TypedFactory;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
namespace AsFactoryImplementation
{
interface IDummyComponnentFactory
{
IDummyComponnent creat();
void Relese(IDummyComponnent factory);
}
interface IDummyComponnent
{
void show();
}
class DummyComponnent:IDummyComponnent
{
public void show()
{
Console.WriteLine("just testing this for better performance");
}
}
class Program:WindsorContainer
{
static void Main(string[] args)
{
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<IDummyComponnent>().ImplementedBy<DummyComponnent>().Named("FirstConnection").LifeStyle.Transient
,Component.For<IDummyComponnentFactory>().AsFactory());
var val = container.Resolve<IDummyComponnent>();
val.show();
var val2 = …Run Code Online (Sandbox Code Playgroud) 我正在写一个看起来像这样的工厂类:
public class RepositoryFactory<T> {
public T getRepository(){
if(T is IQuestionRepository){ // This is where I am not sure
return new QuestionRepository();
}
if(T is IAnswerRepository){ // This is where I am not sure
return new AnswerRepository();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是如何检查它T是否是指定的类型interface?
ExecutorService和Service是接口,因此只有抽象方法,这意味着它们的方法没有实现.我们再怎么称呼,例如future.get(),es.submit()和es.shutdown()方法的接口类型的参考?例如,我们为什么要这样做呢?
Future f = ...
f.get();
Run Code Online (Sandbox Code Playgroud)
这是一个更具体的例子:
import java.util.concurrent.*;
class Factorial implements Callable<Long> {
long n;
public Factorial(long n) {
this.n = n;
}
public Long call() throws Exception {
if (n <= 0) {
throw new Exception("for finding factorial, N should be > 0");
}
long fact = 1;
for(long longVal = 1; longVal <= n; longVal++) {
fact *= longVal;
}
return fact;
}
}
class CallableTest {
public static void …Run Code Online (Sandbox Code Playgroud) 我在Spring中实现了一个工厂bean来实例化超类的不同子类.我遇到的问题是超类属性不存在@Autowired(我猜是由于new工厂方法中的命令).这是我的代码:
@Component
public class ConfigBeanImpl implements ConfigBean{
@Override
public String expandParam(String param) {
return String.format("expanded %s", param);
}
}
public abstract class FactoryBean {
@Autowired
protected ConfigBean configBean;
private String property;
protected FactoryBean() {
this.property = configBean.expandParam("property");
}
public abstract String getProperty();
public static FactoryBean GET(int id) {
return new FactoryBeanGet(id);
}
public static FactoryBean POST(String param){
return new FactoryBeanPost(param);
}
}
public class FactoryBeanGet extends FactoryBean {
private int id;
protected FactoryBeanGet(int id) {
this.id …Run Code Online (Sandbox Code Playgroud) 假定基类Foo实现IDisposable。类FooA并FooB继承Foo类。一个简单的工厂方法,FooFactory.Create()根据客户端的需要返回FooA或FooB对象。
在下面的客户端代码(FooTest模块)中,尝试在“使用”语句中使用工厂方法会导致以下编译错误:
“使用”资源变量必须具有显式初始化。
我非常感谢有关通过Using语句支持实例化FooA或FooB(由客户端指定)的实现的任何建议(最佳实践)。不需要工厂-这只是我尝试的方法。理想情况下,我希望FooA和FooB是具有公共基类或接口的独立类。
在此先感谢您提供的任何帮助。
Public Module FooTest
Public Sub Test()
'the following compiles:
Dim f As Foo = FooFactory.Create("A")
f.DoWork()
f.Dispose()
'the following causes a compile error:
''Using' resource variable must have an explicit initialization.
Using f As FooFactory.Create("A")
f.DoWork()
End Using
End Sub
End Module
Public Module FooFactory
Public Function Create(ByVal AorB As String) As Foo
If AorB = "A" Then
Return New FooA
Else
Return New FooB
End If
End Function …Run Code Online (Sandbox Code Playgroud) 我有一堆结构需要添加到我的矢量并给它们一些默认值.
问:可以用更好/更简单的方式完成吗?
编辑我没有C++ 17
//外部结构
typedef struct A {
int member1;
float member2;
char *member3;
// ...
} A;
typedef struct B {
double member5;
float member3;
int *member4;
// ...
} B;
typedef struct C {
char* member5;
char* member2;
float *member3;
// ...
} C;
...
template <class T>
void myClass::addStruct(std::vector<T> &vp)
{
void *sp = nullptr;
if(std::is_same<T, A>::value) {
A s{};
s.member1 = 2;
s.member3 = "whatever";
sp …Run Code Online (Sandbox Code Playgroud) factory ×10
c# ×3
java ×3
.net ×1
abstract ×1
autowired ×1
c++ ×1
constraints ×1
delphi ×1
delphi-2009 ×1
exception ×1
generics ×1
inheritance ×1
interface ×1
refactoring ×1
spring ×1
vb.net ×1
visual-c++ ×1