Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6750123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:46:16+00:00 2026-05-26T12:46:16+00:00

Which way use of Factory is better(correct)? IPacket info = PacketFactory.CreatePacketObject(PacketType.Info, currentUser, DateTime.Now, disconnected);

  • 0

Which way use of Factory is better(correct)?

IPacket info = PacketFactory.CreatePacketObject(PacketType.Info, currentUser, DateTime.Now, " disconnected");

or should I throw out second method in PacketFactory and use this one?

IPacket info = PacketFactory.CreatePacketObject(PacketType.Info);
            info.CreationTime = DateTime.Now;
            info.Creator = currentUser;
            info.Data = " disconnected";

or maybe some other?

PacketFactory code:

public static class PacketFactory
    {
        public static IPacket CreatePacketObject(PacketType type)
        {
            IPacket packetToCreate = null;
            switch (type)
            {
                case PacketType.Info:
                    packetToCreate = new Info();
                    break;
                case PacketType.Log:
                    packetToCreate = new Log();
                    break;
                case PacketType.Message:
                    packetToCreate = new Message();
                    break;
            }
            return packetToCreate;
        }

        public static IPacket CreatePacketObject(PacketType type, Client creator, DateTime creationTime, string data)
        {
            IPacket packetToCreate = null;
            switch (type)
            {
                case PacketType.Info:
                    packetToCreate = new Info(creator, creationTime, data);
                    break;
                case PacketType.Log:
                    packetToCreate = new Log(creator, creationTime, data);
                    break;
                case PacketType.Message:
                    packetToCreate = new Message(creator, creationTime, data);
                    break;
            }
            return packetToCreate;
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:46:16+00:00Added an answer on May 26, 2026 at 12:46 pm

    Before applying a pattern you should have a clear idea of what you gain by doing so, and in this instance I don’t really see that introducing a static “Factory” is gaining you anything. Look at it from the point of view of the client of the PacketFactory: has introducing it reduced the coupling between the client and the various concrete implementors of IPacket? I would argue not since the client already has to know which kind of IPacket it wants by specifying a enumeration value of either PacketType.Info, PacketType.Message or PacketType.Log. How is that any different from the client knowing about the Info, Message and Log classes? Since the “Factory” is a static class the client is just as coupled to the type of IPacket being returned as it would be if it just called the constructor of the appropriate IPacket implementor because you would have to change the client in order for it to work with a different type of IPacket in either case.

    So, if you really must use a Factory of some kind, then I would suggest employing the Abstract Factory pattern so that clients of the Factory would only depend on the factory interface and would therefore be able to work with different kinds of IPacket without having to change. For example:

    public interface IPacketFactory
    {
       IPacket CreatePacket();
       IPacket CreatePacket(Client creator, DateTime creationTime, string data);
    }
    
    public class MessageFactory : IPacketFactory
    {
       public CreatePacket()
       {
          return new Message();
       }
    
       public CreatePacket(Client creator, DateTime creationTime, string data)
       {
          return new Message(creator, creationTime, data);
       }
    }
    
    //You'd implement factories for each IPacket type...
    
    public class Client
    {
       private IPacketFactory _factory;
    
       public Client(IPacketFactory factory)
       {
          _factory = factory;
       }
    
       public SomeMethodThatNeedsToCreateIPacketInstance()
       { 
          IPacket packet = _factory.CreatePacket();
    
         //work with packet without caring what type it is
       }
    
    }
    
    
    //a higher level class or IOC container would construct the client with the appropriate factory
    
    Client client = new Client(new MessageFactory());
    
    // the Client class can work with different IPacket instances without it having to change (it's decoupled)
    
    Client client2 = new Client(new LogFactory());
    

    As far as whether the factory should allow one to construct an IPacket without specifying the creator, data and creation time or not depends on the class’s invariants. If the class invariants can be satisfied when the fields are not specified then that’s fine, otherwise they should be required. A part of a class’s job should be to ensure that it cannot be constructed in an invalid state since users of the class will be depending upon that to be the case.

    In the case where one of the IPacket implementors needs extra parameters:

    The Abstract Factory pattern needs there to be a uniform interface for all implementers, so if it makes sense for all the factories to have a Create method with the extra parameters then you can add them to the interface. One form of this is to pass an object with various properties/methods that the Create method can use to derive the extra parameter values it needs. A special case is Double Dispatch where the caller passes itself (in this case the Client) and is then called from inside the Create method.

    //in MessageFactory : the PacketContext holds various data that may be relevant to creation
    
    public IPacket Create(Client creator, DateTime creationTime, string data, PacketContext ctx)
    {
       return new Message(creator, creationTime, data, ctx.SomeExtraData); 
    }
    
    //in LogFactory: the Log doesn't need anything from the PacketContext but it does call something on the Client (Double Dispatch)
    
    public IPacket Create(Client creator, DateTime creationTime, string data, PacketContext ctx)
    {
       return new Log(creator.Name, creationTime, data);
    }
    

    You need to remember that the goal is to abstract the type of IPacket being created, so if whilst implementing this approach you start to get the feeling that the Client is starting to implicitly know the specific type being constructed then you may have to take a step back and consider if the factory is appropriate at all. Your only other option is to provide the extra information when you construct the factory (i.e. pass it to the constructor).

    public class MessageFactory : IPacketFactory
    {
       private object _data;
    
       public MessageFactory(object extraData)
       {
          _data = extraData;
       }
    
        IPacket CreatePacket(Client creator, DateTime creationTime, string data)
        {
           return new Message(creator, creationTime, data, _extraData);
        }
    
        ///rest of implementation
    }
    

    Those represent some of the options, but in any case, I would strongly advise that you do not use a static or singleton “Factory” class because it will strongly-couple your client class to the factory and most likely the IPacket subclass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is better way to use jQuery() ? I've heard that jQuery(element) doesn't ruin
When using a Random Number Generator, which is the better way to use it
Does anyone know the correct way to explicitly specify which rules Gendarme will use?
I'm really interested which way you guys use to effectivly debug a large (let's
Is there a way to use yield blocks to implement an IEnumerator<T> which can
Is there any way to detect which windows XP theme is in use? I
Which way is better for removing float decimals places or is there a more
In classic ASP (which I am forced to use), I have a few factory
Which way do you prefer to create your forms in MVC? <% Html.Form() {
In which way could I call the function that automatically opens my annotation (with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.