用户名: 密码: 验证码:           网站地图 高级搜索 RSS订阅 收藏本站
Google
您的位置:首页>>网络编程>>.Net编程>>阅读资讯:C#设计模式之三:Abstract Factory

C#设计模式之三:Abstract Factory

[ 来源: | 阅读:次 | 更新日期:2007-10-19 21:43:55 | 评论 0 条 | 我要投稿 ]

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

www.yueluo.net

  意图

字串9

  提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

月落网

  场景

字串5

  还是上次说的那个网络游戏,定下来是一个休闲的FPS游戏。和CS差不多,8到16个玩家在游戏里面分成2组对战射击。现在要实现初始化场景的工作。要呈现一个三维物体一般两个元素是少不了的,一是这个物体的骨架,也就是模型,二就是这个骨架上填充的纹理。 字串9

  我们知道,这样的一个游戏不可能只有一张地图,而且地图的数量肯定是会一直增加的。如果游戏在初始化场景的时候需要根据不同的地图分别加载模型和纹理对象,那么势必就会使得场景的扩充变得很不方便。由此,我们引入Abstract Factory,抽象工厂生产的都是实际类型的接口(或者抽象类型),如果加了新的场景可以确保不需要修改加载场景的那部分代码。 字串7

  示例代码

yueluo.net

字串6

  using System;
  using System.Reflection;
  namespace AbstractFactoryExample
  {
  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("AbstractFactoryExample").CreateInstance("AbstractFactoryExample." + gameSceneName);
  }
  public void LoadScene(string gameSceneName)
  {
  PatrixSceneFactory psf = GetGameScene(gameSceneName);
  Texture texture = psf.CreateTexture();
  Model model = psf.CreateModel(); 月落网
  model.FillTexture(texture);
  }
  }
  abstract class PatrixSceneFactory
  {
  public abstract Model CreateModel();
  public abstract Texture CreateTexture();
  }
  abstract class Model
  {
  public abstract void FillTexture(Texture texture);
  }
  abstract class Texture
  {
  }
  class HalfPaper : PatrixSceneFactory
  {
  public override Model CreateModel()
  {
  return new HalfPaperModel();
  }
  public override Texture CreateTexture()
  {
  return new HalfPaperTexture();
  }
  }
  class HalfPaperModel : Model
  {
  public HalfPaperModel()
  {
  Console.WriteLine("HalfPaper Model Created"); 月落
  }
  public override void FillTexture(Texture texture)
  {
  Console.WriteLine("HalfPaper Model is filled Texture");
  }
  }
  class HalfPaperTexture : Texture
  {
  public HalfPaperTexture()
  {
  Console.WriteLine("HalfPaper Texture Created");
  }
  }
  class Matrix : PatrixSceneFactory
  {
  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");
  }
  }
  }
共2页: 上一页 1 [2] 下一页
Tags:C# 设计模式 Abstract Factory
责任编辑:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为