码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂

时间:2014-05-22 16:29:48      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:class   c   ext   a   int   get   

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

namespace 抽象工厂
{
class Program
{
static void Main(string[] args)
{
//IQueryable<Person>
/////List<Person> ls = new List<Person>();
//IList<Person>

}
}

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}

 

/*其实工厂和抽象工厂没有多大区别,只不过是抽象工厂生产的商品是多个而已
通俗的说,就是抽象工厂模式的具体工厂里面会有多个实例具体对象的方法
更直观的就是,抽象工厂模式每个工厂一次造多个玩意,而工厂模式的每个工厂只造一个玩意*/
public abstract class Product
{
public abstract void work();
}
public class ProductA : Product
{
public override void work()
{
Console.Write("ProductA");
}
}
public class ProductB : Product
{
public override void work()
{
Console.Write("ProductB");
}
}
public abstract class Car
{
public abstract void Build();
}
public class CarA : Car
{
public override void Build()
{
Console.Write("Build CarA");
}
}
public class CarB : Car
{
public override void Build()
{
Console.Write("Build CarB");
}
}
public abstract class AbstractFactory
{
//工厂生产多个对象
public abstract Product newproduct();
public abstract Car newCar();
}
public class AbstractFactoryA : AbstractFactory
{
public override Product newproduct()
{
return new ProductA();//子类里面实现具体的实例化
}
public override Car newCar()
{
return new CarA();
}
}
public class AbstractFactoryB : AbstractFactory
{
public override Product newproduct()
{
return new ProductB();//子类里面实现具体的实例化
}
public override Car newCar()
{
return new CarB();
}
}

public class AbstractFactoryClient
{
public static void GetFactoryMethod()
{
AbstractFactory factorysubA = new AbstractFactoryA();
AbstractFactory factorysubB = new AbstractFactoryA();

Product pruductA = factorysubA.newproduct();
Product pruductB = factorysubB.newproduct();
Car factorycarA = factorysubA.newCar();

factorycarA.Build();

pruductA.work();
pruductB.work();
}
}

 

}

抽象工厂,布布扣,bubuko.com

抽象工厂

标签:class   c   ext   a   int   get   

原文地址:http://www.cnblogs.com/sumg/p/3742753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!