Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Abstract Factory Pattern Simple Question

Posted on 2006-10-28
4
Medium Priority
?
608 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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