抽象工厂模式实例

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

//抽象工厂
namespace AbstractFactoryConsole
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"'猫和老鼠'动画片中喜欢哪个? 1.猫 2.老鼠");
            
string s = Console.ReadLine();


            
if (s == "1" || s == "" || s.ToUpper() == "CAT" || s.ToUpper() == "MAO")
            
{
                AnimalFactory factory 
= new CatFactory();
                AnimalWorld world 
= new AnimalWorld(factory);
                world.Count();
            }

            
else if (s == "2" || s == "老鼠" || s.ToUpper() == "MOUSE" || s.ToUpper() == "LAOSHU")
            
{
                AnimalFactory factory 
= new MouseFactory();
                AnimalWorld world 
= new AnimalWorld(factory);
                world.Count();
            }


            Console.Write(
"please press any key to continue");
            Console.ReadLine();
        }

    }


    
abstract class AnimalFactory
    
{
        
public abstract Head CreateHead();
        
public abstract Foot CreateFoot();
    }


    
class CatFactory : AnimalFactory
    
{
        
public override Head CreateHead()
        
{
            
return new CatHead();
        }


        
public override Foot CreateFoot()
        
{
            
return new CatFoot();
        }

    }


    
class MouseFactory : AnimalFactory
    
{
        
public override Head CreateHead()
        
{
            
return new MouseHead();
        }


        
public override Foot CreateFoot()
        
{
            
return new MouseFoot();
        }

    }


    
abstract class Head
    
{
        
public abstract string GetHead();
    }


    
abstract class Foot
    
{
        
public abstract void Count(Head head);
    }


    
class CatHead : Head
    
{
        
public override string GetHead()
        
{
            
return "我是一只猫";
        }

    }


    
class CatFoot : Foot
    
{
        
public override void Count(Head head)
        
{
            Console.WriteLine(
"请再输入'猫'!");
            
string s = Console.ReadLine();

            
if (s == "" || s.ToUpper() == "CAT" || s.ToUpper() == "MAO")
            
{
                Console.WriteLine(
"喵喵喵!" + head.GetHead());
            }

            
else
            
{
                Console.WriteLine(
"错啦错啦!");
            }

        }

    }


    
class MouseHead : Head
    
{
        
public override string GetHead()
        
{
            
return "我是一只老鼠";
        }

    }


    
class MouseFoot : Foot
    
{
        
public override void Count(Head head)
        
{
            Console.WriteLine(
"请再输入'老鼠'!");
            
string s = Console.ReadLine();

            
if (s == "老鼠" || s.ToUpper() == "MOUSE" || s.ToUpper() == "LAOSHU")
            
{
                Console.WriteLine(
"吱吱吱!" + head.GetHead());
            }

            
else
            
{
                Console.WriteLine(
"错啦错啦,唉!");
            }

        }

    }


    
class AnimalWorld
    
{
        
private Head head;
        
private Foot foot;

        
public AnimalWorld(AnimalFactory factory)
        
{
            foot 
= factory.CreateFoot();
            head 
= factory.CreateHead();
        }


        
public void Count()
        
{
            
            foot.Count(head);
        }

    }

}

意图:提供一个创建一系列相关或互相依赖对象的接口,而无需指定它们具体的类

适用性:

1.一个系统要独立于它的产品的创建、组合和表示时。

2.一个系统要由多个产品系列中的一个来配置时。

3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。

4.当你提供一个产品类库,而只想显示他们的接口而不是实现时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值