Creational design patterns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Creational design patterns

    I have been trying to wrap my head around design patterns
    in c# and am very new to this.

    I do not understand the point of using factories to create
    objects. My understanding is that creating factories to
    initialise objects has the benifit of allowing you to
    easily add classes without modifying the code.

    However in saying this, creating new classes usually
    requires requiring new input for the constructors when
    requires a change to feed the right data into the factory
    to use its constructor anyway. Could anyone here
    elaborate on my problem?
    Regards
    Dan
  • Aravind C

    #2
    Re: Creational design patterns

    Hi,

    If your class factory supports the creation of objects that accept
    different types of constructor parameter arguments , you may also like
    to consider exposing a Create method in your factory that accepts an array
    of
    generic System.Object objects, which contains the parameters to
    initialize your object with.

    MyClassFactory. Create(String className, Object[] constructorPara ms);

    When the factory creates the object via reflection, the right
    constructor would be invoked based on the type and number of
    the parameters passed in constructorPara ms.

    Regards,
    Aravind C


    <anonymous@disc ussions.microso ft.com> wrote in message
    news:0a5e01c3a8 2e$968a8bd0$a10 1280a@phx.gbl.. .[color=blue]
    > I have been trying to wrap my head around design patterns
    > in c# and am very new to this.
    >
    > I do not understand the point of using factories to create
    > objects. My understanding is that creating factories to
    > initialise objects has the benifit of allowing you to
    > easily add classes without modifying the code.
    >
    > However in saying this, creating new classes usually
    > requires requiring new input for the constructors when
    > requires a change to feed the right data into the factory
    > to use its constructor anyway. Could anyone here
    > elaborate on my problem?
    > Regards
    > Dan[/color]


    Comment

    • Yura2000

      #3
      Creational design patterns

      Hi!
      For right using of patterns you need right architecture
      and right implementation.
      When ou use Factory pattern you can pass as argument any
      object that implements any generic interface.
      In Factory method u can check what type of this object and
      return something expected for this type.
      For instance, u pass object implements IEmployee interface.
      Within pattern you check something like this:
      if (objEmployee is Manager)
      [color=blue]
      >-----Original Message-----
      >I have been trying to wrap my head around design patterns
      >in c# and am very new to this.
      >
      >I do not understand the point of using factories to[/color]
      create[color=blue]
      >objects. My understanding is that creating factories to
      >initialise objects has the benifit of allowing you to
      >easily add classes without modifying the code.
      >
      >However in saying this, creating new classes usually
      >requires requiring new input for the constructors when
      >requires a change to feed the right data into the factory
      >to use its constructor anyway. Could anyone here
      >elaborate on my problem?
      >Regards
      >Dan
      >.
      >[/color]

      Comment

      • Yura2000

        #4
        Creational design patterns

        Hi!
        For right using of patterns you need right architecture
        and right implementation.
        When ou use Factory pattern you can pass as argument any
        object that implements any generic interface.
        In Factory method u can check what type of this object and
        return something expected for this type.
        For instance, u pass object implements IEmployee interface.
        Within pattern you check something like this:
        if (objEmployee is Manager)
        <your code here>

        I hope it will help u.
        Regards
        [color=blue]
        >-----Original Message-----
        >I have been trying to wrap my head around design patterns
        >in c# and am very new to this.
        >
        >I do not understand the point of using factories to[/color]
        create[color=blue]
        >objects. My understanding is that creating factories to
        >initialise objects has the benifit of allowing you to
        >easily add classes without modifying the code.
        >
        >However in saying this, creating new classes usually
        >requires requiring new input for the constructors when
        >requires a change to feed the right data into the factory
        >to use its constructor anyway. Could anyone here
        >elaborate on my problem?
        >Regards
        >Dan
        >.
        >[/color]

        Comment

        • Guest's Avatar

          #5
          Re: Creational design patterns

          My question still remains unanswered. What is the
          difference from calling the constructor directly instead
          of going through a factory.

          Regards
          Dan
          [color=blue]
          >-----Original Message-----
          >Hi,
          >
          >If your class factory supports the creation of objects[/color]
          that accept[color=blue]
          >different types of constructor parameter arguments , you[/color]
          may also like[color=blue]
          >to consider exposing a Create method in your factory that[/color]
          accepts an array[color=blue]
          >of
          >generic System.Object objects, which contains the[/color]
          parameters to[color=blue]
          >initialize your object with.
          >
          >MyClassFactory .Create(String className, Object[][/color]
          constructorPara ms);[color=blue]
          >
          >When the factory creates the object via reflection, the[/color]
          right[color=blue]
          >constructor would be invoked based on the type and number[/color]
          of[color=blue]
          >the parameters passed in constructorPara ms.
          >
          >Regards,
          >Aravind C
          >
          >
          ><anonymous@dis cussions.micros oft.com> wrote in message
          >news:0a5e01c3a 82e$968a8bd0$a1 01280a@phx.gbl. ..[color=green]
          >> I have been trying to wrap my head around design[/color][/color]
          patterns[color=blue][color=green]
          >> in c# and am very new to this.
          >>
          >> I do not understand the point of using factories to[/color][/color]
          create[color=blue][color=green]
          >> objects. My understanding is that creating factories to
          >> initialise objects has the benifit of allowing you to
          >> easily add classes without modifying the code.
          >>
          >> However in saying this, creating new classes usually
          >> requires requiring new input for the constructors when
          >> requires a change to feed the right data into the[/color][/color]
          factory[color=blue][color=green]
          >> to use its constructor anyway. Could anyone here
          >> elaborate on my problem?
          >> Regards
          >> Dan[/color]
          >
          >
          >.
          >[/color]

          Comment

          • Jeff Louie

            #6
            Re: Creational design patterns

            Hi Dan... Here is the list from Guy Steele
            1) The methods can have unique self documenting names
            2) Objects can be reused rather than constructed each time
            3) You can return non public subtypes, hiding the implementation,
            supporting dynamic registration of a new subtype on the fly. So you may
            get back some encryption/decryption service of unknowm implentation on
            the fly, as long as the new service supports the base interface methods
            eg encrypt, decrypt etc.

            Regards,
            Jeff[color=blue]
            >My question still remains unanswered. What is the[/color]
            difference from calling the constructor directly instead
            of going through a factory.<

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Jeff Louie

              #7
              Re: Creational design patterns

              Second try at posting. 1) Lets you use self documenting names and
              redundant parameter lists. 2) Allows the reuse of existing objects 3)
              Allows dynamic instantiation of non public subtypes eg. registering a
              new crytography service and returning an instance of the new service
              when you only know the interface type. Adding new choices to a menu
              item when you only know the supported interface of the menu item.


              Regards,
              Jeff

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Daniel Billingsley

                #8
                Re: Creational design patterns

                Dan, the factory shields from the underlying implementation. That is to
                say, it is classic OO.

                Data access is a great example. You can write a factory that returns a
                IDbConnection interface and all the code "above" that point never knows or
                cares whether that's a SQL Server or OLEDB connection "under the hood".

                <anonymous@disc ussions.microso ft.com> wrote in message
                news:08bc01c3a8 c8$f879e710$a30 1280a@phx.gbl.. .[color=blue]
                > My question still remains unanswered. What is the
                > difference from calling the constructor directly instead
                > of going through a factory.
                >
                > Regards
                > Dan[/color]


                Comment

                Working...