创建型设计模式

创建型设计模式

创建型设计模式是一类处理对象创建的设计模式,通过某种方式控制对象的创建来避免基本对象创建时可能导致设计上的问题或增加设计上的复杂度。

简单工厂模式(Simple Factory):

又名静态工厂方法,由一个工厂对象决定创建某一种产品对象类的实例。主要用来创建同一类对象。

通过类实例化对象创建

var LoginAlert = function(text) { this.content = text; }; LoginAlert.prototype.show = function() { // 显示警告信息 }; var LoginConfirm = function(text) { this.content = text; }; LoginConfirm.prototype.show = function() { // 显示确认信息 }; var LoginPrompt = function(text) { this.content = text; }; LoginPrompt.prototype.show = function() { // 显示提示信息 }; var PopFactory = function(name) { switch (name) { case "alert": return new LoginAlert(); case "confirm": return new LoginConfirm(); case "prompt": return new LoginPrompt(); } };

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdxpz.html