Click here to Skip to main content
15,395,390 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I Have got two answers for the internet Singleton and Factory design pattern? Which should we use? Both of them? and Why?

What I have tried:

I Have got two answers for the internet Singleton and Factory design pattern?
Posted
Updated 28-Jun-21 1:10am
Comments
CHill60 28-Jun-21 4:16am
   
It might help if you describe your situation a little better. What are you trying to achieve
Richard MacCutchan 28-Jun-21 4:48am
   
Design patterns may be used to solve issues in your design. But you cannot decide which one to use before you have the design.

1 solution

It's difficult to answer given the brevity of your question.

I wouldn't call a session a design pattern. It could be called a protocol pattern, but it's not a software pattern in the usual sense of the term. As a protocol pattern, it consists of a session establishment phase, ongoing communication phase, and session release phase. Some sessions have simple protocols, and some have complex ones. TCP and phone calls are examples of sessions that are familiar to most developers.

If we assume IP-based communication, a Service that has a session could be a singleton. If the system has more than one type of Service, each run-time instance of a Session (subclassed according to the Service that it supports) could be created using the Factory pattern. A Service might have State singletons and EventHandler singletons, although each State would probably be a Flyweight rather than a true singleton.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900