一、创建型模式
1.单例模式
概念:
确保一个类仅有一个实例,并提供一个方法暴露出该实例供外面使用,避免重复创建带来的资源浪费。
实例:
// 懒汉式
class Singleton {
private static singleton: Singleton | null = null;
/**
* 私有构造函数,防止外部实例化对象
*/
private constructor() {
}
public static getInstance(): Singleton {
if (Singleton.singleton === null) {
return new Singleton()
} else {
return Singleton.singleton;
}
}
}
// 饿汉式
class Singleton {
private static singleton: Singleton = new Singleton();
/**
* 私有构造函数,防止外部实例化对象
*/
private constructor() {
}
public static getInstance(): Singleton {
return Singleton.singleton
}
}
2.工厂方法模式(违背了闭包原则)
概念:
建立一个工厂类,对实现同一接口的类(或者继承同一个抽象类)进行实例的创建。将对象的创建和使用分离。
实例:
interface Chat {
Method(): void
}
class WX implements Chat {
Method(): void {
console.log('微信聊天呐')
}
}
class QQ implements Chat {
Method(): void {
console.log('QQ聊天呐')
}
}
class ChatFactory {
public produce(type: string): Chat | null {
if ("wx".localeCompare(type)) {
return new WX()
} else if ("QQ".localeCompare(type)) {
return new QQ()
} else {
console.log('逻辑完善处理')
return null
}
}
}
3.抽象工厂模式
概念:
每一个新功能对应一个工厂,解决了工厂的问题,增加新功能的时候,增加新工厂就行。
实例:
class FactoryProducer {
static getFactory(type: string): AbstractFactory|null {
if (type === 'WX') {
return new ChatToolFactory()
} else if (type === 'QQ') {
return new ChatMethodFactory()
}
return null
}
}
// 抽象工厂
abstract class AbstractFactory {
abstract getChatTool(tool: string): ChatTools | null
abstract getChatMethod(method: string): ChatMethods | null
}
class ChatToolFactory extends AbstractFactory {
getChatTool(tool: string): ChatTools | null {
if (tool.localeCompare('WX')) {
return new WX()
} else if (tool.localeCompare('QQ')) {
return new QQ()
} else {
return null
}
}
getChatMethod(method:string): ChatMethods|null {
return null
}
}
class ChatMethodFactory extends AbstractFactory {
getChatTool(tool:string): ChatTools|null {
return null
}
getChatMethod(method:string): ChatMethods|null {
if (method.localeCompare('Video')) {
return new Video()
}else if(method.localeCompare('Voice')){
return new Voice()
}else {
return null
}
}
}
interface ChatTools {
tool(): void
}
class WX implements ChatTools {
tool(): void {
console.log('微信')
}
}
class QQ implements ChatTools {
tool(): void {
console.log('QQ')
}
}
interface ChatMethods {
method(): void
}
class Video implements ChatMethods {
method(): void {
console.log('视频')
}
}
class Voice implements ChatMethods {
method(): void {
console.log('语音')
}
}
4.建造者模式
概念:
逐步建造一个复杂对象,而不是一次性建造
实例:
class Car {
brand: string = ''
model: string = ''
color: string = ''
public setBrand(brand: string) {
this.brand = brand
}
public setModel(model: string) {
this.model = model
}
public setColor(color: string) {
this.color = color
}
}
class createCarBuilder {
private car: Car = new Car()
constructor() {
}
build(): Car {
return this.car
}
setBrand(brand: string) {
this.car.setBrand(brand)
}
setModel(model: string) {
this.car.setModel(model)
}
setColor(color: string) {
this.car.setColor(color)
}
}
class CarDirector {
private carBuilder: createCarBuilder | null = null
constructor(carBuilder: createCarBuilder) {
this.carBuilder = carBuilder
}
build(): Car | null {
if (this.carBuilder) {
this.carBuilder.setBrand('Benz')
this.carBuilder.setModel('C300')
this.carBuilder.setColor('Black')
return this.carBuilder.build()
}
return null
}
}