Abstract Factory Pattern sample source code for C # Design Patterns

Source: Internet
Author: User
Tags abstract
Design | example | source code

Using System;

Namespace Design_pattern
{

public class Computer
{
Private RAM RAM;
Private CPU CPU;
Public Computer (RAM ram,cpu CPU)
{
This. Ram =ram;
This. Cpu =cpu;

Console. WriteLine ("A Computer is composed!" +this. Ram + "" +this. CPU);
}
}

Public abstract class RAM
{
Public abstract Computer Composeacomputer (CPU CPU);
}
Public abstract class CPU
{
Public abstract Computer Composeacomputer (Ram RAM);
}

Public class Pcram:ram

{
public override Computer Composeacomputer (CPU CPU)
{
Console. WriteLine ("Pccomputer ...");

return new Computer (THIS,CPU);
}


}

public class Pccpu:cpu
{
public override Computer Composeacomputer (Ram RAM)
{
Console. WriteLine ("Pccomputer ...");

return new Computer (ram,this);
}

}


public class Macram:ram

{
public override Computer Composeacomputer (CPU CPU)
{
Console. WriteLine ("Maccomputer ...");
return new Computer (THIS,CPU);
}


}

public class Maccpu:cpu
{
public override Computer Composeacomputer (Ram RAM)
{
Console. WriteLine ("Maccomputer ...");
return new Computer (ram,this);
}

}

Public abstract class Abstractfactory

{
Public abstract RAM Createram ();
Public abstract CPU createcpu ();
}

public class Pcfactory:abstractfactory
{
public override RAM Createram ()
{
return new Pcram ();
}
public override CPU Createcpu ()
{
return new Pccpu ();
}

}
public class Macfactory:abstractfactory
{
public override RAM Createram ()
{
return new Macram ();

}
public override CPU Createcpu ()
{
return new Maccpu ();
}
}

Class Itmarket

{
Private RAM RAM;
Private CPU CPU;
Public Itmarket (Abstractfactory Factory)
{
This.ram =factory. Createram ();
This.cpu =factory. Createcpu ();
}
public void Run ()
{
Ram.composeacomputer (CPU);
Cpu.composeacomputer (RAM);
}

}

Class ClientApp

{
public static void Main (string[] args)
{
Console. WriteLine ("PC factory.....\n");
Abstractfactory pcfactory=new pcfactory ();
Itmarket itmarket=new Itmarket (pcfactory);
Itmarket. Run ();

Console. WriteLine ("MAC factory.....\n");
Abstractfactory macfactory=new macfactory ();
Itmarket =new Itmarket (macfactory);
Itmarket. Run ();
}
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.