欢迎光临
我们一直在努力

C#设计模式之抽象工厂(abstract factory)模式示例源代码-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

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 ();
  }
 }

   

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#设计模式之抽象工厂(abstract factory)模式示例源代码-.NET教程,Asp.Net开发
分享到: 更多 (0)