IContainer 介面

定義

提供容器的功能。 容器是邏輯上包含零個或多個元件的物件。

public interface class IContainer : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IContainer : IDisposable
public interface IContainer : IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type IContainer = interface
    interface IDisposable
type IContainer = interface
    interface IDisposable
Public Interface IContainer
Implements IDisposable
衍生
屬性
實作

範例

下列程式碼範例示範如何實作 IContainer 介面。

//This code segment implements the IContainer interface.  The code segment
//containing the implementation of ISite and IComponent can be found in the documentation
//for those interfaces.

//Implement the LibraryContainer using the IContainer interface.

class LibraryContainer : IContainer
{
    readonly ArrayList m_bookList = [];

    public virtual void Add(IComponent book) =>
        //The book will be added without creation of the ISite object.
        m_bookList.Add(book);

    public virtual void Add(IComponent book, string ISNDNNum)
    {
        for (int i = 0; i < m_bookList.Count; ++i)
        {
            IComponent curObj = (IComponent)m_bookList[i];
            if (curObj.Site != null)
            {
                if (curObj.Site.Name.Equals(ISNDNNum))
                {
                    throw new ArgumentException("The ISBN number already exists in the container");
                }
            }
        }

        book.Site = new ISBNSite(this, book) { Name = ISNDNNum };
        m_bookList.Add(book);
    }

    public virtual void Remove(IComponent book)
    {
        for (int i = 0; i < m_bookList.Count; ++i)
        {
            if (book.Equals(m_bookList[i]))
            {
                m_bookList.RemoveAt(i);
                break;
            }
        }
    }

    public ComponentCollection Components
    {
        get
        {
            IComponent[] datalist = new BookComponent[m_bookList.Count];
            m_bookList.CopyTo(datalist);
            return new ComponentCollection(datalist);
        }
    }

    public virtual void Dispose()
    {
        for (int i = 0; i < m_bookList.Count; ++i)
        {
            IComponent curObj = (IComponent)m_bookList[i];
            curObj.Dispose();
        }

        m_bookList.Clear();
    }

    static void Main()
    {
        LibraryContainer cntrExmpl = new();

        try
        {
            BookComponent book1 = new("Wizard's First Rule", "Terry Gooodkind");
            cntrExmpl.Add(book1, "0812548051");
            BookComponent book2 = new("Stone of Tears", "Terry Gooodkind");
            cntrExmpl.Add(book2, "0812548094");
            BookComponent book3 = new("Blood of the Fold", "Terry Gooodkind");
            cntrExmpl.Add(book3, "0812551478");
            BookComponent book4 = new("The Soul of the Fire", "Terry Gooodkind");
            //This will generate exception because the ISBN already exists in the container.
            cntrExmpl.Add(book4, "0812551478");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine("Unable to add books: " + e.Message);
        }

        ComponentCollection datalist = cntrExmpl.Components;
        IEnumerator denum = datalist.GetEnumerator();

        while (denum.MoveNext())
        {
            BookComponent cmp = (BookComponent)denum.Current;
            Console.WriteLine("Book Title: " + cmp.Title);
            Console.WriteLine("Book Author: " + cmp.Author);
            Console.WriteLine("Book ISBN: " + cmp.Site.Name);
        }
    }
}
    'This code segment implements the IContainer interface.  The code segment 
    'containing the implementation of ISite and IComponent can be found in the documentation
    'for those interfaces.
    
    'Implement the LibraryContainer using the IContainer interface.

