-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from minamicr/abstract-factory-pattern
Implementation of abstract factory pattern
- Loading branch information
Showing
11 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
...tract Factory Pattern/com/abstractfactory/java/factory/BrazilianRulesAbstractFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...bstract Factory Pattern/com/abstractfactory/java/factory/CountryRulesAbstractFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
18 changes: 18 additions & 0 deletions
18
...rns/Abstract Factory Pattern/com/abstractfactory/java/factory/USRulesAbstractFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ract Factory Pattern/com/abstractfactory/java/model/certificate/BrazilianCertificate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...erns/Abstract Factory Pattern/com/abstractfactory/java/model/certificate/Certificate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
9 changes: 9 additions & 0 deletions
9
...ns/Abstract Factory Pattern/com/abstractfactory/java/model/certificate/USCertificate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...rns/Abstract Factory Pattern/com/abstractfactory/java/model/packing/BrazilianPacking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Design Patterns/Abstract Factory Pattern/com/abstractfactory/java/model/packing/Packing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
9 changes: 9 additions & 0 deletions
9
...n Patterns/Abstract Factory Pattern/com/abstractfactory/java/model/packing/USPacking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
... Patterns/Abstract Factory Pattern/com/abstractfactory/java/test/AbstractFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
|
||
} |