首页 > 代码库 > js24---工厂模式2

js24---工厂模式2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <!-- 引入的核心js公共文件 -->
        <script type=text/javascript charset=utf-8 src=../commons/../commons/CommonUtil.js></script>
        <script type=text/javascript charset=utf-8>
            
            //分发到不同的4S店(类似于java的抽象类)
            function CarShop(){};
            CarShop.prototype = {
                constructor : CarShop ,
                sellCar:function(type){
                    // 销售人员...
                    //var car = CarFactory.createCar(type);
                    // 保险、相关的售后服务
                    //return car ;
                    this.abstractSellCar(type);
                } ,
                abstractSellCar: function(){
                    throw new Error(this method is abstract...);
                }
            };
            
            // Benz 4s 店 
            function BenzCarShop(){};
            BH.extend(BenzCarShop,CarShop);
            BenzCarShop.prototype = {
                constructor:BenzCarShop ,
                sellCar:function(type){
                    var car ; 
                    var types = [Benz];    //输入参数校验
                    for(t in types){
                        // 如果我的商店里有你想要汽车型号
                        if(types[t] === type){
                            car = CarFactory.createCar(type);
                        } else { 
                            alert(没有你要的型号!);
                        }
                    }
                    return car ;
                }
            };
            
            // 宝马  4s 店 
            function BmwCarShop(){};
            BH.extend(BmwCarShop,CarShop);
            BmwCarShop.prototype = {
                constructor:BmwCarShop ,
                sellCar:function(type){
                    var car ;
                    var types = [Bmw];    //输入参数校验
                    for(t in types){
                        // 如果我的商店里有你想要汽车型号
                        if(types[t] === type){
                            car = CarFactory.createCar(type);
                        } else { 
                            alert(没有你要的型号!);
                        }
                    }
                    return car ;
                }                    
            };        
            
                
            
            //生产车的工厂 目的就是为了生产车
            //单体模式 动态工厂模式
            var CarFactory = {
                createCar:function(type){
                    // var car = new Benz();,不需要做switch判断了
                    var car = eval(new +type+());
                    //检验接口实现
                    BH.Interface.ensureImplements(car ,CarInterface);
                    return car ;                        
                }
            };
            
            
            
            
            
            // 接口对象的实例
            var CarInterface = new BH.Interface(CarInterface ,[start,run]);
            
            // SuperClass  implements CarInterface 
            function BaseCar(){};
            BaseCar.prototype = {
                constructor:BaseCar , 
                start:function(){
                    alert(this.constructor.name +  ..start);
                },
                run:function(){
                    alert(this.constructor.name +  ..run);
                }
            };
            
            function Benz(){}; 
            BH.extend(Benz,BaseCar); 
            Benz.prototype.driveBenz = function(){alert(Benz..drive)}; 
            //Benz.prototype.run = function(){alert(‘Benz..run‘)};
            
            function Bmw(){};
            BH.extend(Bmw,BaseCar);
            Bmw.prototype.driveBmw = function(){alert(Bmw..drive)};
            //Bmw.prototype.run = function(){alert(‘Bmw..run‘)};    
            
            function Audi(){};
            BH.extend(Audi,BaseCar);
            Audi.prototype.driveAudi = function(){alert(Audi..drive)};
            //Audi.prototype.run = function(){alert(‘Audi..run‘)};    
            
            
            var shop = new CarShop();        
            var car = shop.sellCar(Benz);
            car.start();
            car.run();
            car.driveBenz();

            var car2 = shop.sellCar(Bmw);
            car2.start();
            car2.run();    
            car2.driveBmw();    
            
            
            
            
            
            
            var shop1 = new BenzCarShop();
            var car1 = shop1.sellCar(Benz);
            car1.run();
            
            var shop2 = new BmwCarShop();
            var car2 = shop2.sellCar(Bmw);
            car2.run();
            
            
        </script>
    </head>
    <body>
    </body>
</html>

 

js24---工厂模式2