c# - 如何以比 C# 中的接口(interface)更宽松的方式定义模板

标签 c# oop design-patterns

假设我有一组从模板生成文档的类。例如……

class CustomerInvoice
{
    public satic string TemplatePath 
    { 
        get { return @"C:\Templates\InvoiceTemplate.doc"; }
    }

    public static DocumentType DocumentType 
    { 
        get { return DocumentType.WordDocument; }
    }

    public static void Create(Customer customer, int orderNumber)
    {
         //...
    }
}

所有这些类共享相同的方法名称,但不一定是方法签名

例如,我可能有

CustomerInvoice.Create(Customer customer, int orderNumber);
DespatchNote.Create(Customer customer, int orderNumber, Warehouse warehouse);
PackingLabel.Create(int orderNumber);

... 或其他(努力想出合理的例子)。

OO中是否有一种机制可以指定一组类以这种方式命名的方法?我真的在想有一种方法可以在一组相似的对象中强制执行一致的实现和命名,因此它们对消费者来说更直观。像这样的案例会被认为是对任何此类技术的有效/有值(value)的使用吗?

最佳答案

不,没有用于此的结构。静态方法特别是 无法强制执行任何通用性。如果是一个接口(interface),你可以有类似 Create(object)Create(T) 的东西(对于在通用接口(interface)上定义的一些 T)并采用单个参数来表示德州。

关于c# - 如何以比 C# 中的接口(interface)更宽松的方式定义模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4561796/

相关文章:

c# API 使用 wordnet-什么更好?

c++ - 在 "Modern C++ Design"为什么 Prototype Factory Unit::GetPrototype 必须调用 DoGet Prototype?

c# - 为什么我们有这么多种程序集加载方法?

c# - Exchange server 2010 版本支持对 asp.net 的文件夹搜索

C# XML 序列化和十进制值

c# - 命令模式 : Any point in OOP? 的丑陋体验

c# - 如何用 MVC ActionFilter(或其他东西)替换基本 Controller ?

java - 实例化对象用于显示另一个类中存储的信息

java - 如何使用数组内部的整数作为方法的参数。 (测试员)

perl - 我如何调用祖 parent 的构造函数(因为未定义 parent 的构造函数)?