mercredi 8 novembre 2023

Abstract class vs Factory bridge design pattern C# example [closed]

I'm making an imaging desktop application using Windows Form in C# .NETFramework. The application main task is to process and visualize images form an images stack. I can have a single stack or two of them combined, some processing steps are gonna be done on every Image before visualization and others when reading from the stacks depending on the input data. This is a simple prototype with abstract classes, how can it be written using Factory-Bridge Design pattern?

class:

public abstract class Mstack
{
    public abstract int GetLen();
    public abstract Image<Gray, float> GetImage(int i);
}
public class MBaseStackClass<TDepth> : Mstack where TDepth : new ()
{
    Image<Gray, TDepth>[] data;
    int len;
    public MBaseStackClass(Image<Gray, TDepth>[] data)
    {
        this.data = data;
        len = data.Length;

    }
    override public Image<Gray, float> GetImage(int i)
    {
        return data[i].Convert<Gray, float>();
    }
    public override int GetLen()
    {
        return len;
    }
}
public class MDEStackClass : Mstack
{
    public enum viewmode { high_en, low_en, divide }
    private viewmode vm = viewmode.high_en;
    Mstack stlow;
    Mstack sthigh;

    public MDEStackClass(Mstack stlow, Mstack sthigh)
    {
        this.stlow = stlow;
        this.sthigh = sthigh;
        if(stlow.GetLen() != sthigh.GetLen()) { throw new Exception("Length must be equal"); }
    }


    override public Image<Gray, float> GetImage(int i)
    {
        switch (vm)
        {
            case viewmode.high_en: 
                return sthigh.GetImage(i);
            case viewmode.low_en:
                return stlow.GetImage(i);
            case viewmode.divide:
                return combine(i);
            default:
                return null;
        }
    }
    public override int GetLen()
    {
        return sthigh.GetLen();
    }
    public void set_viewmode(viewmode vmin)
    {
        vm = vmin;
    }
     
    private Image<Gray, float> combine(int i)//division
    {
        return stlow.GetImage(GetLen() - i-1).Mul(sthigh.GetImage(i).Pow(-1));
    }

}

Implementation in main program:


        private void openCEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stopwatch st = new Stopwatch();
            st.Start();
            stackManager = new MDEStackClass(
                new MBaseStackClass<ushort>((new ReadImageUser()).readFromCeflaFbpFolder(0)),
                new MBaseStackClass<ushort>((new ReadImageUser()).readFromCeflaFbpFolder(1))
                );
            trackBar1.Maximum = stackManager.GetLen() - 1;
            st.Stop();
            Console.WriteLine(st.ElapsedMilliseconds);
            GC.Collect();
        }
        viewmode vm = viewmode.divide;
        public void show_image(int i)
        {
            stackManager.set_viewmode(vm);
            //for the future image processing goes like: pictureBox1.Image = img_processing(stackManager.GetImage(i)).Resize(...).ToBitmap();
            pictureBox1.Image = (stackManager.GetImage(i)).Resize(pictureBox1.Width, pictureBox1.Height, Emgu.CV.CvEnum.Inter.Cubic).ToBitmap();
            pictureBox1.Refresh();
            GC.Collect();
        }

Aucun commentaire:

Enregistrer un commentaire