パターンデコレーター

Decorator パターンは、構造設計パターンを指します。

デコレータは、クラスの機能を拡張するための継承の代替として使用されます。
製品の種類に応じてアプリケーションの機能を拡張するという課題があります。顧客は 3 種類の製品を必要としています。ベーシック、プロフェッショナル、究極。
基本–文字数をカウントする、Professional –機能 Basic + テキストを大文字で印刷します。Ultimate – Basic + Professional + 究極というテキストを印刷します。
継承を使用して実装します。

protocol Feature {
	func textOperation(text: String)
}

class BasicVersionFeature: Feature {
	func textOperation(text: String) {
		print("\(text.count)")
	}
}

class ProfessionalVersionFeature: BasicVersionFeature {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("\(text.uppercased())")
	}
}

class UltimateVersionFeature: ProfessionalVersionFeature {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("ULTIMATE: \(text)")
	}
}

let textToFormat = "Hello Decorator"

let basicProduct = BasicVersionFeature()
basicProduct.textOperation(text: textToFormat)

let professionalProduct = ProfessionalVersionFeature()
professionalProduct.textOperation(text: textToFormat)

let ultimateProduct = UltimateVersionFeature()
ultimateProduct.textOperation(text: textToFormat)

ここで、製品「Ultimate Light」– を実装する必要があります。 Basic + Ultimate ですが、Professional バージョンの機能はありません。最初の OH! が起こるからです。このような単純なタスク用に別のクラスを作成し、コードを複製する必要があります。
継承を使用して実装を続けましょう:

protocol Feature {
	func textOperation(text: String)
}

class BasicVersionFeature: Feature {
	func textOperation(text: String) {
		print("\(text.count)")
	}
}

class ProfessionalVersionFeature: BasicVersionFeature {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("\(text.uppercased())")
	}
}

class UltimateVersionFeature: ProfessionalVersionFeature {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("ULTIMATE: \(text)")
	}
}

class UltimateLightVersionFeature: BasicVersionFeature {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("ULTIMATE: \(text)")	
	}
}

let textToFormat = "Hello Decorator"

let basicProduct = BasicVersionFeature()
basicProduct.textOperation(text: textToFormat)

let professionalProduct = ProfessionalVersionFeature()
professionalProduct.textOperation(text: textToFormat)

let ultimateProduct = UltimateVersionFeature()
ultimateProduct.textOperation(text: textToFormat)

let ultimateLightProduct = UltimateLightVersionFeature()
ultimateLightProduct.textOperation(text: textToFormat)

わかりやすくするためにこの例をさらに発展させることもできますが、継承ベースに基づいてシステムをサポートする複雑さは今でも明らかです –面倒で柔軟性に欠け
ます。デコレータは機能を説明する一連のプロトコルであり、機能を拡張するデコレータ クラスの子具象インスタンスへの参照を含む抽象クラスです。
パターンを使用して上記の例を書き直してみましょう。

protocol Feature {
	func textOperation(text: String)
}

class FeatureDecorator: Feature {
	private var feature: Feature?
	
	init(feature: Feature? = nil) {
		self.feature = feature
	}
	
	func textOperation(text: String) {
		feature?.textOperation(text: text)
	}
}

class BasicVersionFeature: FeatureDecorator {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("\(text.count)")
	}
}

class ProfessionalVersionFeature: FeatureDecorator {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("\(text.uppercased())")
	}
}

class UltimateVersionFeature: FeatureDecorator {
	override func textOperation(text: String) {
		super.textOperation(text: text)
		print("ULTIMATE: \(text)")
	}
}

let textToFormat = "Hello Decorator"

let basicProduct = BasicVersionFeature(feature: UltimateVersionFeature())
basicProduct.textOperation(text: textToFormat)

let professionalProduct = ProfessionalVersionFeature(feature: UltimateVersionFeature())
professionalProduct.textOperation(text: textToFormat)

let ultimateProduct = BasicVersionFeature(feature: UltimateVersionFeature(feature: ProfessionalVersionFeature()))
ultimateProduct.textOperation(text: textToFormat)

let ultimateLightProduct = BasicVersionFeature(feature: UltimateVersionFeature())
ultimateLightProduct.textOperation(text: textToFormat)

これで、あらゆる種類の製品のバリエーションを作成できるようになりました –アプリケーションの起動段階で結合タイプを初期化するだけで十分です。以下の例は、Ultimate + Professional バージョンの作成です。

ultimateProfessionalProduct.textOperation(text: textToFormat)

ソース

https://refactoring.guru/ru/design-patterns/decorator

ソースコード

https://gitlab.com/demensdeum/patterns

Leave a Comment

Your email address will not be published. Required fields are marked *