Generic parameter interface : Generic Interface « Generics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Generics » Generic InterfaceScreenshots 
Generic parameter interface
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public interface IPresentation {
    string Title {
        get;
    }
    string Content {
        get;
    }
}

public class Presentation : IPresentation {
    private string title;
    public string Title {
        get {
            return title;
        }
    }

    private string content;
    public string Content {
        get {
            return content;
        }
    }


    public Presentation(string title, string content) {
        this.title = title;
        this.content = content;
    }
}

public class ProcessPresentations<TPresentation, TPresentationManager>
    where TPresentation : IPresentation
    where TPresentationManager : IPresentationManager<TPresentation> {
    public static void Start(TPresentationManager dm) {
        new Thread(new ProcessPresentations<TPresentation, TPresentationManager>(dm).Run).Start();
    }

    protected ProcessPresentations(TPresentationManager dm) {
        documentManager = dm;
    }

    private TPresentationManager documentManager;

    protected void Run() {
        while (true) {
            if (documentManager.IsPresentationAvailable) {
                TPresentation doc = documentManager.GetPresentation();
                Console.WriteLine("Processing document {0}", doc.Title);
            }
            Thread.Sleep(20);
        }
    }
}

public interface IPresentationManager<T> {
    void AddPresentation(T doc);
    T GetPresentation();
    bool IsPresentationAvailable {
        get;
    }
}

public class PresentationManager<T> : IPresentationManager<T> {
    private Queue<T> documentQueue = new Queue<T>();

    public void AddPresentation(T doc) {
        lock (this) {
            documentQueue.Enqueue(doc);
        }
    }

    public T GetPresentation() {
        T doc;
        lock (this) {
            doc = documentQueue.Dequeue();
        }
        return doc;
    }

    public bool IsPresentationAvailable {
        get {
            return (documentQueue.Count > 0true false;
        }
    }
}

class Program {
    static void Main(string[] args) {
        PresentationManager<Presentation> dm = new PresentationManager<Presentation>();

        ProcessPresentations<Presentation, PresentationManager<Presentation>>.Start(dm);

        for (int i = 0; i < 1000; i++) {
            Presentation d1 = new Presentation("title" + i.ToString()"content");
            dm.AddPresentation(d1);
            Console.WriteLine("added document {0}", d1.Title);
            Thread.Sleep(10);
        }

    }
}

 
Related examples in the same category
1.Demonstrate a generic interfaceDemonstrate a generic interface
2.Generic Interface Demo
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.