?
Solved

Abstract Factory Pattern Simple Question

Posted on 2006-10-28
4
Medium Priority
?
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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