Abstract Factory模式示例

本文通过一个具体的例子介绍了抽象工厂模式的实现方式。该模式提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。文中展示了如何定义抽象产品接口及具体产品的实现,并通过两个不同的工厂来创建这些产品。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考了张逸的书,对抽象工厂这个创建型模式进行了复习.

关于该模式的一些介绍和应用场合,我就不再多说了,有很多人的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();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值