Solved

Abstract Factory Pattern Simple Question

Posted on 2006-10-28
4
531 Views
Last Modified: 2006-11-18
Hi experts,
      My question is: imagine we have 3 different Missiles, 1.Guided 2.Unguided 3.Splitters.
How do you apply Abstract Factory Pattern to this problem?  
0
Comment
Question by:afsheenarab
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17826429
You basically create an abstract class with a method that returns an object of the correct type based on a parameter or some other external/configuration value:

Assuming a set of classes like:

    public abstract class Missile { }
    public class GuidedMissile : Missile { }
    public class UnguidedMissile : Missile { }
    public class SplittersMissile : Missile { }

You could use a factory something like:

    public abstract class MissileFactory
    {
        public static Missile Create(int type)
        {
            Missile newMissile;

            switch (type)
            {
                case 1:
                    newMissile = new GuidedMissile();
                    break;
                case 2:
                    newMissile = new UnguidedMissile();
                    break;
                case 3:
                    newMissile = new SplittersMissile();
                    break;
                default:
                    throw new ArgumentException("Unknown missile type");
            }

            return newMissile;
        }
    }

And use it like:

    Missile myMissile = MissileFactory.Create(1);
0
 

Author Comment

by:afsheenarab
ID: 17826558
So, AbstractProduct is "abstract class missile" and concrete products are GuidedMissile:Missile ,UnguidedMissile:Missile, and SplittersMissile:Missile.
I still don't know what happened to the AbstractFactory and ConcreteFactory. As I found out the abstract MissileFactory is the AbstractFactory so where is the ConcreteFactory? .I know maybe it sounds stupid but when it comes to patterns i think i am stupid. sorry would u explain it a little bit more.
My idea was like having

abstract MilitaryFactory as Abstract Factory,
MissileFactory:MilitaryFactory as Concrete Factory,

and Missile as Abstract Product,                                -------->  These are the same in your design.
and Guided:Missile,Unguided:Missile,Splitters:Missile  -------->
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17826600
What I have posted is an Abstract Factory implementation. It sounds like what you are actually after is a Concrete Factory implementation, which is a little different.

See if this article is any use to you:

    http://msdn.microsoft.com/msdnmag/issues/03/03/DesignPatterns/
0
 

Author Comment

by:afsheenarab
ID: 17826659
I am sure that the design that I want is Abstract Factory maybe the example that gave u was not in a correct format. Basically I am trying to make it like http://www.dofactory.com/Patterns/PatternAbstract.aspx. Thank you for your attention btw.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now