示例名称:家庭合唱比赛
示例说明:定义抽象的父亲(AbstractFather)、母亲(Abstractmother)和孩子(AbstractChild)及抽象行为Sing。定义抽象的家庭(AbstractFamily),包含父亲、母亲和孩子行为有初始化成员(ConstructMember),准备(抽象的GetReadyToSing),合唱(Tutti),谢幕(抽象的Thank)。定义抽象的家庭工厂负责创建父亲、母亲和孩子。
定义家庭A(FamilyA)、父亲A(FatherA)、母亲A(MotherA)、和孩子A(ChildA)及家庭工厂A(FactoryA)分别继承自家庭、父亲、母亲、孩子和家庭工厂。
示例类图:
关键部分说明
家庭、父亲、母亲、孩子和家庭工厂
关键在于这个抽象的家庭(AbstractFamily),它的行为合唱(Tutti)包装了基于通过抽象的家庭工厂(AbstractFamilyFactory)创建的家庭成员的行为。
家庭A
示例说明:定义抽象的父亲(AbstractFather)、母亲(Abstractmother)和孩子(AbstractChild)及抽象行为Sing。定义抽象的家庭(AbstractFamily),包含父亲、母亲和孩子行为有初始化成员(ConstructMember),准备(抽象的GetReadyToSing),合唱(Tutti),谢幕(抽象的Thank)。定义抽象的家庭工厂负责创建父亲、母亲和孩子。
定义家庭A(FamilyA)、父亲A(FatherA)、母亲A(MotherA)、和孩子A(ChildA)及家庭工厂A(FactoryA)分别继承自家庭、父亲、母亲、孩子和家庭工厂。
示例类图:
关键部分说明
家庭、父亲、母亲、孩子和家庭工厂
//抽象的家庭
abstract class AbstractFamily
{
//家庭成员
protected AbstractFather farther;
protected Abstractmother mother;
protected AbstractChild child;
//创建家庭成员
public void ConstructMember(AbstractFamilyFactory factory)
{
farther = factory.CreateFather();
mother = factory.CreateMother();
child = factory.CreateChild();
}
//合唱准备工作
abstract public void GetReadyToSing();
//谢幕
abstract public void Thanks();
//合唱
public void Tutti()
{
if (farther != null) farther.Sing();
if (mother != null) mother.Sing();
if (child != null) child.Sing();
}
}
//抽象父亲
abstract class AbstractFather
{
abstract public void Sing();
}
//抽象母亲
abstract class Abstractmother
{
abstract public void Sing();
}
//抽象孩子
abstract class AbstractChild
{
abstract public void Sing();
}
//抽象家庭工厂
abstract class AbstractFamilyFactory
{
abstract public AbstractFather CreateFather();
abstract public Abstractmother CreateMother();
abstract public AbstractChild CreateChild();
}
abstract class AbstractFamily
{
//家庭成员
protected AbstractFather farther;
protected Abstractmother mother;
protected AbstractChild child;
//创建家庭成员
public void ConstructMember(AbstractFamilyFactory factory)
{
farther = factory.CreateFather();
mother = factory.CreateMother();
child = factory.CreateChild();
}
//合唱准备工作
abstract public void GetReadyToSing();
//谢幕
abstract public void Thanks();
//合唱
public void Tutti()
{
if (farther != null) farther.Sing();
if (mother != null) mother.Sing();
if (child != null) child.Sing();
}
}
//抽象父亲
abstract class AbstractFather
{
abstract public void Sing();
}
//抽象母亲
abstract class Abstractmother
{
abstract public void Sing();
}
//抽象孩子
abstract class AbstractChild
{
abstract public void Sing();
}
//抽象家庭工厂
abstract class AbstractFamilyFactory
{
abstract public AbstractFather CreateFather();
abstract public Abstractmother CreateMother();
abstract public AbstractChild CreateChild();
}
关键在于这个抽象的家庭(AbstractFamily),它的行为合唱(Tutti)包装了基于通过抽象的家庭工厂(AbstractFamilyFactory)创建的家庭成员的行为。
家庭A
class FamilyA : AbstractFamily
{
public FactoryA factory = new FactoryA();
public override void GetReadyToSing()
{
Console.WriteLine("我们是幸运家庭A,我们已经做好了上台的准备!");
}
public override void Thanks()
{
Console.WriteLine("我们是幸运家庭A,我们的演出结束,谢谢大家!");
}
}
class FatherA : AbstractFather
{
public override void Sing()
{
Console.WriteLine("我是父亲,我唱男高音!");
}
}
class MotherA : Abstractmother
{
public override void Sing()
{
Console.WriteLine("我是母亲,我唱民歌");
}
}
class ChildA : AbstractChild
{
public override void Sing()
{
Console.WriteLine("我是孩子,我唱儿童歌曲!");
}
}
class FactoryA : AbstractFamilyFactory
{
public override AbstractFather CreateFather()
{
return new FatherA();
}
public override Abstractmother CreateMother()
{
return new MotherA();
}
public override AbstractChild CreateChild()
{
return new ChildA();
}
}
{
public FactoryA factory = new FactoryA();
public override void GetReadyToSing()
{
Console.WriteLine("我们是幸运家庭A,我们已经做好了上台的准备!");
}
public override void Thanks()
{
Console.WriteLine("我们是幸运家庭A,我们的演出结束,谢谢大家!");
}
}
class FatherA : AbstractFather
{
public override void Sing()
{
Console.WriteLine("我是父亲,我唱男高音!");
}
}
class MotherA : Abstractmother
{
public override void Sing()
{
Console.WriteLine("我是母亲,我唱民歌");
}
}
class ChildA : AbstractChild
{
public override void Sing()
{
Console.WriteLine("我是孩子,我唱儿童歌曲!");
}
}
class FactoryA : AbstractFamilyFactory
{
public override AbstractFather CreateFather()
{
return new FatherA();
}
public override Abstractmother CreateMother()
{
return new MotherA();
}
public override AbstractChild CreateChild()
{
return new ChildA();
}
}
通过对对应的抽象类的继承实现了和抽象类相同的创建、组织和表示。
调用及运行结果
FamilyA family = new FamilyA();
family.ConstructMember(family.factory);
family.GetReadyToSing();
family.Tutti();
family.Thanks();
family.ConstructMember(family.factory);
family.GetReadyToSing();
family.Tutti();
family.Thanks();