Factory Method Pattern : Factory Pattern « Design Patterns « C# / C Sharp






Factory Method Pattern

 

using System;
using System.Collections;

public abstract class Module {
    public abstract void SomeModule();
}

public class FeaturesModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Technical content.");
    }

    public FeaturesModule() {}
}

public class InstructionModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Instruction content.");
    }

    public InstructionModule() {}

}

public class PictureModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Picture content.");
    }

    public PictureModule() {}

}


public class TechnicalModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Technical content.");
    }

    public TechnicalModule() {}

}


public abstract class Page {
    protected ArrayList pageCompositor = new ArrayList();
    public abstract void AddModule();
    public abstract void DisplayPage();
}


public class CatalogPage : Page {
    public override void AddModule() {
        this.pageCompositor.Clear();
        this.pageCompositor.Add(new FeaturesModule());
        this.pageCompositor.Add(new PictureModule());
    }
    public override void DisplayPage() {
        foreach (Module c in this.pageCompositor)
            c.SomeModule();
    }


    public CatalogPage() {
        this.AddModule();
    }
}


public class ManualPage : Page {
    public override void AddModule() {
        this.pageCompositor.Clear();
        this.pageCompositor.Add(new TechnicalModule());
        this.pageCompositor.Add(new PictureModule());
        this.pageCompositor.Add(new InstructionModule());
    }
    public override void DisplayPage() {
        Console.WriteLine("Manual page contains:");
        foreach (Module c in this.pageCompositor)
            c.SomeModule();
        Console.WriteLine();
    }
    public ManualPage() { }
}
class Client {
    static void Main(string[] args) {
        Page p = new CatalogPage();
        p.AddModule();
        p.DisplayPage();
        p = new ManualPage();
        p.AddModule();
        p.DisplayPage();
    }
}

 








Related examples in the same category