All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
.NET,Framework与.NET,Framework,SDK有什么不同?_[Asp.Net教程]
Visual C++ 6.0教程:#include文件包含指令
PHP技巧:得到一个字符串的最后一个字符_[PHP教程]
怎样正确理解.NET和SQL,Server中“空值”_.net资料_编程技术
ASP.NET,2.0发送电子邮件全面剖析之二_.net资料_编程技术
PHP学习宝典-第七章_[PHP教程]
在,Linux,下运行,ASP.NET_[Asp.Net教程]
用,PHP,构建自定义搜索引擎_php资料_编程技术
新瓶旧酒ASP.NET,AJAX(9),-,客户端脚本编程(Sys.Net命名空间下的)_[Asp.Net教程]
制作WEB在线编辑器-插入HTML标签_.net资料_编程技术
Reading number is top 10 pictures
六种更聪明的工作方法
BingBingFan apple dew point photo gallery4
The money of more than 100 countries and regions14
俄罗斯台球天后惊艳魅惑1
Sora aoi on twitter1
Seductive beauty of crime2
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first2
中国的阶级现状
Beautiful vacuum girl1
Beautiful Japanese beauty(漂亮的日本美女)
Download software ranking
Tram sex maniac 2 (H) rar bag17
matrix1
The Bermuda triangle3
asp.net技术内幕
传奇私服架设教程-chm
I'm come from Beijing2
功夫熊猫2(上集)
Detective task-the top secret prostitution files
linux初级教程
Sora aoi - one of more PK
aaa published in(发表于) 2013/12/18 8:20:38 Edit(编辑)
无废话C#设计模式之四:Factory,Method_.net资料_编程技术

无废话C#设计模式之四:Factory,Method_.net资料_编程技术

无废话C#设计模式之四:Factory Method_.net资料_编程技术-你的首页-uuhomepage.com

  本系列文章将向大家介绍一下C#的设计模式,此为第四篇文章,相信对大家会有所帮助的。废话不多说,继续来看。


  意图


  定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。


  场景


  上次,我们使用抽象工厂解决了生产一组产品的问题,但是我们把各个场景作为了具体工厂来生产场景模式和场景纹理两个产品。在调用代码中也并没有出现具体工厂的影子。其实,场景类要做的不仅仅是创建具体的产品系列,可能它还需要做一个初始化工作。那么,我们就需要在调用代码中能得到这个场景类。


  在前一节中,由于场景类(比如HalfPaper)本身是具体级别的(具体工厂)。那么,我们也不应该在调用代码中直接依赖场景类。因此,我们可以使用工厂方法来生产这个具体产品。


  示例代码



  using System;
  using System.Reflection;
  namespace FactoryMethodExample
  {
  class Program
  {
  static void Main(string[] args)
  {
  Patrix patrix = new Patrix();
  patrix.LoadScene("HalfPaper");
  patrix.LoadScene("Matrix");
  }
  }
  class Patrix
  {
  private PatrixSceneFactory GetGameScene(string gameSceneName)
  {
  return (PatrixSceneFactory)Assembly.Load("FactoryMethodExample").CreateInstance("FactoryMethodExample." + gameSceneName + "Factory");
  }
  public void LoadScene(string gameSceneName)
  {
  PatrixSceneFactory psf = GetGameScene(gameSceneName);
  PatrixScene ps = psf.CreateScene();
  ps.InitScene();
  }
  }
  abstract class PatrixSceneFactory
  {
  public abstract PatrixScene CreateScene();
  }
  abstract class PatrixScene
  {
  public void InitScene()
  {
  Texture texture = CreateTexture();
  Model model = CreateModel();
  model.FillTexture(texture);
  }
  public abstract Model CreateModel();
  public abstract Texture CreateTexture();
  }
  abstract class Model
  {
  public abstract void FillTexture(Texture texture);
  }
  abstract class Texture
  {
  }
  class HalfPaperFactory : PatrixSceneFactory
  {
  public override PatrixScene CreateScene()
  {
  return new HalfPaper();
  }
  }
  class HalfPaper : PatrixScene
  {
  public HalfPaper()
  {
  Console.WriteLine("HalfPaper Creating");
  }
  public override Model CreateModel()
  {
  return new HalfPaperModel();
  }
  public override Texture CreateTexture()
  {
  return new HalfPaperTexture();
  }
  }
  class HalfPaperModel : Model
  {
  public HalfPaperModel()
  {
  Console.WriteLine("HalfPaper Model Creating");
  }
  public override void FillTexture(Texture texture)
  {
  Console.WriteLine("HalfPaper Model is filled Texture");
  }
  }
  class HalfPaperTexture : Texture
  {
  public HalfPaperTexture()
  {
  Console.WriteLine("HalfPaper Texture Created");
  }
  }
  class MatrixFactory : PatrixSceneFactory
  {
  public override PatrixScene CreateScene()
  {
  return new Matrix();
  }
  }
  class Matrix : PatrixScene
  {
  public Matrix()
  {
  Console.WriteLine("Matrix Created");
  }
  public override Model CreateModel()
  {
  return new MatrixModel();
  }
  public override Texture CreateTexture()
  {
  return new MatrixTexture();
  }
  }
  class MatrixModel : Model
  {
  public MatrixModel()
  {
  Console.WriteLine("Matrix Model Created");
  }
  public override void FillTexture(Texture texture)
  {
  Console.WriteLine("Matrix Model is filled Texture");
  }
  }
  class MatrixTexture : Texture
  {
  public MatrixTexture()
  {
  Console.WriteLine("Matrix Texture Created");
  }
  }
  }



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.