参考了张逸的书,对抽象工厂这个创建型模式进行了复习.
关于该模式的一些介绍和应用场合,我就不再多说了,有很多人的blog 和书上都有介绍.
下面把项目的 架构和实现给大家。
架构:
实现代码:
IReportFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractFactory
{
public interface IReportFactory
{
IReportObject CreateReportObject();
IReportFormat CreateReportFormat();
IReportProcessor CreateReportProcessor();
}
}
IReportFormat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractFactory
{
public interface IReportFormat
{
}
}
IReportObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractFactory
{
public interface IReportObject
{
}
}
IReportProcessor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractFactory
{
public interface IReportProcessor
{
}
}
CellReportFormat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace CellReportDetail
{
public class CellReportFormat:IReportFormat
{
public CellReportFormat()
{
Console.WriteLine("this is CellReportFormat class.");
}
}
}
CellReportObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory ;
namespace CellReportDetail
{
public class CellReportObject:IReportObject
{
public CellReportObject()
{
Console.WriteLine("this is CellReportObject class.");
}
}
}
CellReportProcessor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace CellReportDetail
{
public class CellReportProcessor:IReportProcessor
{
public CellReportProcessor()
{
Console.WriteLine("this is CellReportProcessor class.");
}
}
}
CrystalReportFormat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace CrystalReportDetail
{
public class CrystalReportFormat:IReportFormat
{
public CrystalReportFormat()
{
Console.WriteLine("this is CrystalReportFormat class.");
}
}
}
CrystalReportObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace CrystalReportDetail
{
public class CrystalReportObject:IReportObject
{
public CrystalReportObject()
{
Console.WriteLine("this is CrystalReportObject class.");
}
}
}
CrystalReportProcessor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace CrystalReportDetail
{
public class CrystalReportProcessor:IReportProcessor
{
public CrystalReportProcessor()
{
Console.WriteLine("this is CrystalReportProcessor class.");
}
}
}
CellReportFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
using CellReportDetail;
namespace FactoryDetail
{
public class CellReportFactory:IReportFactory
{
public IReportObject CreateReportObject()
{
return new CellReportObject();
}
public IReportFormat CreateReportFormat()
{
return new CellReportFormat();
}
public IReportProcessor CreateReportProcessor()
{
return new CellReportProcessor();
}
}
}
CrystalReportFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
using CrystalReportDetail;
namespace FactoryDetail
{
public class CrystalReportFactory:IReportFactory
{
public IReportObject CreateReportObject()
{
return new CrystalReportObject() ;
}
public IReportFormat CreateReportFormat()
{
return new CrystalReportFormat();
}
public IReportProcessor CreateReportProcessor()
{
return new CrystalReportProcessor();
}
}
}
ReportUtil.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory ;
using FactoryDetail ;
namespace TestAbstractFactory
{
class ReportUtil
{
public static IReportFactory reportFactory = new CellReportFactory();
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFactory;
namespace TestAbstractFactory
{
class Program
{
static void Main(string[] args)
{
IReportObject reportObject = ReportUtil.reportFactory.CreateReportObject();
}
}
}