求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

设计模式教程
简单工厂模式
AbstractFactory模式
FactoryMethod模式
Prototype模式
Singleton模式
Template Method模式
Builder模式
Class-Adapter 模式
Facade 模式
Object-Adapter 模式
Bridge模式
 
 

Prototype模式
 
来源:火龙果软件   作者:俎涛
 
1290 次浏览
48次  
问题 创建多个对象,这些对象大同小异,全新创建造成创建耗时耗力
环境 对象属于同类,不同实例的大部分的属性相同,只有小部分有差异。
解决方案 把一个实例当作原型实例,采用复制原型实例的方式快速创建,并把差异的部分修改。这样效率更高。

例如:轨道(track)和灯(light)的属性基本一样,要创建多个轨道和灯,则采用克隆方法,克隆大部分属性。轨道和灯具有类似的属性和方法,则抽象成设备(device ),这样可以提供统一的对外接口。

效果 克隆对象的方式更简单,创建效率更高。
实例 多个轨道和灯的克隆创建

结构类图

模式范例

下面对模式给出范例,包括:

  • 需求,采用用例模型描述
  • 设计,包括:设计类图,顺序图。
  • 实现:代码目录。
  • 运行:运行效果界面

用例模型

用例名称 初始化设备并工作
用例概述 1. 创建 轨道1,
2. 设置轨道1的属性:宽、高、重量,
3. 轨道1 启动;
4. 克隆轨道1为轨道2 ,
5. 轨道2停止。
6. 创建 灯1,
7. 设置轨道1的属性:宽、高、重量,
8. 灯1启动,
9. 克隆灯1为灯2 ,
10. 等2停止。

设计类图

顺序图:

代码目录

主程序Program.Main()

/// <summary>

/// 通过复制,快速创建多个同类对象

/// </summary>

public class Program

{

    public static void Main()

    {

        Device track1 = new Track();

        track1.ID = "101";

        track1.width = 10;

        track1.height = 5;

        track1.weight = 30;

        track1.Start();

 

        Device track2 = track1.Clone();

        track2.ID = "102"; ;

        track2.Stop();

 

        Device light1 = new Light();

        light1.ID = "201";

        light1.width = 30;

        light1.height = 15;

        light1.weight = 40;

        light1.Start();

 

        Device light2 = light1.Clone();

        light2.ID = "202";

        light2.Stop();

    }

}

 

abstract public class Device

{

    private string id;

    public string ID

    {

        set { id = value; }

        get { return id; }

    }

 

    public int width;

    public int height;

    public int weight;

 

    private  string state;

    public string State

    {

        set { state = value; }

        get { return state; }

    }

 

    public abstract Device Clone();

 

 

    public abstract void Start();

    public abstract void Stop();

}

 

class Track : Device

{

    public override Device Clone()

    {

        return (Device)this.MemberwiseClone();

    }

 

    public override void Start()

    {

        State = "free";

        Console.WriteLine("Track: {0} ,width={1} , height={2}, weight={3},state ={4}", ID, width, height, weight, State);

    }

 

    public override void Stop()

    {

        State = "occupy";

        Console.WriteLine("Track: {0} ,width={1} , height={2}, weight={3},state ={4}", ID, width, height, weight, State);

    }

}

class Light : Device

{

    public override Device Clone()

    {

        return (Device)this.MemberwiseClone();

    }

 

 

    public override void Start()

    {

        State = "on";

        Console.WriteLine("Light: {0} ,width={1} , height={2}, weight={3},state ={4}", ID, width, height, weight, State);

    }

 

    public override void Stop()

    {

        State = "off";

        Console.WriteLine("Light: {0} ,width={1} , height={2}, weight={3},state ={4}", ID, width, height, weight, State);

    }

}

运行效果

 


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1290 次浏览
48次
 捐助