Solved

Abstract Factory Pattern Simple Question

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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!

696 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