, . , , ...
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
{
private static HashSet<string> AllowedProperties = new HashSet<string>(
new string [] {
"Color",
"SeedCount"
});
private Dictionary<string, string> Properties = new Dictionary<string, string>();
public string this[string prop]
{
get
{
if (!AllowedProperties.Contains(prop))
{
}
if (Properties.ContainsKey(prop))
{
return this.Properties[prop];
}
else
{
return "0";
}
}
set
{
if (!AllowedProperties.Contains(prop))
{
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;
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;
}
}
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());
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 - : , , . , .