简体   繁体   English

如何实现这个Base / Derived类结构?

[英]How to I implement this Base/Derived class structure?

I'm writing a C# application that reads in a source code file of language X, and populates a data structure with the classes, methods etc. that appear in the source file. 我正在编写一个C#应用程序,它读取语言X的源代码文件,并使用源文件中出现的类,方法等填充数据结构。

After that, using this data structure I just populated, I can call any of these three functions: 之后,使用我刚刚填充的这个数据结构,我可以调用这三个函数中的任何一个:

GenerateCS()
GenerateJava()
GenerateCPP()

Basically, it ports language X to either of those three languages. 基本上,它将语言X移植到这三种语言中的任何一种。

My question is, how can I structure this such that I have one class GenerateCode which functions as a base class and the other generate functions derive from it? 我的问题是,我如何构造这样的,以便我有一个类GenerateCode作为基类,另一个生成函数从它派生? I suppose the particular syntax details of each language would have to reside within the derived classes itself, but what stuff could I abstract to the superclass? 我想每种语言的特定语法细节必须驻留在派生类本身中,但是我可以将哪些东西抽象到超类?

What about: 关于什么:

public enum Language
{
    CS,
    Java,
    CPP
}

public class CS: BaseClass { }
public class Java: BaseClass { }
public class Cpp: BaseClass { }

public class BaseClass
{
    public abstract BaseClass ConvertTo(Language lang);
}

or 要么

public class BaseClass
{
    public abstract FromClass(BaseClass class, Language lang);
}

I would recommend that you start with a structure like this: 我建议你从这样的结构开始:

public class MetaCode
{
    private IList<Fields> fields;
    private IList<Properties> properties;
    private IList<Methods> methods;

    public IList<Fields> Fields
    {
        get { return this.fields; }
    }

    public IList<Properties> Properties
    {
        get { return this.properties; }
    }


    public IList<Methods> Methods
    {
        get { return this.methods; }
    }

    // etc...
}

public interface ISourceReader
{
    MetaCode ReadCode(string sourceCode);
}

public interface ISourceWriter
{
    string WriteCode(MetaCode metaCode);
}

public class CodeConverter
{
    private ISourceReader reader;
    private ISourceWriter writer;

    public CodeConverter(ISourceReader reader, ISourceWriter writer)
    {
        this.reader = reader;
        this.writer = writer;
    }

    public string Convert(string sourceCode)
    {
        MetaCode metaCode = this.reader.ReadCode(sourceCode);
        return this.writer.WriteCode(metaCode);
    }
}

This is just pseudo-code, but you could probably make your interfaces follow the StreamReader/StreamWriter pattern that appears frequently in the .NET framework. 这只是伪代码,但您可以使接口遵循.NET框架中经常出现的StreamReader / StreamWriter模式。

The interfaces allow neat extension points where you can add new source and destination programming languages to your application. 接口允许整齐的扩展点,您可以在其中向应用程序添加新的源和目标编程语言。 The best thing about this approach is that the CodeConverter class knows nothing about the different programming languages that exist. 这种方法最好的事情是CodeConverter类对存在的不同编程语言一无所知。 New ones can be added or removed and it doesn't need to change. 可以添加或删除新的,不需要更改。 Other people can even create new language readers / writers and use them without touching your code / compiled assembly. 其他人甚至可以创建新的语言读者/编写者并使用它们而无需触及您的代码/编译程序集。

To be honest, thinking about this, I dont think there is much functionality that you can abstract out to a base class. 说实话,考虑到这一点,我不认为有很多功能可以抽象到基类。 The details of each language is so specific that a base class is difficult to do correctly. 每种语言的细节都是如此具体,以至于基类很难正确完成。 In any case, I'd always recommend starting out with the interfaces because then you can always create an implementation no matter how obscure / different a programming language is. 无论如何,我总是建议从接口开始,因为无论编程语言有多么模糊/不同,你总是可以创建一个实现。

Perhaps you could create several "helper" base classes that contain some abstracted functionality for the different general styles of programming language there are: 也许你可以创建几个“辅助”基类,它们包含一些抽象功能,用于编程语言的不同通用样式:

public abstract class CLikeSourceReader : ISourceReader
{
    public MetaCode ReadCode(string sourceCode)
    {
        // etc..
    }

    // etc..
}

public abstract class VisualBasicLikeSourceReader : ISourceReader
{
    public MetaCode ReadCode(string sourceCode)
    {
        // etc..
    }

    // etc..
}

public abstract class AssemblyLanguageLikeSourceReader : ISourceReader
{
    public MetaCode ReadCode(string sourceCode)
    {
        // etc..
    }

    // etc..
}

This way, when adding a new language you have the option to inherit from one of these pre-existing base classes, with the option to fall back on the interfaces if none of the them are suitable. 这样,在添加新语言时,您可以选择从这些预先存在的基类之一继承,如果它们都不合适,可以选择回退到接口。

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

相关问题 基类不实现INotifyPropertyChange; 如何在派生类中实现它? - Base class does not implement INotifyPropertyChange; how do I implement it in derived class? 如何使用方法实现基类并强制派生类重写它? - How to Implement a Base Class with a Method and yet force the Derived Class to Override it? 如何在派生1和派生2中而不是基类中访问结果 - How can I access the result in Derived 1 and Derived 2 and not the base class 基于构造函数异常将派生类实现为基础? - Implement Derived Class as Base on Constructor Exception? 如何在派生类中隐藏基类公共属性 - How can I hide a base class public property in the derived class 如何从基类调用派生类方法? - How do I call a derived class method from the base class? 如何实例化基类,然后将其转换为派生类? - How can I instantiate a base class and then convert it to a derived class? 我怎样才能转换Class <Derived> 上课 <Base> ? - How can I convert Class<Derived> to Class<Base>? 如何使用NHibernate在基类上映射派生属性? - How do I map a derived property on a base class using NHibernate? 如何制作接口基属性,然后用派生来实现? - How to make a interface base property, then implement with the derived?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM