AbstractFactory pattern: Provides an interface to create a family of products, each subclass of which can produce a series of related products. Product family is the soul of this model and the biggest difference from the Builder model.Copy the code
using System;
namespace AbstractFactory
{
/ * * / products
public abstract class Phone {
public abstract void design();
}
public abstract class Pad {
public abstract void design();
}
/* The factory that produces the product */
public abstract class Efactory
{
public abstract Phone createPhone();
public abstract Pad createPad();
}
/ * * / m home
public class MiFactory : Efactory
{
public override Pad createPad()
{
return new Mipad();
}
public override Phone createPhone()
{
return newMiphone(); }}/* Apple */
public class IFactory : Efactory
{
public override Pad createPad()
{
return new Ipad();
}
public override Phone createPhone()
{
return newIphone(); }}public class Company
{
public void create(Efactory factory){ factory.createPhone().design(); factory.createPad().design(); }}/* Product */
public class Miphone : Phone
{
public override void design()
{
Console.WriteLine("I am Xiaomi Phone"); }}public class Mipad : Pad
{
public override void design()
{
Console.WriteLine("I'm a Xiaomi tablet."); }}/* Apple home product */
public class Iphone : Phone
{
public override void design()
{
Console.WriteLine("I'm an iPhone."); }}public class Ipad : Pad
{
public override void design()
{
Console.WriteLine("I'm an Apple tablet."); }}public class Test {
static void Main(string[] args){
// I want a bucket.
Efactory miFactory = new MiFactory();
Company company = new Company();
// The company will produce the products of the factory it sets free.company.create(miFactory); }}}Copy the code