• blackn1ght@feddit.uk
    link
    fedilink
    English
    arrow-up
    74
    ·
    edit-2
    2 days ago
    public interface ICanTravelThroughTheAir
    {
    
    }
    
    public class Flea : ICanTravelThroughTheAir
    {
    
    }
    
    public class AircraftCarrier
    {
      private readonly List<ICanTravelThroughTheAir> _aircraft = new();
    
      public void AddAircraft(ICanTravelThroughTheAir flyingThing)
      {
        _aircraft.Add(flyingThing);
      }
    }
    
    public class Dog : AircraftCarrier
    {
        public void Woof()
        {
            Console.WriteLine("Bitch I'm an aircraft carrier!");
        }
    }
    
    public static class Program
    {
      public int Main(string[] args)
      {
        var dog = new Dog();
        
        for (var i = 0; i < 10000; i++)
        {
            dog.AddAircraft(new Flea());
        }
    
        dog.Woof();
      }
    }
    
    • ZILtoid1991@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      9 hours ago

      Now someone needs to make it an entity component system!

      Attempt 1:

      public struct Entity {
        bool isDog : 1;
        bool isAircraftCarrier : 1;
        bool isFlea : 1;
        bool canFlyInAir : 1;
        ubyte opt_numOfAircrafts : 4;
        int entityID;
        int opt_parentID;
        static Entity createDog(int entityID) {
          Entity result;
          result.isDog = true;
          result.entityID = entityID;
          return result;
        }
        static Entity createFlea(int entityID) {
          Entity result;
          result.isFlea = true;
          result.canFlyInAir = true;
          result.entityID = entityID;
          return result;
        }
        void addAirCraft(ref Entity aircraft) {
          if (aircraft.canFlyInAir && this.isAircraftCarrier) {
            aircraft.opt_parentID = this.entityID;
            this.opt_numOfAircrafts++;
          }
        }
        void woof() {
          if (isDog) {
            if (isAircraftCarrier) writeln("I'm a motherfucking aircraft carrier");
            else writeln("Woof!");
          }
        }
      }
      
      void main() {
        Entity dog = Entity.createDog(1);
        Entity flea = Entity.createFlea(2);
        dog.woof();
        dog.isAircraftCarrier = true;
        dog.addAirCraft(flea);
        dog.woof();
      }
      
      • Supercrunchy@programming.dev
        link
        fedilink
        arrow-up
        22
        ·
        1 day ago

        And dependency injection!

        Every class needs to use DI for calling other classes, even though we’ll have anyway just a single implementation for all of them, and we won’t be using this flexibility at all (not even for tests where it might be useful for mocking dependencies).