Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of abstract factory pattern #56

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Design Patterns/Abstract Factory Pattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ABSTRACT FACTORY

Definição:
Prover uma interface para criar famílias de objetos relacionados ou dependentes sem especificar suas classes concretas.
Responsável por criar um conjunto de objetos dentro do mesmo contexto.

Projeto:
O exemplo refere-se a um certificado (Certificate) e empacotamento (Packing), que serão aplicados em um aparelho celular.
Este celular pode ser produzido no Brazil ou Estados Unidos, devendo seguir as regras do respectivo país (CountryRulesAbstractFactory).
Para testar o pattern, foi desenvolvida a classe AbstractFactoryTest.

Informações adicionais:
Este exemplo faz parte do curso Padrões de Projeto em JAVA na Prática - Udemy - Cod3r
Fonte adicional para estudo de patterns:
https://refactoring.guru/design-patterns/abstract-factory
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abstractfactory.java.factory;


import com.abstractfactory.java.model.certificate.BrazilianCertificate;
import com.abstractfactory.java.model.certificate.Certificate;
import com.abstractfactory.java.model.packing.BrazilianPacking;
import com.abstractfactory.java.model.packing.Packing;

public class BrazilianRulesAbstractFactory implements CountryRulesAbstractFactory {

public Certificate getCertificates() {
return new BrazilianCertificate();
}

public Packing getPacking() {
return new BrazilianPacking();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.abstractfactory.java.factory;


import com.abstractfactory.java.model.certificate.Certificate;
import com.abstractfactory.java.model.packing.Packing;

public interface CountryRulesAbstractFactory {
Certificate getCertificates();

Packing getPacking();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abstractfactory.java.factory;

import com.abstractfactory.java.model.certificate.Certificate;
import com.abstractfactory.java.model.certificate.USCertificate;
import com.abstractfactory.java.model.packing.Packing;
import com.abstractfactory.java.model.packing.USPacking;

public class USRulesAbstractFactory implements CountryRulesAbstractFactory {

public Certificate getCertificates() {
return new USCertificate();
}

public Packing getPacking() {
return new USPacking();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.abstractfactory.java.model.certificate;

public class BrazilianCertificate implements Certificate {
@Override
public String applyCertification() {
return "\t- Calibrating Brazil rules\n\t- Applying Anatel's Stamp";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.abstractfactory.java.model.certificate;

public interface Certificate {
String applyCertification();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.abstractfactory.java.model.certificate;

public class USCertificate implements Certificate {

public String applyCertification() {
return "\t- Calibrating US rules";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.abstractfactory.java.model.packing;

public class BrazilianPacking implements Packing {

@Override
public String pack() {

return "\t- Packing in Brazil";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.abstractfactory.java.model.packing;

public interface Packing {
String pack();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.abstractfactory.java.model.packing;

public class USPacking implements Packing {

public String pack() {
return "\t- Packing in US";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.abstractfactory.java.test;

import static org.junit.jupiter.api.Assertions.assertTrue;

import com.abstractfactory.java.factory.BrazilianRulesAbstractFactory;
import com.abstractfactory.java.factory.CountryRulesAbstractFactory;
import com.abstractfactory.java.factory.USRulesAbstractFactory;
import org.junit.jupiter.api.Test;

public class AbstractFactoryTest {

@Test
void whenInstantiateBrazilRulesReturnCertificateAndPackInBrazil(){
String match = "Brazil";
CountryRulesAbstractFactory rules = new BrazilianRulesAbstractFactory();
assertTrue(rules.getCertificates().applyCertification().contains(match));
assertTrue(rules.getPacking().pack().contains(match));

}

@Test
void whenInstantiateUSRulesReturnCertificateAndPackInUS(){
String match = "US";
CountryRulesAbstractFactory rules = new USRulesAbstractFactory();
assertTrue(rules.getCertificates().applyCertification().contains(match));
assertTrue(rules.getPacking().pack().contains(match));

}

}