Code
/*题目:
"假设这里是原始社会,有石头,2块石头互磨可以变成石刀,石刀可以去砍木头,木头被砍成木材,木材可以组成椅子,"
"请你用oo的思想把这些事物和他们之间的关系表达出来,但是要考虑到以后可能我会增加以下几点:"
"1.有可能我还想让石刀去砍椅子,把椅子砍成木材,"
"2.可能我还想让石头增加关系,例如互相砸,互相摔,而不只是磨,也可以变成石刀"
"3.也许我又要多添一百种不同的事物,再多添120种不同的关系"
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 原始社会 {
class Program {
static void Main(string[] args) {
Stone s1 = new Stone();
Stone s2 = new Stone();
//GrindStratety gs = new GrindStratety();
//StoneSword sword = CreateSwordFactory.CreateSword(s1, s2,gs);
StoneSword sword = CreateSwordFactory.CreateSword(s1, s2, "GrindStratety");
Tree t1 = new Tree();
Wood wood = sword.Chop(t1);
Chair chair = CreateChairFactory.CreateChair(wood);
}
}
class Stone { public Stone() { Console.WriteLine("创建了一块石头。"); } }
class StoneSword {
public Wood Chop(Wood wood) {
Console.WriteLine("将 {0} 砍成木材。", wood.GetType());
return new Wood();
}
}
class Wood { }
class Tree : Wood { }
class Chair { }
interface ICreateSwordStrategy {
StoneSword CreateSword(Stone sA, Stone sB);
}
#region 造刀工厂,系统的主要变化点 【 工厂模式+策略模式+反射】
sealed class CreateSwordFactory {
public static StoneSword CreateSword(Stone sA, Stone sB, ICreateSwordStrategy strategy) {
Console.WriteLine("用 {0} 将两块石头变成刀。", strategy.GetType());
return strategy.CreateSword(sA, sB);
}
public static StoneSword CreateSword(Stone sA, Stone sB, string strategyName) {
Type type = Type.GetType("原始社会." + strategyName);
if (type == null) {
return null;
}
ICreateSwordStrategy strategy = (ICreateSwordStrategy)Activator.CreateInstance(type);
Console.WriteLine("用 {0} 将两块石头变成刀。", strategy.GetType());
return strategy.CreateSword(sA, sB);
}
}
//互磨策略
sealed class GrindStratety : ICreateSwordStrategy {
public StoneSword CreateSword(Stone sA, Stone sB) {
Console.WriteLine("互磨方式制造了一把刀。");
return new StoneSword();
}
}
//摔的策略
sealed class ThrowStratety : ICreateSwordStrategy {
public StoneSword CreateSword(Stone sA, Stone sB) {
Console.WriteLine("用摔的方式造了一把刀。");
return new StoneSword();
}
}
#endregion
sealed class CreateChairFactory {
public static Chair CreateChair(Wood wood) {
Console.WriteLine("用木材做了一只椅子。");
return new Chair();
}
}
}
/*题目:
"假设这里是原始社会,有石头,2块石头互磨可以变成石刀,石刀可以去砍木头,木头被砍成木材,木材可以组成椅子,"
"请你用oo的思想把这些事物和他们之间的关系表达出来,但是要考虑到以后可能我会增加以下几点:"
"1.有可能我还想让石刀去砍椅子,把椅子砍成木材,"
"2.可能我还想让石头增加关系,例如互相砸,互相摔,而不只是磨,也可以变成石刀"
"3.也许我又要多添一百种不同的事物,再多添120种不同的关系"
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 原始社会 {
class Program {
static void Main(string[] args) {
Stone s1 = new Stone();
Stone s2 = new Stone();
//GrindStratety gs = new GrindStratety();
//StoneSword sword = CreateSwordFactory.CreateSword(s1, s2,gs);
StoneSword sword = CreateSwordFactory.CreateSword(s1, s2, "GrindStratety");
Tree t1 = new Tree();
Wood wood = sword.Chop(t1);
Chair chair = CreateChairFactory.CreateChair(wood);
}
}
class Stone { public Stone() { Console.WriteLine("创建了一块石头。"); } }
class StoneSword {
public Wood Chop(Wood wood) {
Console.WriteLine("将 {0} 砍成木材。", wood.GetType());
return new Wood();
}
}
class Wood { }
class Tree : Wood { }
class Chair { }
interface ICreateSwordStrategy {
StoneSword CreateSword(Stone sA, Stone sB);
}
#region 造刀工厂,系统的主要变化点 【 工厂模式+策略模式+反射】
sealed class CreateSwordFactory {
public static StoneSword CreateSword(Stone sA, Stone sB, ICreateSwordStrategy strategy) {
Console.WriteLine("用 {0} 将两块石头变成刀。", strategy.GetType());
return strategy.CreateSword(sA, sB);
}
public static StoneSword CreateSword(Stone sA, Stone sB, string strategyName) {
Type type = Type.GetType("原始社会." + strategyName);
if (type == null) {
return null;
}
ICreateSwordStrategy strategy = (ICreateSwordStrategy)Activator.CreateInstance(type);
Console.WriteLine("用 {0} 将两块石头变成刀。", strategy.GetType());
return strategy.CreateSword(sA, sB);
}
}
//互磨策略
sealed class GrindStratety : ICreateSwordStrategy {
public StoneSword CreateSword(Stone sA, Stone sB) {
Console.WriteLine("互磨方式制造了一把刀。");
return new StoneSword();
}
}
//摔的策略
sealed class ThrowStratety : ICreateSwordStrategy {
public StoneSword CreateSword(Stone sA, Stone sB) {
Console.WriteLine("用摔的方式造了一把刀。");
return new StoneSword();
}
}
#endregion
sealed class CreateChairFactory {
public static Chair CreateChair(Wood wood) {
Console.WriteLine("用木材做了一只椅子。");
return new Chair();
}
}
}