建設(shè)企業(yè)高端網(wǎng)站/電商引流推廣方法
1.單例模式
單例模式主要用于某個(gè)類有且只能用一個(gè)對(duì)象的場(chǎng)景,單例模式下不能外部實(shí)例化對(duì)象,由類內(nèi)部自行私有化實(shí)例對(duì)象并提供一個(gè)可以獲得該對(duì)象的方法。單例模式主要有餓漢模式(安全,但在編譯時(shí)就會(huì)自動(dòng)創(chuàng)建對(duì)象,即使不使用對(duì)象也會(huì)存在,會(huì)造成一定資源的浪費(fèi))、懶漢模式(安全,通過(guò)synchroized鎖實(shí)現(xiàn),每次獲取對(duì)象時(shí)都會(huì)加鎖,會(huì)對(duì)性能有一定影響)、DCL模式(基本安全,通過(guò)synchroized鎖實(shí)現(xiàn),在獲取對(duì)象的方法內(nèi)部加鎖,當(dāng)已經(jīng)實(shí)例化對(duì)象時(shí)不會(huì)加鎖直接返回對(duì)象,但在極少數(shù)情況下會(huì)出現(xiàn)線程不安全的情況)、靜態(tài)內(nèi)部類模式(安全,在類內(nèi)定義一個(gè)靜態(tài)的私有化的類來(lái)實(shí)例化對(duì)象)。
餓漢模式:
public class CEO {private static final mCEO = new CEO();//私有化實(shí)例化對(duì)象private CEO() { }//私有化構(gòu)造方法public static CEO getCEO() {return mCEO;}//提供獲取對(duì)象的接口
}
懶漢模式:
public class CEO {private static CEO mCEO;private CEO() { }public static synchroized CEO getCEO() {if(mCEO == null) {mCEO = new CEO(); }return mCEO;}//synchroized,加同步鎖,保證唯一;
}DCL模式:
public class CEO {private static CEO mCEO = null;private CEO() { }public static CEO getCEO() {if(mCEO == null) {synchroized(CEO.class) {if(mCEO == null) {mCEO = new CEO();}}}return mCEO;}
}靜態(tài)內(nèi)部類模式:當(dāng)外部類被加載時(shí),靜態(tài)內(nèi)部類不會(huì)立即加載,只有在第一次調(diào)用內(nèi)部類的靜態(tài)成員或方法時(shí)才會(huì)加載,這樣實(shí)現(xiàn)了懶加載的效果。同時(shí),類加載過(guò)程是線程安全的,因此靜態(tài)內(nèi)部類單例模式也是線程安全的
public class CEO {private CEO() { }public static CEO getCEO() {return CEOInstance.mCEO;}//靜態(tài)內(nèi)部類private static class CEOInstance {private static final CEOInstance mCEO = new CEO();}
}
2.構(gòu)建者模式
用于創(chuàng)建復(fù)雜對(duì)象,將對(duì)象的創(chuàng)建與表示分離、按步奏創(chuàng)建對(duì)象,通常包含以下角色。
產(chǎn)品(Product)——要?jiǎng)?chuàng)建的復(fù)雜對(duì)象,一般包含多種屬性;
抽象構(gòu)建者(Abstract Builder)——?jiǎng)?chuàng)建產(chǎn)品的各個(gè)抽象方法,以及返回最終產(chǎn)品的方法;
具體構(gòu)建者(Concrete Builder)——負(fù)責(zé)產(chǎn)品的具體創(chuàng)建,以及最終產(chǎn)品的返回;
指導(dǎo)者(Director)——使用構(gòu)建者構(gòu)建產(chǎn)品;
public class Product {private String partA;private Int partB;private Double partC;public get();//相應(yīng)參數(shù)的get方法public set();//相應(yīng)參數(shù)的set方法
}
public interface Builder {void buildPartA(String part);void buildPartA(Int part);void buildPartA(Double part);Product getProduct();
}
public ConcreteBuilder implements Builder {private Product product;public ConcreteBuilder() {product = new Product();}@Overridepublic void buildPartA(String part) {product.setPartA(part);}@Overridepublic void buildPartA(Int part) {product.setPartB(part);}@Overridepublic void buildPartA(Double part) {product.setPartC(part);}@Overridepublic Product getProduct() {return product;}
}
public class Director {private Builder builder;public void setBuilder(Builder builder) {this.builder = builder;}public Product concreteProduct() {builder.builderPartA("partA");builder.builderPartB(45);builder.builderPartC(1.44);return builder.getProduct();}
}
//實(shí)際使用
ConcreteBuilder builder = new ConcreteBuilder();
Director director = new Director();
director.setBuilder(builder);
Product product = director.concreteProduct();
3.工廠模式
特點(diǎn):向上轉(zhuǎn)換思想;不直接new對(duì)象,子類具體決定實(shí)例化什么對(duì)象;
應(yīng)用:需要生成復(fù)雜對(duì)象的地方;
角色:抽象工廠、具體工廠、抽象產(chǎn)品、具體產(chǎn)品;
通用模式寫(xiě)法:
抽象工廠——定義一個(gè)返回抽象產(chǎn)品的抽象方法,該方法負(fù)責(zé)構(gòu)造產(chǎn)品;
具體工廠——重寫(xiě)構(gòu)造產(chǎn)品的方法,返回對(duì)象為具體產(chǎn)品;
抽象產(chǎn)品——定義一個(gè)抽象方法,該方法與產(chǎn)品關(guān)聯(lián);
具體產(chǎn)品——根據(jù)具體產(chǎn)品重寫(xiě)父類中與產(chǎn)品關(guān)聯(lián)的抽象方法;
//抽象產(chǎn)品
public abstract class Product{public abstract void fuction();
}
//具體產(chǎn)品A
public class ProductA extends Product{@Override public void function() {system.out.println("產(chǎn)品A的功能是...");}
}
//具體產(chǎn)品B
public class ProductB extends Product{@Override public void function() {system.out.println("產(chǎn)品B的功能是...");}
}//抽象工廠寫(xiě)法
public abstract class Factory{public abstract Product createProduct();
}
//具體工廠寫(xiě)法
public ConcreteFactoryA extends Factory {@Overridepublic ProductA createProduct() {return new ProductA();}
}
public ConcreteFactoryB extends Factory {@Overridepublic ProductB createProduct() {return new ProductB();}
}
//客戶or測(cè)試
public class Client{public static void main(String[] args) {Factory factory = new ConcerateFactoryA(); Product product = factory.createProduct();product.fuction();}
}