Is this a good example to represent an abstract factory pattern

Want to check if this is a good example to represent an abstract factory pattern. Here's the theme Dell (Factory) does xps (product) Dell (Factory) does inspiron (Product) hp (Factory) does messenger (product) hp (Factory) does presario (Product)

BestBuy sells computers.

//Abstract factory abstract class ComputerFactory { public abstract Computer BuildComputer(Computer.ComputerType compType); } //Concrete factory class Dell : ComputerFactory { public override Computer BuildComputer(Computer.ComputerType compType) { if (compType == Computer.ComputerType.xps) return (new xps()); else if (compType == Computer.ComputerType.inspiron) return new inspiron(); else return null; } } //Concrete factory class Hp : ComputerFactory { public override Computer BuildComputer(Computer.ComputerType compType) { if (compType == Computer.ComputerType.envoy) return (new envoy()); else if (compType == Computer.ComputerType.presario) return new presario(); else return null; } } //Abstract product public abstract class Computer { public abstract string Mhz { get; set; } public enum ComputerType { xps, inspiron, envoy, presario } } //Concrete product for DELL public class xps : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for DELL public class inspiron : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for HP public class envoy : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for HP public class presario : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } public class BestBuy { ComputerFactory compFactory; Computer comp; public BestBuy(Computer.ComputerType compType) { if (compType == Computer.ComputerType.xps || compType == Computer.ComputerType.inspiron) compFactory = new Dell(); else compFactory = new Hp(); comp = compFactory.BuildComputer(compType); } public Computer Sell() { return comp; } } 

Thanks in advance.

+6
c # factory-pattern
source share
3 answers

This is a good example of parts of a template. The basic construction of objects is a worthy example, but the logic relies on a single Computer.ComputerType enumeration. This listing should know in advance, each type of computer that is exposed to each factory.

Often the motivation for using an abstract factory is to abstract this type of hard-coded requirement from an image. Instead of having a single enumeration, it would be better to add a ComputerType class and let the factory return a collection of available types. You can then use the returned ComputerType to build new systems.

This allows you to add other factories without changing your API, which is one of the main advantages of the abstract factory pattern. Read Annotation factory A sample is one of the highlights:

The client does not know (or does not care) about which specific objects he receives from each of these internal plants, since he uses only the common interfaces of his products.

In this case, you β€œhard code” the known types into an enumeration, which violates this part of the template.

+8
source share

I'm not an expert on the Factory pattern, but here are a few things I would do differently:

  • Instead of an abstract class, I would use an interface. Therefore, if "Dell" needs to be inherited from another class, it can and still be ComputerFactory, for example, implementing IComputerFactory.
  • Another little thing is to use "switch" instead of "if / else if" in your BuildComputer function. Who knows how many computers you might end up in the end.
  • How do you know which specific Factory to use between Hp and Dell? You can use something like Autofac "to enable Factory.
+3
source share

I think that there is only one type of product in the script and code you provided, for example, "Computer." No family of products. Therefore, the abstract factory pattern does not apply here. Instead, you can use the factory pattern. I changed the code below for understanding.

 //Abstract factory abstract class ComputerFactory { public abstract Computer BuildComputer(Computer.ComputerType compType); } public class ConcreteFactory : ComputerFactory { public override Computer BuildComputer(Computer.ComputerType compType) { if (compType == Computer.ComputerType.xps) return (new xps()); else if (compType == Computer.ComputerType.inspiron) return new inspiron(); else if (compType == Computer.ComputerType.envoy) return (new envoy()); else if (compType == Computer.ComputerType.presario) return new presario(); else return null; } } //Abstract product public abstract class Computer { public abstract string Mhz { get; set; } public enum ComputerType { xps, inspiron, envoy, presario } } //Concrete product for DELL public class xps : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for DELL public class inspiron : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for HP public class envoy : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } //Concrete product for HP public class presario : Computer { string _mhz = string.Empty; public override string Mhz { get { return _mhz; } set { _mhz = value; } } } public class BestBuy { ConcreteFactory compFactory; Computer comp; public BestBuy(Computer.ComputerType compType) { comp = compFactory.BuildComputer(compType); } public Computer Sell() { return comp; } } 
0
source share

All Articles