抽象工厂模式有点疑问?
比如总工厂类:using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryMode
{
/// <summary>
/// 总工厂
/// </summary>
public abstract class AbstractFactory
{
public abstract AbstractUser CreateUser();
public abstract AbstractBook CreateBook();
}
}
子工厂类:
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryMode
{
/// <summary>
/// 一个具体的工厂
/// </summary>
public class ConcreteFactory : AbstractFactory
{
/// <summary>
/// 子工厂,用来创造用户
/// </summary>
/// <returns></returns>
public override AbstractUser CreateUser()
{
//具体的产物
return new User();
}
/// <summary>
/// 子工厂,用来创造书籍
/// </summary>
/// <returns></returns>
public override AbstractBook CreateBook()
{
//具体的产物
return new Book() ;
}
}
}
.........................................
最后调用
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryMode{
class Program
{
static void Main(string[] args)
{
//得到一个子工厂
AbstractFactory factory = new ConcreteFactory(); //这里干嘛不 ConcreteFactory factory = new ConcreteFactory(),这样也可以吧,这样感觉抽象工厂AbstractFactor没用了?
//创造用户
AbstractUser user = factory.CreateUser();
user.CreateUser();
//创造书籍
AbstractBook book = factory.CreateBook();
book.CreateBook();
}
}
}