mardi 15 janvier 2019

Is my example about Abstract Factory Design Pattern correct?

I want to know my code about Abstract Factory Pattern is near the principle? My exm is about Windows Explorer that when user run it , new form and other materials in form created .

Please look my code and know me about that.

Output :: Hello, world! Form Created Close button created Minimize button created

//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine("Hello, world!");

            WindowsFactory formFactory=new Window();
           WindowManager wm=new WindowManager(formFactory);
            wm.RunForm();


        }
    }

    abstract class WindowsFactory
    {
        public abstract Form CreateForm();
        public abstract Button CreateCloseButton();
        public abstract Button CreateMinimizebutton();

    }
   class Window:WindowsFactory
   {
       public override Form CreateForm()
       {
          return new XForm();
       }
       public override Button CreateCloseButton()
       {
           return new CloseButton();
       }
       public override Button CreateMinimizebutton()
       {
           return new MinimizeButton();
       }
   }
   abstract class Form
   {
        public abstract void DrawForm();
   }
    class XForm:Form
    {
        public override void DrawForm()
        {
            Console.WriteLine("Form Created");
        }
    }

    abstract class Button
    {
        public abstract void DrawCloseButton();
        public abstract void DrawMinimizeButton();
    }
    class CloseButton:Button
    {
        public override void DrawCloseButton()
        {
            Console.WriteLine("Close button created");
        }
        public override void DrawMinimizeButton()
        {

        }
    }
    class MinimizeButton:Button
    {
        public override void DrawCloseButton()
        {

        }
        public override void DrawMinimizeButton()
        {
            Console.WriteLine("Minimize button created");
        }
    }


    class WindowManager
    {
        private Form _form;
        private Button _closeButton;
        private Button _minimizeButton;

        public WindowManager(WindowsFactory factory)
        {
            _form=factory.CreateForm();
            _closeButton=factory.CreateCloseButton();
            _minimizeButton=factory.CreateMinimizebutton();
        }
        public void RunForm()
        {
            _form.DrawForm();
            _closeButton.DrawCloseButton();
            _minimizeButton.DrawMinimizeButton();
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire