1. Diagrama de Classes
2. Participantes
Builder: especifica a interface abstrata para criação de partes de um objeto-builder.
BuilderConcreto: constrói as partes do produto, define e mantém a representação que cria.
Diretor: constrói um objeto apartir da interface do builder.
Produto: representa o objeto complexo que será construído.
3. Vantagens e Desvantagens
Permite variar a representação interna de um produto. O objeto builder fornece ao produtor/diretor apenas a sua abstração. Ou seja, o mesmo tem apenas a assinatura dos métodos para controlar a produção.
O cliente não precisa saber como é o processo interno do método. Apenas é de responsabilidade dele delegar aos métodos da abstração.
4. Padrões com a mesma finalidade
A diferença do padrão Builder para o Abstract Factory é o domínio da fabricação do objeto, pois cria o objeto final passo-a-passo.
5. Código
C#
using System;
using System.Collections.Generic;
using System.Text;
namespace BuilderTest
{
class Program
{
static void Main(string[] args)
{
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
director.Construct(b1);
BuilderTest.ConcreteBuilder2.Product p1 = b1.GetResult();
p1.Show();
Console.ReadKey();
}
}
class Director
{
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract BuilderTest.ConcreteBuilder2.Product GetResult();
}
class ConcreteBuilder1 : Builder
{
private BuilderTest.ConcreteBuilder2.Product _product = new BuilderTest.ConcreteBuilder2.Product();
public override void BuildPartA()
{
_product.Add("PartA");
}
public override void BuildPartB()
{
_product.Add("PartB");
}
public override BuilderTest.ConcreteBuilder2.Product GetResult()
{
return _product;
}
}
class ConcreteBuilder2 : Builder
{
private Product _product = new Product();
public override void BuildPartA()
{
_product.Add("PartX");
}
public override void BuildPartB()
{
_product.Add("PartY");
}
public override Product GetResult()
{
return _product;
}
public class Product
{
private IList_parts = new List ();
public void Add(string part)
{
_parts.Add(part);
}
public void Show()
{
Console.WriteLine("\n Product Parts");
foreach (string part in _parts)
{
Console.WriteLine(part);
}
}
}
}
}
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace BuilderTest
Class Program
Private Shared Sub Main(ByVal args As String())
Dim director As New Director()
Dim b1 As Builder = New ConcreteBuilder1()
Dim b2 As Builder = New ConcreteBuilder2()
director.Construct(b1)
Dim p1 As BuilderTest.ConcreteBuilder2.Product = b1.GetResult()
p1.Show()
Console.ReadKey()
End Sub
End Class
Class Director
Public Sub Construct(ByVal builder As Builder)
builder.BuildPartA()
builder.BuildPartB()
End Sub
End Class
MustInherit Class Builder
Public MustOverride Sub BuildPartA()
Public MustOverride Sub BuildPartB()
Public MustOverride Function GetResult() As BuilderTest.ConcreteBuilder2.Product
End Class
Class ConcreteBuilder1
Inherits Builder
Private _product As New BuilderTest.ConcreteBuilder2.Product()
Public Overloads Overrides Sub BuildPartA()
_product.Add("PartA")
End Sub
Public Overloads Overrides Sub BuildPartB()
_product.Add("PartB")
End Sub
Public Overloads Overrides Function GetResult() As BuilderTest.ConcreteBuilder2.Product
Return _product
End Function
End Class
Class ConcreteBuilder2
Inherits Builder
Private _product As New Product()
Public Overloads Overrides Sub BuildPartA()
_product.Add("PartX")
End Sub
Public Overloads Overrides Sub BuildPartB()
_product.Add("PartY")
End Sub
Public Overloads Overrides Function GetResult() As Product
Return _product
End Function
Public Class Product
Private _parts As IList(Of String) = New List(Of String)()
Public Sub Add(ByVal part As String)
_parts.Add(part)
End Sub
Public Sub Show()
Console.WriteLine(vbLf & " Product Parts")
For Each part As String In _parts
Console.WriteLine(part)
Next
End Sub
End Class
End Class
End Namespace
Nenhum comentário:
Postar um comentário