1. 程式人生 > >js23---工廠模式1

js23---工廠模式1

create 面向對象 utf-8 reat back comm spa var org

<!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> //既可以當成java的類用,也可以當成c++函數用 function CarShop(){};//賣車類,沒有工廠模式 CarShop.prototype = { constructor : CarShop , sellCar:
function(type){ var car ; // 聲明一個變量 switch(type){ case Benz: car = new Benz(); break; case Bmw : car = new Bmw(); break; case Audi: car = new Audi(); break; default: not buy it ; } BH.Interface.ensureImplements(car ,CarInterface);
//接口檢測,當函數用BH.Interface.ensureImplements = function(object){},java的接口檢測是虛擬機做的,js接口檢測是我們自己寫的。 return car ; } }; // 接口對象的實例,當類用BH.Interface = function(name,methods){} var CarInterface = new BH.Interface(CarInterface ,[start,run]); // SuperClass implements CarInterface function BaseCar(){}; BaseCar.prototype = {//父類方法都在原型對象中 constructor:BaseCar , start:function(){ alert(this.constructor.name + ..start);//this是調用方法的子類,java多態(通過父類方法裏面的this實現) }, run:function(){ alert(this.constructor.name + ..run); } }; function Benz(){}; BH.extend(Benz,BaseCar); //BH.extend=function(sub ,sup){},當函數用 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(); </script> </head> <body> </body> </html>
<!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>
            // 萬事萬物都是Object  : 賣車的商店 -> 賣車 -> 生產車 -> Factory
            
            function CarShop(){};// 商店
            CarShop.prototype = {
                constructor : CarShop ,
                sellCar:function(type){
                /*   面向對象   */
                    // 銷售人員...
                    var car = CarFactory.createCar(type);//工廠
                    // 保險、相關的售後服務
                    return car ;
                }
            };
            
            //工廠,單體模式
            var CarFactory = {
                    createCar:function(type){
                            var car ; // 聲明一個變量
                            switch(type){
                                case Benz: car = new Benz(); break;
                                case Bmw : car = new Bmw();  break;
                                case Audi: car = new Audi(); break;
                                default: not buy it ;
                            }
                            //檢驗接口 交給工廠
                            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);
                }
            };
            
            
            // Class benz bmw audi (都是車)
            // 註意關鍵問題:子類先繼承父類 子類在擴展子類自己特有的方法
            
            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();        
        </script>
    </head>
    <body>
    </body>
</html>

js23---工廠模式1