设计模式之工厂模式

工厂模式

工厂模式定义创建对象的接口,但是让子类去真正的实例化。也就是工厂方法将类的实例化延迟到子类

工厂模式对比其它方式(构造函数)

  1. 工厂模式类集中了所有对象的创建,便于对象的创建的统一管理
  2. 对象的使用者仅仅是使用产品,实现了单一职责
  3. 便于扩展,如果新增了一种业务,只需增加相关的业务对象类和工厂类的生产业务对象的方法,不需要修改其他的地方
  4. 确实违反了开闭原则

大家都玩过飞机大战吧,飞机种类太多太多了,其实构造函数去做还不太理想,用工厂模式还是不错,但是它有一个弊端,违反了开闭原则

       //小飞机
       function SmallPlane(){
           this.height = 100
           this.width = 100
           this.name = 'SmallPlane'
           this.blod = 100
           this.touch = function(){
               this.blood -= 100
               if(this.blod==0){
                   console.log('die')
               }
           }
       } 

       //大飞机 
       function BigPlane(){
           this.height = 200
           this.width = 200
           this.name = 'BigPlane'
           this.bold = 200
           this.touch = function(){
               this.bold -= 100
               if(this.bold == 0){
                   console.log('die')
               }
           }
       } 

       //攻击飞机
       function AtackPlane(){
           this.height = 125;
           this.width = 100
           this.blod =100
           this.name = 'AtackPlane'
           this.touch = function () {
               this.bold -= 100
               if (this.bold == 0){
                   console.log('die')
               }
           }
           this.attack = function(){
               console.log('bong')
           }
       }

       //工厂模式
       function PlaneFactor(type){
           var newPlane = null
           switch(type){
               case 'SmallPlane':
               newPlane= new SmallPlane()
               break;
               case 'BigPlane':
               newPlane = new BigPlane()
               break;
               case 'AtackPlane':
               newPlane = new AtackPlane()
               break;
           }
           newPlane.die = function (){
               console.log('biu')
           }
           return newPlane;
       }
 

工厂方法模式

不再有一个唯一的工厂类就创建产品,而是将不同的产品交给对应的工厂类去实现。每个产品由负责生产的子工厂来创造。如果添加新的产品,需要做的是添加新的子工厂和产品,而不需要修改其他的工厂代码。

工厂方法模式组成:

  1. 抽象工厂类:负责定义创建产品的公共接口。
  2. 产品子工厂:继承抽象工厂类,实现抽象工厂类提供的接口。
  3. 每一种产品有各自的产品类。

代码比简单工厂模式复杂了,引入了抽象层,还有子工厂,这会增加代码的复杂度和理解难度。但是相比简单工厂模式,代码的维护性和扩展性提高了,新增产品时,只需要增加对应的产品类和产品工厂类,不需要修改到抽象工厂类和其他工厂。更加符合面向对象的开闭原则

        //工厂方法模式
        function PlaneFactor() {

        }

        //子类工厂都可以使用的公共方法
        PlaneFactor.prototype.touch = function () {
            this.blod -= 50;
            if (this.blod == 0) {
                console.log('die')
            }
        }

        PlaneFactor.prototype.die = function(){
            console.log('bong')
        }

        //创建对象的接口
        PlaneFactor.create = function (type) {
            //是否存在改类型的子类工厂
            if (PlaneFactor.prototype[type] == undefined) {
                throw 'no this constructor'
            }
            //继承
            if (PlaneFactor.prototype[type].prototype.__proto__ != PlaneFactor.prototype) {
                PlaneFactor.prototype[type].prototype = new PlaneFactor()
            }
            var arg = [].slice.call(arguments,1)

            var newPlane = new PlaneFactor.prototype[type](...arg)
            return newPlane;
        }

        //真正定义子类工厂
        PlaneFactor.prototype.SmallPlane = function (x, y) {
            this.x = x;
            this.y = y;
            this.height = 100
            this.width = 100
            this.name = 'SmallPlane'
            this.blod = 100
        }
        PlaneFactor.prototype.BigPlane = function (x,y) {
            this.x = x;
            this.y = y;
            this.height = 200
            this.width = 200
            this.name = 'BigPlane'
            this.blold = 200
        }
        PlaneFactor.prototype.AtackPlane = function (x,y) {
            this.x = x;
            this.y = y;
            this.height = 125;
            this.width = 100
            this.blod = 100
            this.name = 'AtackPlane'
            this.attack = function () {
                console.log('biu')
            }
        }
        var a = PlaneFactor.create('AtackPlane',10,20)
        var b = PlaneFactor.create('BigPlane',20,20)
        var c = PlaneFactor.create('SmallPlane',40,20)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值