Class LibraryContainer
    Implements IContainer
    Private m_bookList As ArrayList

    Public Sub New()
        m_bookList = New ArrayList()
    End Sub

    Public Sub Add(ByVal book As IComponent) Implements IContainer.Add
        'The book will be added without creation of the ISite object.
        m_bookList.Add(book)
    End Sub

    Public Sub Add(ByVal book As IComponent, ByVal ISNDNNum As String) Implements IContainer.Add

        Dim i As Integer
        Dim curObj As IComponent

        For i = 0 To m_bookList.Count - 1
            curObj = CType(m_bookList(i), IComponent)
            If curObj.Site IsNot Nothing Then
                If (curObj.Site.Name.Equals(ISNDNNum)) Then
                    Throw New ArgumentException("The ISBN number already exists in the container")
                End If
            End If
        Next i

        Dim data As ISBNSite = New ISBNSite(Me, book)
        data.Name = ISNDNNum
        book.Site = data
        m_bookList.Add(book)

    End Sub

    Public Sub Remove(ByVal book As IComponent) Implements IContainer.Remove
        Dim i As Integer
        Dim curComp As BookComponent = CType(book, BookComponent)

        For i = 0 To m_bookList.Count - 1
            If (curComp.Equals(m_bookList(i)) = True) Then
                m_bookList.RemoveAt(i)
                Exit For
            End If
        Next i
    End Sub


    Public ReadOnly Property Components() As ComponentCollection Implements IContainer.Components
        Get
            Dim datalist(m_bookList.Count - 1) As IComponent
            
            m_bookList.CopyTo(datalist)
            Return New ComponentCollection(datalist)
        End Get
    End Property

    Public Overridable Sub Dispose() Implements IDisposable.Dispose
        Dim i As Integer
        For i = 0 To m_bookList.Count - 1
            Dim curObj As IComponent = CType(m_bookList(i), IComponent)
            curObj.Dispose()
        Next i

        m_bookList.Clear()
    End Sub

    Public Shared Sub Main()
        Dim cntrExmpl As LibraryContainer = New LibraryContainer()

        Try
            Dim book1 As BookComponent = New BookComponent("Wizard's First Rule", "Terry Gooodkind")
            cntrExmpl.Add(book1, "0812548051")
            Dim book2 As BookComponent = New BookComponent("Stone of Tears", "Terry Gooodkind")
            cntrExmpl.Add(book2, "0812548094")
            Dim book3 As BookComponent = New BookComponent("Blood of the Fold", "Terry Gooodkind")
            cntrExmpl.Add(book3, "0812551478")
            Dim book4 As BookComponent = New BookComponent("The Soul of the Fire", "Terry Gooodkind")
            'This will generate an exception, because the ISBN already exists in the container.
            cntrExmpl.Add(book4, "0812551478")
        Catch e As ArgumentException
            Console.WriteLine("Unable to add books: " + e.Message)
        End Try

        Dim datalist As ComponentCollection = cntrExmpl.Components
        Dim denum As IEnumerator = datalist.GetEnumerator()

        While (denum.MoveNext())
            Dim cmp As BookComponent = CType(denum.Current, BookComponent)
            Console.WriteLine("Book Title: " + cmp.Title)
            Console.WriteLine("Book Author: " + cmp.Author)
            Console.WriteLine("Book ISBN: " + cmp.Site.Name)
        End While
    End Sub
End Class

備註

容器是封裝並追蹤零個或多個元件的物件。 在此語境中,「收容」指的是邏輯上的「收容」,而非視覺上的「收容」。 你可以在各種情境中使用元件和容器,包括視覺化與非視覺化的情境。

給實施者的注意事項

要成為容器,類別必須實作 IContainer 介面,該介面支援新增、移除與擷取元件的方法。

屬性

名稱 Description
Components

取得所有元件。IContainer

方法

名稱 Description
Add(IComponent, String)

在清單末尾將指定的 IComponentIContainer 加入,並為元件指派名稱。

Add(IComponent)

將指定的 IComponent 資料加到 IContainer 清單末尾。

Dispose()

執行與釋放、釋放或重置未管理資源相關的應用程式定義任務。

(繼承來源 IDisposable)
Remove(IComponent)

從 中移除一個元件。IContainer

適用於

另請參閱