如果在游戏中,需要大量的古典风格或现代风格的墙或屋子,这时可以通过拷贝一个已有的原型对象来生成新对象,就是一个原型模式的例子了。通过克隆来解决“易变对象”的创建问题。
究竟选用哪一种模式最好取决于很多的因素。使用Abstract Factory、Prototype Pattern或Builder Pattern的设计比使用Factory Method的设计更加灵活,但是也更加复杂,尤其Abstract Factory需要庞大的工厂类来支持。通常,设计以使用Factory Method开始,并且当设计者发现需要更大的灵活性时,设计便会向其他设计模式演化,当你在多个设计模式之间进行权衡的时候,了解多个设计模式可以给你提供更多的选择余地。using System;
![]()
![]()
![]()
public abstract class RoomPrototype
![]()
{
![]()
public abstract RoomPrototype Clone();
![]()
}
![]()
![]()
![]()
public class ModernPrototype:RoomPrototype
![]()
{
![]()
public override RoomPrototype Clone()
![]()
{
![]()
return (RoomPrototype)this.MemberwiseClone();
![]()
}
![]()
}
![]()
![]()
![]()
public class ClassicalPrototype:RoomPrototype
![]()
{
![]()
public override RoomPrototype Clone()
![]()
{
![]()
return (RoomPrototype)this.MemberwiseClone();
![]()
}
![]()
}
![]()
总结
使用创建者模式是为了提高系统的可维护性和可扩展性,提高应对需求变化的能力!
参考文献:
《设计模式中文版》
《DesignPatternsExplained》
idior 的《你了解创建者模式了吗? --- 创建者模式详解 》