抽象工厂模式

1.抽象工厂模式:

提供一个接口,让该接口负责创建一系列相关或者相互依赖的对象,无需指定它们具体的类。

2.抽象工厂模式应用场景:

一系列相互依赖的对象的创建工作;同时,由于需求变化,往往存在更多系列的对象的创建工作。

3.举例

例1

ContractedBlock.gif ExpandedBlockStart.gif AbstractFactory.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    
public interface IFruit
    {
    }
    
public class Orange : IFruit
    {
        
public Orange()
        {
            Console.WriteLine(
"Orange is got.");
        }
    }
    
public class Apple : IFruit
    {
        
public Apple()
        {
            Console.WriteLine(
"Apple is got.");
        }
    }

    
public class FruitFactory
    {     
        
public IFruit MakeFruit(string name)
        {
            
switch (name)
            {
                
case "Orange":
                    
return new Orange();
                
case "Apple":
                    
return new Apple();
                
default:
                    
return null;
            }
        }
    }
    
    
class Program
    {
        
static void Main(string[] args)
        {
            
string fruitName = Console.ReadLine();
            IFruit MyFruit;
            FruitFactory MyFruitFactory 
= new FruitFactory();
            MyFruit 
= MyFruitFactory.MakeFruit(fruitName);
        }
    }
}
例2
ContractedBlock.gif ExpandedBlockStart.gif AbstractFactory_2.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory_2
{
    
//interface
    public interface IProduct
    {
        
string Hello();
    }
    
public interface IFruit:IProduct
    {
    }
    
public interface ICloth:IProduct
    {
    }

    
//class
    public class Product : IProduct
    {
        
public string Hello()
        {
            
return "Hello!";
        }
    }
    
public class Fruit : IFruit
    {
        
public string Hello()
        {
            
return "You like fruit.Fruit is very delicious.";
        }
    }
    
public class Orange : Fruit
    {
        
string price = "Orange price is $1.1";
        
public  Orange()
        {
            Console.WriteLine(price);
        }
    }
    
public class Apple : Fruit
    {
        
string price = "Apple price is $2.1";
        
public Apple()
        {
            Console.WriteLine(price);
        }
    }
    
public class Cloth : ICloth
    {
        
public string Hello()
        {
            
return "You like cloth.Cloth is very beautiful.";
        }
    }
    
public class Shoe : Cloth
    {
        
string price = "Shoe price is $3.1";
        
public Shoe()
        {
            Console.WriteLine(price);
        }
    }
    
public class Trouse : Cloth
    {
        
string price = "Trouse price is $4.1";
        
public Trouse()
        {
            Console.WriteLine(price);
        }
    }

    
//AbstractFactory
    public class ProductFactory
    {
        
public IProduct MakeProduct(string name)
        {
            
switch (name)
            {
                
case "Orange":
                    
return new Orange();
                
case "Apple":
                    
return new Apple();
                
case "Shoe":
                    
return new Shoe();
                
case "Trouse":
                    
return new Trouse();
                
default:
                    
return new Product();
            }
        }
    }
    
    
class Program
    {
        
static void Main(string[] args)
        {
            
string s = Console.ReadLine();
            IProduct myProduct;
            ProductFactory myProductFactory 
= new ProductFactory();
            myProduct 
= myProductFactory.MakeProduct(s);
            Console.WriteLine(myProduct.Hello());
            Console.ReadLine();
        }
    }
}

转载于:https://www.cnblogs.com/HelloCG/archive/2008/12/31/1366344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值