Solved

Abstract Factory Pattern Simple Question

Posted on 2006-10-28
4
513 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

19 Experts available now in Live!

Get 1:1 Help Now