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();
}
}
}
抽象工厂实例
最新推荐文章于 2021-07-06 12:16:16 发布