Generic parameter interface

image_pdfimage_print

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
where TPresentation : IPresentation
where TPresentationManager : IPresentationManager {
public static void Start(TPresentationManager dm) {
new Thread(new ProcessPresentations(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 {
void AddPresentation(T doc);
T GetPresentation();
bool IsPresentationAvailable {
get;
}
}

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

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 > 0) ? true : false;
}
}
}

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

ProcessPresentations>.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); } } } [/csharp]

This entry was posted in Generics. Bookmark the permalink.