zoukankan      html  css  js  c++  java
  • 抽象工廠模式示例(外摘)

    C#设计模式之抽象工厂(abstract factory)模式示例源代码

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

       

    }

  • 相关阅读:
    HTML5开发手机应用--viewport的作用
    media query(媒体查询)和media type(媒体类型)
    html px em pt长度单位(像素 相对长度 点)知识篇
    Markdown——入门指南
    什么是 BOM头以及如何去掉
    设计网页,常见的宽度是多少像素?或者说应该选择多大的分辨率才更美观,更能提高网站的用户驻足时间!
    CSS中元素水平居中显示的方法
    CSS hack技巧大全
    主流浏览器兼容性问题---专属CSS hack
    兼容浏览器的getElementsByClassName方法
  • 原文地址:https://www.cnblogs.com/HondaHsu/p/710759.html
Copyright © 2011-2022 走看看