C # Lack of static inheritance - what should I do?

Good, as you probably know, static inheritance is not possible in C #. I understand that, however, I adhere to the development of my program.

I will try to make it as simple as possible. Let's say that our code needs to manage the facilities that represent airplanes at any airport. The requirements are as follows:

  • There are members and methods that are used for all aircraft

  • There are many types of aircraft, each type may have its own additional methods and participants. There can be many instances for each type of aircraft.

  • Each type of aircraft should have a friendly name for this type and more detailed information about this type. For example, a class named F16 will have a static FriendlyName member with the value "Lockheed Martin F-16 Fighting Falcon".

  • Other programmers should be able to add more aircraft, although they should be used to create the same static data on aircraft types.

  • In some kind of graphical interface, the user should see a list of available types (with details such as FriendlyName) and add or remove aircraft instances saved, say, for some XML file.

So, basically, if I could apply inherited classes to implement static members and methods, I would apply plane types to static members like FriendlyName. Unfortunately, I cannot do this.

So what would be the best design for this scenario?

+7
inheritance design c # oop
source share
7 answers

One answer is to decorate each class with attributes (metadata):

[Description("Lockheed Martin F-16 Fighting Falcon")] public class F16 : Aircraft { // ... } 

This DescriptionAttribute is already in the System.ComponentModel .

You can get metadata as follows:

 Type t = typeof(F16); DescriptionAttribute attr = (DescriptionAttribute)Attribute.GetCustomAttribute(t, typeof(DescriptionAttribute)); string description = (attr != null) ? attr.Description : t.Name; 

This will give you a description text from a reference to class F16 .

+8
source share

Why do you need these properties to be static?

 public class Aircraft { protected string AircraftName { get; protected set; } } public class F16 : Aircraft { public F16() { AircraftName="F16 Falcon"; } } 
+4
source share

Do not use static methods. use instance methods instead.

Also, the top abstract can produce an abstract method that returns the specific name of the aircraft.

 public abstract class Aircraft { public abstract string Name { get; } public abstract string FriendlyName { get; } } 
+3
source share

This is the case when you can use the Factory pattern. Instead of importing certain types of aircraft, create a standard IAircraftFactory interface that defines what you need to do for each Factory aircraft. Here you can return descriptions, user interface information, etc. Factory Aircraft is then responsible for creating a specific aircraft. Since your customers must create a custom Factory to expose their aircraft, they are forced to implement the interface and remind (through their members) that they have a contract to execute.

Something like:

 public interface IAircraft { //Aircraft instance details... } public interface IAircraftFactory { //Can include parameters if needed... IAircraft BuildAircraft(); //And other useful meta-data... string GetDescription(); } //In some other Client-provided DLL... public class MyAircraftFactory : IAircraftFactory { IAircraft BuildAircraft() { return new MyAircraft(); } //... } 
+2
source share

Use the enumeration for friendly names and instantiate this type for the friendly name. Require initialization of this item during construction.

+1
source share

@Aaronaught hit a nail on the head with a comment similar to a plugin.

What I did the last time I came across this was to have a β€œDescriptor” type, which is not so expensive to create, and to store metadata in an instance field.

 public class F16Descriptor : AircraftDescriptor { public override string Name { get { return "Lockheed Martin F-16 Fighting Falcon"; } } public override Type AircraftType { get { return typeof(F16); } } } public class F16 : AircraftBase { ... } 
0
source share

An interesting way to solve this problem is to recognize that aircraft types are also an important concept in design and create them as separate classes, instances of which act as aircraft types. This is called a type object type (pdf) , and it allows you to create very flexible constructs.

0
source share

All Articles