简体   繁体   English

工厂模式-无法创建抽象类或接口的实例

[英]Factory pattern - cannot create an instance of the abstract class or interface

For explanation: I have a code which creates a pattern of symbols (I've got 4 different classes with one pattern each) on the Console and I want to create a "Factory" to decide which class it should use for that symbol pattern, but I can't create an object of this class. 举例说明:我有一个代码可以在Console上创建一个符号模式(我有4个不同的类,每个类都有一个模式),并且我想创建一个“工厂”来决定它应用于该符号模式的类,但我无法创建此类的对象。

//doesn't work --> PrinterFactory is an abstract class
PrinterFactory baumPrinterFactory = new PrinterFactory();

//decides which one to get
Baum b1 = new Nadelbaum() { Kronenhoehe = 10, StammHoehe = 9 };
//Baum b1 = new Laubbaum() { Kronenhoehe = 21 };
//Baum b1 = new Weihnachtsbaum() { Kronenhoehe = 15, StammHoehe = 7 };
//Baum b1 = new Obstbaum() { Kronenhoehe = 32 };

//Prints the Pattern
BaumPrinter baumPrinter = new PrinterFactory();
baumPrinterFactory.GetBaumPrinter();
baumPrinter.Print(b1);    

You don't say, but I'm assuming that each of the other types derives from PrinterFactory (eg: public class Nadelbaum : PrinterFactory { ... } ). 您没有说,但是我假设其他每个类型都派生自PrinterFactory (例如: public class Nadelbaum : PrinterFactory { ... } )。

Normally you'd use a static factory method for this. 通常,您将为此使用static工厂方法。

public abstract class PrinterFactory 
{
   public static PrinterFactory GetInstance(string instanceType) {
       switch (instanceType) {
           case "Nadelbaum": return new Nadelbaum();
           case "Laubbaum": return new Laubbaum();
           // etc
       }
   }
}

Then you can use: 然后,您可以使用:

var printer = PrinterFactory.GetInstance("Nadelbaum");

and printer will be defined as type PrinterFactory but actually be an instance of Nadelbaum . 并且printer将定义为PrinterFactory类型,但实际上是Nadelbaum的实例。

You didn't specify how you choose which instance type to create, so I just showed and example with strings, but you can of course whatever criteria you can think of to choose the type based on whatever parameters you pass to to the factory method. 您没有指定如何选择要创建哪种实例类型的方法,因此我仅显示了字符串示例,但您当然可以根据传递给工厂方法的任何参数来考虑选择哪种类型的标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法创建抽象类或接口的实例,但它不是抽象类,也不是接口? - Cannot create an instance of the abstract class or interface, but it is not an abstract class and it's not an interface? 无法创建抽象类或接口的实例 - Cannot create an instance of the abstract class or interface 无法使用类型创建抽象类或接口接口的实例 - Cannot create an instance of the abstract class or interface interface with type C#:无法创建抽象类或接口的实例 - C#: Cannot create an instance of the abstract class or interface “无法创建抽象类或接口的实例” C#错误消息 - “Cannot create an instance of the abstract class or interface” C# error message 无法创建类型的实例。 类型是接口或抽象类,不能被实例化 - Could not create an instance of type . Type is an interface or abstract class and cannot be instantiated 无法创建抽象类或接口“ System.Drawing.Image”的实例 - Cannot create an instance of the abstract class or interface 'System.Drawing.Image' 无法在WCF Silverlight服务上创建抽象类或接口的实例 - Cannot create an instance of the abstract class or interface on WCF Silverlight Service 无法创建抽象类或接口“ System.Array”的实例 - Cannot create an instance of the abstract class or interface 'System.Array' 无法创建抽象类的实例 - Cannot create instance of abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM