设计模式 - 建造者模式
设计模式 - 适配器

设计模式 - 原型模式

violet posted @ Jul 07, 2020 07:39:31 AM in 笔记 with tags Design Pattern , 202 阅读

原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。

意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

主要解决:在运行期建立和删除原型。

何时使用: 1、当一个系统应该独立于它的产品创建,构成和表示时。 2、当要实例化的类是在运行时刻指定时,例如,通过动态装载。 3、为了避免创建一个与产品类层次平行的工厂类层次时。 4、当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。

如何解决:利用已有的一个原型对象,快速地生成和原型对象一样的实例。

关键代码: 1、实现克隆操作,在 JAVA 继承 Cloneable,重写 clone(),在 .NET 中可以使用 Object 类的 MemberwiseClone() 方法来实现对象的浅拷贝或通过序列化的方式来实现深拷贝。 2、原型模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些"易变类"拥有稳定的接口。

优点: 1、性能提高。 2、逃避构造函数的约束。

缺点: 1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。 2、必须实现 Cloneable 接口。

 

package main

import "fmt"

type Shape struct {
	ID   int
	Type string
}

func (s *Shape) Draw() string {
	return s.Type
}

func (s *Shape) GetID() int {
	return s.ID
}

func (s *Shape) SetID(ID int) {
	s.ID = ID
}

func Clone(input *Shape) *Shape {
	return &Shape{
		ID:   input.ID,
		Type: input.Type,
	}
}

type Rectangle struct {
	Shape *Shape
}

func NewRectangle() *Rectangle {
	return &Rectangle{
		Shape: &Shape{
			Type: "Rectangle",
		},
	}
}

func (r *Rectangle) Draw() {
	fmt.Println("draw Rectangle")
}

type Square struct {
	Shape *Shape
}

func NewSquare() *Square {
	return &Square{
		Shape: &Shape{
			Type: "Square",
		},
	}
}

func (s *Square) Draw() {
	fmt.Println("draw Square")
}

type Circle struct {
	Shape *Shape
}

func NewCircle() *Circle {
	return &Circle{
		Shape: &Shape{
			Type: "Circle",
		},
	}
}

func (c *Circle) Draw() {
	fmt.Println("draw Circle")
}

func LoadCache() map[string]interface{} {
	hash := map[string]interface{}{}
	circle := NewCircle()
	circle.Shape.SetID(1)
	hash["circle"] = circle

	rectangle := NewRectangle()
	rectangle.Shape.SetID(2)
	hash["rectangle"] = rectangle

	square := NewSquare()
	square.Shape.SetID(3)
	hash["square"] = square

	return hash
}

func GetShape(shape interface{}) interface{} {
	switch v := shape.(type) {
	case *Circle:
		return &Circle{Shape: Clone(v.Shape)}
	case *Rectangle:
		return &Rectangle{Shape: Clone(v.Shape)}
	case *Square:
		return &Square{Shape: Clone(v.Shape)}
	}
	return nil
}

func main() {
	cache := LoadCache()
	GetShape(cache["circle"]).(*Circle).Draw()
	GetShape(cache["rectangle"]).(*Rectangle).Draw()
	GetShape(cache["square"]).(*Square).Draw()
}
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter