1. Diagrama de Classes
2. Desvantagens x Benefícios
• Por isolar as classes concretas, o cliente controla o componente apenas a partir da assinatura da abstração da classe.
• Facilidade na troca de família de objetos, uma vez que a família implementa a mesma abstração e a fábrica é montada apenas na instancialização do objeto.
• Dificuldade em extensão da família. Estender uma classe pode acarretar dificuldades, pois deve se alterar a assinatura da fábrica e todas as classes herdadas.
• Há um senso harmônico entre os objetos produzidos. Todos possuem a mesma característica de sua abstração. E ainda há a possibilidade de fabricação de objetos apenas uma vez de cada vez.
3. Código
C#
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryTest
{
class Program
{
static void Main(string[] args)
{
// Cria celulares da Companhia A.
CellphoneFactory factoryOne = new CompanyACellphoneFactory();
Industry industryOne = new Industry(factoryOne);
industryOne.Produce();
// Cria celulares da Companhia B.
CellphoneFactory factoryTwo = new CompanyBCellphoneFactory();
Industry industryTwo = new Industry(factoryTwo);
industryTwo.Produce();
Console.ReadKey();
}
}
#region ProductsFromACompany
public class CompanyATouchedScreenCellphone : AbstractCellphoneWithTouchedScreen
{
public override string ToString()
{
return "Um celular touch screen da empresa A";
}
}
public class CompanyARegularCellphone : AbstractRegularCellphone
{
public override string ToString()
{
return "Um celular comum da empresa A";
}
}
#endregion
#region ProductsFromBCompany
public class CompanyBTouchedScreenCellphone : AbstractCellphoneWithTouchedScreen
{
public override string ToString()
{
return "Cellphone touch screen da empresa B";
}
}
public class CompanyBRegularCellphone : AbstractRegularCellphone
{
public override string ToString()
{
return "Um celular comum da empresa B";
}
}
#endregion
#region Abstracoes dos Produtos
public abstract class AbstractCellphoneWithTouchedScreen
{
public abstract override string ToString();
}
public abstract class AbstractRegularCellphone
{
public abstract override string ToString();
}
#endregion
abstract class CellphoneFactory
{
public abstract AbstractCellphoneWithTouchedScreen CreateCellphoneA();
public abstract AbstractRegularCellphone CreateCellphoneB();
}
class CompanyACellphoneFactory : CellphoneFactory
{
public override AbstractCellphoneWithTouchedScreen CreateCellphoneA()
{
return new CompanyATouchedScreenCellphone();
}
public override AbstractRegularCellphone CreateCellphoneB()
{
return new CompanyARegularCellphone();
}
}
class CompanyBCellphoneFactory : CellphoneFactory
{
public override AbstractCellphoneWithTouchedScreen CreateCellphoneA()
{
return new CompanyBTouchedScreenCellphone();
}
public override AbstractRegularCellphone CreateCellphoneB()
{
return new CompanyBRegularCellphone();
}
}
class Industry
{
private AbstractCellphoneWithTouchedScreen cellphoneOne;
private AbstractRegularCellphone cellphoneTwo;
public Industry(CellphoneFactory cellphoneFactory)
{
this.cellphoneOne =cellphoneFactory.CreateCellphoneA();
this.cellphoneTwo = cellphoneFactory.CreateCellphoneB();
}
public void Produce()
{
Console.WriteLine("Construção do celular: {0}. ", this.cellphoneOne.ToString());
Console.WriteLine("Construção do celular: {0}. ", this.cellphoneTwo.ToString());
}
}
}
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace AbstractFactoryTest
Class Program
Private Shared Sub Main(ByVal args As String())
' Cria celulares da Companhia A.
Dim factoryOne As CellphoneFactory = New CompanyACellphoneFactory()
Dim industryOne As New Industry(factoryOne)
industryOne.Produce()
' Cria celulares da Companhia B.
Dim factoryTwo As CellphoneFactory = New CompanyBCellphoneFactory()
Dim industryTwo As New Industry(factoryTwo)
industryTwo.Produce()
Console.ReadKey()
End Sub
End Class
#Region "ProductsFromACompany"
Public Class CompanyATouchedScreenCellphone
Inherits AbstractCellphoneWithTouchedScreen
Public Overloads Overrides Function ToString() As String
Return "Um celular touch screen da empresa A"
End Function
End Class
Public Class CompanyARegularCellphone
Inherits AbstractRegularCellphone
Public Overloads Overrides Function ToString() As String
Return "Um celular comum da empresa A"
End Function
End Class
#End Region
#Region "ProductsFromBCompany"
Public Class CompanyBTouchedScreenCellphone
Inherits AbstractCellphoneWithTouchedScreen
Public Overloads Overrides Function ToString() As String
Return "Cellphone touch screen da empresa B"
End Function
End Class
Public Class CompanyBRegularCellphone
Inherits AbstractRegularCellphone
Public Overloads Overrides Function ToString() As String
Return "Um celular comum da empresa B"
End Function
End Class
#End Region
#Region "Abstracoes dos Produtos"
Public MustInherit Class AbstractCellphoneWithTouchedScreen
Public MustOverride Overloads Overrides Function ToString() As String
End Class
Public MustInherit Class AbstractRegularCellphone
Public MustOverride Overloads Overrides Function ToString() As String
End Class
#End Region
MustInherit Class CellphoneFactory
Public MustOverride Function CreateCellphoneA() As AbstractCellphoneWithTouchedScreen
Public MustOverride Function CreateCellphoneB() As AbstractRegularCellphone
End Class
Class CompanyACellphoneFactory
Inherits CellphoneFactory
Public Overloads Overrides Function CreateCellphoneA() As AbstractCellphoneWithTouchedScreen
Return New CompanyATouchedScreenCellphone()
End Function
Public Overloads Overrides Function CreateCellphoneB() As AbstractRegularCellphone
Return New CompanyARegularCellphone()
End Function
End Class
Class CompanyBCellphoneFactory
Inherits CellphoneFactory
Public Overloads Overrides Function CreateCellphoneA() As AbstractCellphoneWithTouchedScreen
Return New CompanyBTouchedScreenCellphone()
End Function
Public Overloads Overrides Function CreateCellphoneB() As AbstractRegularCellphone
Return New CompanyBRegularCellphone()
End Function
End Class
Class Industry
Private cellphoneOne As AbstractCellphoneWithTouchedScreen
Private cellphoneTwo As AbstractRegularCellphone
Public Sub New(ByVal cellphoneFactory As CellphoneFactory)
Me.cellphoneOne = cellphoneFactory.CreateCellphoneA()
Me.cellphoneTwo = cellphoneFactory.CreateCellphoneB()
End Sub
Public Sub Produce()
Console.WriteLine("Construção do celular: {0}. ", Me.cellphoneOne.ToString())
Console.WriteLine("Construção do celular: {0}. ", Me.cellphoneTwo.ToString())
End Sub
End Class
End Namespace
Nenhum comentário:
Postar um comentário