quarta-feira, 9 de março de 2022

 Powershell Design Patterns Abstract Factory


class AbstractFactory {
CreateProductA() {}
CreateProductB() {}
}
class ConcreteFactory1 : AbstractFactory {
[AbstractProductA] CreateProductA() {
return [ConcreteProductA1]::new()
}
[AbstractProductB] CreateProductB() {
return [ConcreteProductB1]::new()
}
}
class ConcreteFactory2 : AbstractFactory {
[AbstractProductA] CreateProductA() {
return [ConcreteProductA2]::new()
}
[AbstractProductB] CreateProductB() {
return [ConcreteProductB2]::new()
}
}
class AbstractProductA {
UsefulFunctionA() {}
}
class ConcreteProductA1 : AbstractProductA {
[string] UsefulFunctionA() {
return "The result of the product A1."
}
}
class ConcreteProductA2 : AbstractProductA {
[string] UsefulFunctionA() {
return "The result of the product A2."
}
}
class AbstractProductB {
UsefulFunctionB() {}
AnotherUsefulFunctionB([AbstractProductA] $collaborator) {}
}
class ConcreteProductB1 : AbstractProductB {
[string] UsefulFunctionB() {
return "The result of the product B1."
}
[string] AnotherUsefulFunctionB([AbstractProductA] $collaborator) {
$result = $collaborator.UsefulFunctionA()
return "The result of the B1 collaborating with the ({0})" -f $result
}
}
class ConcreteProductB2 : AbstractProductB {
[string] UsefulFunctionB() {
return "The result of the product B2."
}
[string] AnotherUsefulFunctionB([AbstractProductA] $collaborator) {
$result = $collaborator.UsefulFunctionA()
return "The result of the B2 collaborating with the ({0})" -f $result
}
}
class Client {

[void] ClientMethod([AbstractFactory] $factory) {
$productA = $factory.CreateProductA()
$productB = $factory.CreateProductB()
Write-Host $productB.UsefulFunctionB()
Write-Host $productB.AnotherUsefulFunctionB($productA)
}
[void] Main() {
Write-Host "Client: Testing client code with the first factory type..."
$this.ClientMethod([ConcreteFactory1]::new())
Write-Host
Write-Host "Client: Testing the same client code with the second factory type..."
$this.ClientMethod([ConcreteFactory2]::new())
}
}
class Program {
Main() {
[Client]::new().Main()
}
}
[Program]::new().Main()

Nenhum comentário:

Postar um comentário

Decorator Powershell Design Patterns

 Powershell Design Patterns Decorator ```powershell # Classe base 'Beverage' class Beverage {     [string]$description = "Unkno...