Abstract Factory模式示例

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

关于该模式的一些介绍和应用场合,我就不再多说了,有很多人的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、付费专栏及课程。

余额充值