Sign in to follow this  
Followers 0
KJohn

Abstract Factory Pattern

4 posts in this topic

#1 ·  Posted (edited)

I am doing an Advanced Software Engineering paper (revolving around Design patterns), and one of the things we do is take turns finding actual code (C#,C++,Java) in the wild (like on Google code search) and discuss it. Please note that this is different from simple example code found on "helpful" websites.

My problem is that I am not adept at using Google code search to find what I am looking for since I am looking for something very abstract (pun unintended) - an "idea" if you will. The problem is that rarely is it ever explicitly mentioned that an Abstract Factory is being used in the code as a comment so it can't really be searched for.

You can give me a fish:

if you can link me to code in the google code repository (which uses the abstract factory pattern),

and/or teach me to fish (which is what I really prefer). My presentation on Abstract Factory is over but I do not want the collective effort of compiling examples to weaken because of my inexperience. So if you could help me in some way, I'd be really grateful.

Edited by Koshy John

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi, nice to see you again.

Did you mean something like this? I am still learning c++ and c# so I am not that strong(only 4 days ago for c# >_< ). I still don't understand the thing myself at all lol.

using System;

namespace ConsoleApplication1
{
    class Ab
    {

        public interface IForm
        {
            void ShowFormName();
        }

        public interface IAbstractFormFactory
        {
            IForm MakeForm();
        }

        public class VistaForm : IForm
        {
            public void ShowFormName()
            {
                Console.WriteLine("Vista Form");
            }
        }

        public class VistaFormFactory : IAbstractFormFactory
        {
            public IForm MakeForm()
            {
                return new VistaForm();
            }
        }

        public class Win7Form : IForm
        {
            public void ShowFormName()
            {
                Console.WriteLine("Win7 Form");
            }
        }

        public class Win7FormFactory : IAbstractFormFactory
        {
            public IForm MakeForm()
            {
                return new Win7Form();
            }
        }

        public static class Application
        {
            public static void Run(IAbstractFormFactory factory)
            {
                var form = factory.MakeForm();
                form.ShowFormName();
            }
        }

        public class Program
        {
            public static void Main()
            {
                IAbstractFormFactory factory;

                var sys = 0;

                if (sys == 0)
                {
                    factory = new Win7FormFactory();
                }
                else
                {
                    factory = new VistaFormFactory();
                }

                Application.Run(factory);

                Console.ReadKey();
            }
        }
    }
}
Edited by athiwatc

Share this post


Link to post
Share on other sites

Thanks for the reply. That's the idea but I'm looking for slightly more complex code than that. Where did you find this by the way?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0