Back
Close

Design Pattern Factory & Abstract Factory

NicolasAirault
1,746 views
Previous: Design pattern Abstract Factory Next: Quizz

Dans notre exemple, on va essayer de faire un système pour créer deux types d'ordinateurs, soit un pc, soit un serveur :

Etape 1

Création de la classe abstaite Computer qui correspond à la classe mère.

public abstract class Computer {
	
	public abstract String getRAM();
	public abstract String getHDD();
	public abstract String getCPU();
	
	@Override
	public String toString(){
		return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
	}
}

Etape 2

Création des classes PC et Server les classes filles descendant de Computer.

public class PC extends Computer {

	private String ram;
	private String hdd;
	private String cpu;
	
	public PC(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public String getRAM() {
		return this.ram;
	}

	@Override
	public String getHDD() {
		return this.hdd;
	}

	@Override
	public String getCPU() {
		return this.cpu;
	}

}
public class Server extends Computer {

	private String ram;
	private String hdd;
	private String cpu;
	
	public Server(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public String getRAM() {
		return this.ram;
	}

	@Override
	public String getHDD() {
		return this.hdd;
	}

	@Override
	public String getCPU() {
		return this.cpu;
	}

}

Étape 3

Création de l'interface ComputerAbstractFactory qui possède une méthode createComputer().

public interface ComputerAbstractFactory {

	public Computer createComputer();

}

Étape 4

Création des classes d'usine PCFactory et ServerFactory qui implementent l'interface ComputerAbstractFactory.

public class PCFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public PCFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public Computer createComputer() {
		return new PC(ram,hdd,cpu);
	}

}
public class ServerFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public ServerFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	
	@Override
	public Computer createComputer() {
		return new Server(ram,hdd,cpu);
	}

}

Étape 5

Création de la classe ComputerFactory qui possède une méthode abstraite getComputer() prenant en paramètre une ComputerAbstractFactory.

public class ComputerFactory {

	public static Computer getComputer(ComputerAbstractFactory factory){
		return factory.createComputer();
	}
}

Voici le diagramme de classes :

Diag_design

Étape 6

Création d'une classe TestFactory qui utilise l'implémentation du modèle de conception ci-dessus.

abstract class Computer {...}
class PC extends Computer {...}
class Server extends Computer {...}
interface ComputerAbstractFactory {...}
class PCFactory implements ComputerAbstractFactory {...}
class ServerFactory implements ComputerAbstractFactory {...}
class ComputerFactory {...}
class Main {
public static void main(String[] args) {
Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
System.out.println("AbstractFactory PC Config::"+pc);
System.out.println("AbstractFactory Server Config::"+server);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants