Abstract Factory模式樣本

來源:互聯網
上載者:User

參考了張逸的書,對抽象工廠這個建立型模式進行了複習.

關於該模式的一些介紹和應用場合,我就不再多說了,有很多人的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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.