What is the best way to create a scalable class?

what I mean: I basically have a class that has too many properties and functions. To remain functional and understandable, he needs to shrink somehow. But I still need all these properties and methods. Like this:

class Apple

  float seedCount;
  ...
  ...about 25 variables and properties here.
  void Update() <-- a huge method that checks for each property and updates if so

In most cases, a class almost does not need any of these properties. In some cases, you need to be able to grow very selectively and gain a function or lose a function. The only solution I came up with is that I create a bunch of classes and put some properties there. I only initialize this class object when one of these properties is required, otherwise it remains zero.

class Apple

  Seed seed;

Many problems because of this: I constantly have to check every object and every frame. If the seed is not initialized, I do not need to count anything for it. If so, I must. If I decided to add more than one property / function to the Seed class, I need to check each one as well. It is getting harder. Therefore, the problem is that I need granular control over all functions and cannot divide them into larger subclasses. Any form of subclass will contain only a set of properties that need to be checked and updated if they want. I cannot subclass Apple exactly because of the need for such granular control. It would be crazy to create as many classes as there are combinations of properties. My main goal: I need a short code.

+5
8

, .

, Decorator Pattern. , , . ( #, , ), .

Apple Update , base.Update, .

, "".

+4

, , . - , , . , , . .

, -, , .

+1

, : . , . . Seed, , Apple .

, , null objects . , null, .

+1

, . , , ...

Apple: Apple , . Apple , , , .

  • Apple,
    .
     a1. GetSeedCount
     a2....
     .
      b1. GetSkinColor
       2....

.

, , .

, ( , , ). , ( , , , < string, string > . , .

. .

, , , , , , (, ..).

Apple.cs

namespace ConsoleApplication1
{
    using System.Collections.Generic;
    using System.Text;

    public class Apple
    {
        // Define the set of valid properties for all apple objects.
        private static HashSet<string> AllowedProperties = new HashSet<string>(
            new string [] {
                "Color",
                "SeedCount"
            });

        // The main store for all properties
        private Dictionary<string, string> Properties = new Dictionary<string, string>();

        // Indexer for accessing properties
        // Access via the indexer should be restricted to the extension methods!
        // Unfortunately can't enforce this by making it private because then extension methods wouldn't be able to use it as they are now.
        public string this[string prop]
        {
            get
            {
                if (!AllowedProperties.Contains(prop))
                {
                    // throw exception
                }

                if (Properties.ContainsKey(prop))
                {
                    return this.Properties[prop];
                }
                else
                {
                    // TODO throw 'property unitialized' exeception || lookup & return default value for this property || etc.

                    // this return is here just to make the sample runable
                    return "0"; 
                }
            }

            set
            {
                if (!AllowedProperties.Contains(prop))
                {
                    // TODO throw 'invalid property' exception

                    // these assignments are here just to make the sample runable
                    prop = "INVALID";
                    value = "0";
                }

                this.Properties[prop] = value.ToString();
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (var kv in this.Properties)
            {
                sb.AppendFormat("{0}={1}\n", kv.Key, kv.Value);
            }

            return sb.ToString();
        }
    }
}

AppleExtensions.cs

namespace AppleExtensionMethods
{
    using System;
    using ConsoleApplication1;

   // Accessors for Seed Properties
    public static class Seed
    {
        public static float GetSeedCount(this Apple apple)
        {
            return Convert.ToSingle(apple["SeedCount"]);
        }

        public static void SetSeedCount(this Apple apple, string count)
        {
            apple["SeedCount"] = count;
        }
    }

   // Accessors for Skin Properties
    public static class Skin
    {
        public static string GetSkinColor(this Apple apple)
        {
            return apple["Color"];
        }

        public static void SetSkinColor(this Apple apple, string color)
        {
            apple["Color"] = ValidSkinColorOrDefault(apple, color);
        }

        private static string ValidSkinColorOrDefault(this Apple apple, string color)
        {
            switch (color.ToLower())
            {
                case "red":
                    return color;

                case "green":
                    return color;

                default:
                    return "rotten brown";
            }
        }
    }
}

:

Program.cs

namespace ConsoleApplication1
{
    using System;
    using AppleExtensionMethods;

    class Program
    {
        static void Main(string[] args)
        {
            Apple apple = new Apple();

            apple.SetSkinColor("Red");
            apple.SetSeedCount("8");

            Console.WriteLine("My apple is {0} and has {1} seed(s)\r\n", apple.GetSkinColor(), apple.GetSeedCount());

            apple.SetSkinColor("green");
            apple.SetSeedCount("4");

            Console.WriteLine("Now my apple is {0} and has {1} seed(s)\r\n", apple.GetSkinColor(), apple.GetSeedCount());

            apple.SetSkinColor("blue");
            apple.SetSeedCount("0");

            Console.WriteLine("Now my apple is {0} and has {1} seed(s)\r\n", apple.GetSkinColor(), apple.GetSeedCount());

            apple.SetSkinColor("yellow");
            apple.SetSeedCount("15");

            Console.WriteLine(apple.ToString());

            // Unfortunatly there is nothing stopping users of the class from doing something like that shown below.
            // This would be bad because it bypasses any behavior that you have defined in the get/set functions defined
            // as extension methods.
            // One thing in your favor here is it is inconvenient for user of the class to find the valid property names as
            // they'd have to go look at the apple class. It much easier (from a lazy programmer standpoint) to use the
            // extension methods as they show up in intellisense :) However, relying on lazy programming does not a contract make.
            // There would have to be an agreed upon contract at the user of the class level that states, 
            //  "I will never use the indexer and always use the extension methods!"
            apple["Color"] = "don't panic";
            apple["SeedCount"] = "on second thought...";

            Console.WriteLine(apple.ToString());
        }
    }
}

7/11 (, ):)
, , , :

" , BasicBroodmother " "

, - :

BasicBroodmother bm = monster as BasicBroodmother;
if (bm != null)
{
    bm.Eat();
}

( , ), , , . broodmother, /, . , , /, , , , /.

, (, , , , ) .

[edit 7/13]
( ), , .

:

  • Broodfather.cs - , , Broodfathers.
  • BasicBroodFather.cs - , Broodfather.
  • BroodfatherDecorator.cs - , Broodfather.
  • MagicalBroodfather.cs - / Broodfather ""
  • BloodthirstyBroodfather.cs - / Broodfather ""
  • program.cs - : , , . , .
+1

Decorator , , , , . , Decorator . . , .

. Broodfather . : IBroodfatherMagicAbility IBroodfatherBloodthirstAbility. , , , (. ).

, "" , .

  • IBroodfatherBloodthirstAbility.cs - , " ".
  • BroodfatherNonBloodThristy.cs - , .
  • BroodfatherBloodThristy.cs - , .

  • IBroodfatherMagicAbility.cs - , " ".

  • BroodfatherNonMagical.cs - , .
  • BroodfatherMagical.cs - , .

  • BasicBroodfather.cs - , , , , magic bloodthrist , - .

  • Program.cs - , .

, , , , .

+1

, , ?

Seed Apple, , Seed, Seed?

Apple, .

, , ...

, :

0

: .

:

  • .
  • Perl.
  • .
-4

All Articles