设计模式示例三 Abstract Factory(抽象工厂)

本文介绍了一种使用抽象类和工厂模式实现家庭合唱比赛的设计方案。通过定义抽象的家庭、成员角色及行为,利用家庭工厂创建具体家庭成员,实现了不同家庭合唱场景的灵活构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

示例名称:家庭合唱比赛
示例说明:定义抽象的父亲(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();
}

关键在于这个抽象的家庭(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();
    }

}

通过对对应的抽象类的继承实现了和抽象类相同的创建、组织和表示。

调用及运行结果

 

FamilyA family = new FamilyA();
        family.ConstructMember(family.factory);
        family.GetReadyToSing();
        family.Tutti();
        family.Thanks();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值