当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 设计模式系列 - 原型模式

设计模式系列 - 原型模式

2018年12月10日  | 移动技术网IT编程  | 我要评论
所谓原型模式是指为创建重复对象提供一种新的可能。 介绍 当面对系统资源紧缺的情况下,如果我们在重新创建一个新的完全一样的对象从某种意义上来讲是资源的浪费,因为在新对象的创建过程中,是会有系统资源的消耗,而为了尽可能的节省系统资源,我们有必要寻找一种新的方式来创建重复对象。 类图描述 由于 Shape ...

所谓原型模式是指为创建重复对象提供一种新的可能。

介绍

当面对系统资源紧缺的情况下,如果我们在重新创建一个新的完全一样的对象从某种意义上来讲是资源的浪费,因为在新对象的创建过程中,是会有系统资源的消耗,而为了尽可能的节省系统资源,我们有必要寻找一种新的方式来创建重复对象。

类图描述

由于shape 抽象类继承了 icloneable 接口,所以通过上图我们可以发现,所有具体的类型都继承 shape 抽象类,并实现 clone() 方法即可。

代码实现

1、定义具有抽象基类

public abstract class shape : icloneable
{
    private string id;
    protected string type;
    public abstract void draw();

    public string getid() => id;
    public string gettype() => type;

    public void setid(string id) => this.id = id;

    public new object memberwiseclone() => base.memberwiseclone();
    public abstract object clone();
}

2、定义具体类型

public class circle:shape
{
    public circle() => type = "circle";
    public override void draw()
    {
        console.writeline("i am a circle");
    }
    public override object clone()
    {
        circle obj = new circle();
        obj.type = type;
        obj.setid(this.getid());
        return obj;
    }
}

public class rectangle:shape
{
    public rectangle() => type = "rectangle";
    public override void draw()
    {

        console.writeline("i am a rectangle");
    }
    public override object clone()
    {
        rectangle obj = new rectangle();
        obj.type = type;
        obj.setid(this.getid());
        return obj;

    }
}

public class square:shape
{
    public square() => type = "square";
    public override void draw()
    {
        console.writeline("i am a square");
    }
    public override object clone()
    {
        square obj = new square();
        obj.type = type;
        obj.setid(this.getid());
        return obj;
    }
}

3、创建种子数据

public class shapecache
{
    private  static  hashset<shape> shapemap = new hashset<shape>();

    public static shape getshape(string shapeid)
    {
        var cachedshape = shapemap.firstordefault(p => p.getid() == shapeid);
        return (shape) cachedshape?.clone();
    }

    public static void loadcache()
    {
        circle circle = new circle();
        circle.setid("1");
        shapemap.add(circle);


        square square = new square();
        square.setid("2");
        shapemap.add(square);

        rectangle rectangle = new rectangle();
        rectangle.setid("3");
        shapemap.add(rectangle);
    }
}

4、上层调用

class program
{
    static void main(string[] args)
    {
        shapecache.loadcache();

        shape clonedshape1 = (shape) shapecache.getshape("1");
        console.writeline(clonedshape1.gettype());
        clonedshape1.draw();

        shape clonedshape2 = (shape)shapecache.getshape("2");
        console.writeline(clonedshape2.gettype());
        clonedshape2.draw();

        shape clonedshape3 = (shape)shapecache.getshape("3");
        console.writeline(clonedshape3.gettype());
        clonedshape3.draw();

        console.readkey();
    }
}

总结

c# 中实现原型模式的关键是需要定义一个继承 icloneable 接口的抽象类,并在子类中重写相应的 clone() 方法即可。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网