抽象工厂实例

using System;
using System.Collections.Generic;
using System.Text;

namespace AnimalInterface {
	//接口 动物颜色
	interface IAnimalColor {
		void color();
	}
	//鸟
	class BirdColor : IAnimalColor {
		#region IAnimalColor 成员

		public void color() {
			Console.WriteLine("一只红色的小鸟!");
		}

		#endregion
	}
	//猴子
	class MonkeyColor : IAnimalColor {
		#region IAnimalColor 成员

		public void color() {
			Console.WriteLine("一只黑色的猴子!");
		}

		#endregion
	}
	//蛇
	class SnakeColor : IAnimalColor {
		#region IAnimalColor 成员

		public void color() {
			Console.WriteLine("一只花的蟒蛇!");
		}

		#endregion
	}
}


using System;
using System.Collections.Generic;
using System.Text;

namespace AnimalInterface {
	//接口 动物功能
	interface IAnimalFunction {
		void Function();
	}
	//鸟
	class BirdFunction : IAnimalFunction {
		#region IAnimalFunction 成员

		public void Function() {
			Console.WriteLine("小鸟唱歌!");
		}

		#endregion
	}
	//猴子
	class MonkeyFunction : IAnimalFunction {
		#region IAnimalFunction 成员

		public void Function() {
			Console.WriteLine("猴子上树!");
		}

		#endregion
	}
	//蛇
	class SnakeFunction : IAnimalFunction {
		#region IAnimalFunction 成员

		public void Function() {
			Console.WriteLine("蟒蛇吃人!");
		}

		#endregion
	}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace AnimalInterface {
	//抽象工厂
	abstract class AnimalAbstractFactory {
		//放回不同的工厂对象
		public static AnimalAbstractFactory ChooiceFactory(string type) {
			AnimalAbstractFactory factory = null;
			switch(type) {
				case "bird":
					factory = new BirdFactory();
					break;
				case "monkey":
					factory = new MonkeyFactory();
					break;
				case "snake":
					factory = new SnakeFactory();
					break;
			}
			return factory;
		}
		//抽象产品
		public abstract IAnimalColor CreateAnimalColor();
		//抽象产品
		public abstract IAnimalFunction CreateAnimalFunction();
	}
	//实体工厂鸟
	class BirdFactory : AnimalAbstractFactory {
		//鸟颜色
		public override IAnimalColor CreateAnimalColor() {
			return new BirdColor();
		}
		//鸟功能
		public override IAnimalFunction CreateAnimalFunction() {
			return new BirdFunction();
		}
	}
	//实体工厂猴子
	class MonkeyFactory : AnimalAbstractFactory {
		//猴子颜色
		public override IAnimalColor CreateAnimalColor() {
			return new MonkeyColor();
		}
		//猴子功能
		public override IAnimalFunction CreateAnimalFunction() {
			return new MonkeyFunction();
		}
	}
	//实体工厂蛇
	class SnakeFactory : AnimalAbstractFactory {
		//蛇颜色
		public override IAnimalColor CreateAnimalColor() {
			return new SnakeColor();
		}
		//蛇功能
		public override IAnimalFunction CreateAnimalFunction() {
			return new SnakeFunction();
		}
	}
}

using System;
using System.Collections.Generic;
using System.Text;

namespace AnimalInterface {
	class Program {
		static void Main(string[] args) {
			string type = Console.ReadLine();
			AnimalAbstractFactory factory = AnimalAbstractFactory.ChooiceFactory(type);
			IAnimalColor color = factory.CreateAnimalColor();
			IAnimalFunction function = factory.CreateAnimalFunction();
			color.color();
			function.Function();
			Console.ReadLine();
		}
	}
}

 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值