Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Abstract factory

Abstract factory

Scheduled Pinned Locked Moved Design and Architecture
questionhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phil005
    wrote on last edited by
    #1

    Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }

    P D 2 Replies Last reply
    0
    • P Phil005

      Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }

      P Offline
      P Offline
      Phil005
      wrote on last edited by
      #2

      Can somebody help me with this? It's for my colledge presentation and I have to figure it out. Thanks!

      1 Reply Last reply
      0
      • P Phil005

        Hi! Can somebody explain to me why we use a Base interface as a return type in a Factory interface? Second question in concerned with the code in Main. I figured out that Factory factory = new ConcreteFactory2() is a reference to the implementation of a ConcreteFactory2 of GetObject() method. What is the next line? Thanks. I appreciate your help! abstractfactory { class Program { static void Main(string[] args) { Factory factory = new ConcreteFactory2(); Base obj = factory.GetObject(); obj.DoIt(); } } } interface Factory { Base GetObject(); } class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); // do we return an instance of derived 1 interface? } } class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console .WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } }

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.

        VehicleFactory myFactory = new PetrolVehicleFactory();
        Vehicle myVehicle = myFactory.GetVehicle();
        myVehicle.Start();
        myVehicle.DriveOffIntoTheSunset();

        I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:

        VehicleFactory myFactory = new ElectricVehicleFactory();
        Vehicle myVehicle = myFactory.GetVehicle();
        myVehicle.Start();
        myVehicle.DriveOffIntoTheSunset();

        As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.

        L P 2 Replies Last reply
        0
        • D David Skelly

          The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.

          VehicleFactory myFactory = new PetrolVehicleFactory();
          Vehicle myVehicle = myFactory.GetVehicle();
          myVehicle.Start();
          myVehicle.DriveOffIntoTheSunset();

          I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:

          VehicleFactory myFactory = new ElectricVehicleFactory();
          Vehicle myVehicle = myFactory.GetVehicle();
          myVehicle.Start();
          myVehicle.DriveOffIntoTheSunset();

          As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          A nice, clear and concise explanation; have a 5. :thumbsup:

          The best things in life are not things.

          1 Reply Last reply
          0
          • D David Skelly

            The abstract factory pattern is explained fairly well here: http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] It returns Base as the object type because different factories will return different types of object, but those objects must have something in common. Imagine a VehicleFactory interface with a method GetVehicle that returns a Vehicle. One type of VehicleFactory might be a PetrolVehicleFactory, that returns vehicles that run on petrol. Another might be an ElectricVehicleFactory that returns electric-powered vehicles. Both of these return type Vehicle, so I can use the factories interchangeably without having to worry about whether it is an ElectricVehicle or a PetrolVehicle that is being handed to me.

            VehicleFactory myFactory = new PetrolVehicleFactory();
            Vehicle myVehicle = myFactory.GetVehicle();
            myVehicle.Start();
            myVehicle.DriveOffIntoTheSunset();

            I can swap PetrolVehicleFactory for ElectricVehicleFactory, and nothing else in my application needs to be changed:

            VehicleFactory myFactory = new ElectricVehicleFactory();
            Vehicle myVehicle = myFactory.GetVehicle();
            myVehicle.Start();
            myVehicle.DriveOffIntoTheSunset();

            As long as ElectricVehicle and PetrolVehicle both do what a Vehicle is supposed to do, I don't care what exact type of Vehicle the factory gives me. I just want a Vehicle so that I can drive off into the sunset. It's the factory's responsibility to know what type of Vehicle to make and how to make it.

            P Offline
            P Offline
            Phil005
            wrote on last edited by
            #5

            Thank you for your explanation. It's a lot clearer. Could you just explain the client code? I mean is Vehicle myVehicle = myFactory.GetVehicle(); a reference to GetVehicle? Thanks

            L 1 Reply Last reply
            0
            • P Phil005

              Thank you for your explanation. It's a lot clearer. Could you just explain the client code? I mean is Vehicle myVehicle = myFactory.GetVehicle(); a reference to GetVehicle? Thanks

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The variable myVehicle holds a reference to the Vehicle object returned from the GetVehicle() method.

              P 1 Reply Last reply
              0
              • L Lost User

                The variable myVehicle holds a reference to the Vehicle object returned from the GetVehicle() method.

                P Offline
                P Offline
                Phil005
                wrote on last edited by
                #7

                Thank you for the answer! I did well at my presentation! :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups