日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

java设计模式之五(原型模式)

發布時間:2024/1/18 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java设计模式之五(原型模式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是原型模式?

原型模式(Prototype Pattern)是用于創建重復的對象,同時又能保證性能。這種類型的設計模式屬于創建型模式,它提供了一種創建對象的最佳方式。

當我們程序中有幾個相似但又不太一樣的對象時,就可以使用原型模式。簡單一些說的話,就是通過深拷貝的方法用原型實例指定創建對象的種類

Java 自帶的原型模式基于內存二進制流的復制,在性能上比直接 new 一個對象更加優良。

圖示

我們可以舉個例子,比如現在有三個形狀對象,圓形(Circle),正方形(Square),長方形(Rectangle)。現在我們想要通過原型模式創建這三個對象,那就需要有一個原型實例 :形狀(Shape),讓它實現java的接口類Cloneable,并且重寫克隆方法clone()。

然后我們定義一個類 ShapeCache,該類把 shape 對象存儲在一個 Hashtable 中,并在請求的時候返回它們的克隆。最后通過PrototypePatternDemo 類使用 ShapeCache 類來獲取 Shape 對象。

實現

步驟1

//定義原型類,實現java接口 public abstract class Shape implements Cloneable{private String id;protected String type;//需要子類實現的方法abstract void draw();public String getId() {return id;}public void setId(String id) {this.id = id;}public String getType() {return type;}//重寫克隆類public Object clone(){Object clone = null;try{clone = super.clone();} catch (CloneNotSupportedException e){e.printStackTrace();}return clone;}}

步驟2
實現具體對象 圓形(Circle),正方形(Square),長方形(Rectangle)

public class Circle extends Shape{public Circle(){type = "Circle";}@Overridevoid draw() {System.out.println("Inside Circle::draw() method");} } public class Square extends Shape{public Square(){type = "Square";}@Overridevoid draw() {System.out.println("Inside Square::draw() method");} } public class Rectangle extends Shape{public Rectangle(){type = "Rectangle";}@Overridevoid draw() {System.out.println("Inside Rectangle::draw() method");} }

步驟3
通過調用原型對象克隆方法,創建指定對象

import java.util.Hashtable;public class ShapeCache {private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();public static Shape getShape(String shapeId){Shape shapeCache = shapeMap.get(shapeId);return (Shape)shapeCache.clone();}public static void loadCache(){Circle circle = new Circle();circle.setId("1");shapeMap.put(circle.getId(), circle);Square square = new Square();square.setId("2");shapeMap.put(square.getId(), square);Rectangle rectangle = new Rectangle();rectangle.setId("3");shapeMap.put(rectangle.getId(), rectangle);} }

步驟4
main()方法檢查結果

public class PrototypePattenDemo {public static void main(String[] args) {ShapeCache.loadCache();Shape cloneShape1 = (Shape)ShapeCache.getShape("1");System.out.println("Shape: "+ cloneShape1.getType());System.out.println("Shape: "+ cloneShape1.getId());cloneShape1.draw();System.out.println("------------------------------");Shape cloneShape2 = (Shape)ShapeCache.getShape("2");System.out.println("Shape: "+ cloneShape2.getType());System.out.println("Shape: "+ cloneShape2.getId());cloneShape2.draw();System.out.println("------------------------------");Shape cloneShape3 = (Shape)ShapeCache.getShape("3");System.out.println("Shape: "+ cloneShape3.getType());System.out.println("Shape: "+ cloneShape3.getId());cloneShape3.draw();System.out.println("------------------------------");} }

總結

以上是生活随笔為你收集整理的java设计模式之五(原型模式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。