Generic usage is not clear in one program
-
see this class code
public class ShipperFactory
{
private static TShip Create()
where TShip : IShip, // interface contract constraint
new() // public parameter-less constructor constraint
{
return new TShip();
}public static readonly IDictionary\> Creators = new Dictionary\>() { { Shipper.UPS, () => Create() }, { Shipper.FedEx, () => Create() }, { Shipper.Purolator, () => Create() } }; public static IShip CreateInstance(Shipper enumModuleName) { return Creators\[enumModuleName\](); } }
specially the below code is not clear where
public static readonly IDictionary> Creators =
new Dictionary>()
{
{ Shipper.UPS, () => Create() },
{ Shipper.FedEx, () => Create() },
{ Shipper.Purolator, () => Create() }
};- see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------
private void btnUPS_Click(object sender, EventArgs e)
{
ShipperFactory.CreateInstance(Shipper.UPS).Ship();
}private void btnFedEx\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.FedEx).Ship(); } private void btnPurolator\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.Purolator).Ship(); } public enum Shipper { UPS, FedEx, Purolator } public interface IShip { void Ship(); } public abstract class ShipperBase : IShip { //Etc public abstract void Ship(); } public class ShipperUPS : ShipperBase { public override void Ship() { //-- cod
-
see this class code
public class ShipperFactory
{
private static TShip Create()
where TShip : IShip, // interface contract constraint
new() // public parameter-less constructor constraint
{
return new TShip();
}public static readonly IDictionary\> Creators = new Dictionary\>() { { Shipper.UPS, () => Create() }, { Shipper.FedEx, () => Create() }, { Shipper.Purolator, () => Create() } }; public static IShip CreateInstance(Shipper enumModuleName) { return Creators\[enumModuleName\](); } }
specially the below code is not clear where
public static readonly IDictionary> Creators =
new Dictionary>()
{
{ Shipper.UPS, () => Create() },
{ Shipper.FedEx, () => Create() },
{ Shipper.Purolator, () => Create() }
};- see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------
private void btnUPS_Click(object sender, EventArgs e)
{
ShipperFactory.CreateInstance(Shipper.UPS).Ship();
}private void btnFedEx\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.FedEx).Ship(); } private void btnPurolator\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.Purolator).Ship(); } public enum Shipper { UPS, FedEx, Purolator } public interface IShip { void Ship(); } public abstract class ShipperBase : IShip { //Etc public abstract void Ship(); } public class ShipperUPS : ShipperBase { public override void Ship() { //-- cod
The dictionary contains a "key" for each carrier and a "function" (value) that is used to create an instance of that carrier. Over-engineered and pointless indirection (IMO). (A "carrier" class and a "carrier id / ident" would be simpler).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
The dictionary contains a "key" for each carrier and a "function" (value) that is used to create an instance of that carrier. Over-engineered and pointless indirection (IMO). (A "carrier" class and a "carrier id / ident" would be simpler).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
DNFTHV * :laugh: * Do not feed the Help Vampire
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
DNFTHV * :laugh: * Do not feed the Help Vampire
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
It's actually passive aggression :cool:
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
:laugh: Some of these don't deserve "passive" - they just don't want to think for themselves...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
see this class code
public class ShipperFactory
{
private static TShip Create()
where TShip : IShip, // interface contract constraint
new() // public parameter-less constructor constraint
{
return new TShip();
}public static readonly IDictionary\> Creators = new Dictionary\>() { { Shipper.UPS, () => Create() }, { Shipper.FedEx, () => Create() }, { Shipper.Purolator, () => Create() } }; public static IShip CreateInstance(Shipper enumModuleName) { return Creators\[enumModuleName\](); } }
specially the below code is not clear where
public static readonly IDictionary> Creators =
new Dictionary>()
{
{ Shipper.UPS, () => Create() },
{ Shipper.FedEx, () => Create() },
{ Shipper.Purolator, () => Create() }
};- see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------
private void btnUPS_Click(object sender, EventArgs e)
{
ShipperFactory.CreateInstance(Shipper.UPS).Ship();
}private void btnFedEx\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.FedEx).Ship(); } private void btnPurolator\_Click(object sender, EventArgs e) { ShipperFactory.CreateInstance(Shipper.Purolator).Ship(); } public enum Shipper { UPS, FedEx, Purolator } public interface IShip { void Ship(); } public abstract class ShipperBase : IShip { //Etc public abstract void Ship(); } public class ShipperUPS : ShipperBase { public override void Ship() { //-- cod