Permite que um algoritmo varie independentemente dos clientes que o utilizam. As estratégias podem fornecer diferentes implementações do mesmo comportamento.
Strategy (MartialArt): Define uma interface comum para todos os algoritmos apoiados.
ConcreteStrategy (Karate, Judo, Jui-Jitsu): Implementa os algoritmos usando a interface de Strategy.
Context (Fighter): É configurado com um objeto ConcreteStrategy e mantém uma referência para um objeto Strategy.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class Judo implements MartialArt {
@Override
public String toAttack() {
return "Judo Ataque!";
}
@Override
public String toDefend() {
return "Judo Defesa!";
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class Karate implements MartialArt {
@Override
public String toAttack() {
return "Karate Ataque!";
}
@Override
public String toDefend() {
return "Karate Defesa!";
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public interface MartialArt {
public String toAttack();
public String toDefend();
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Inicio da luta!!!
BrazilianFighter fighter1 = new BrazilianFighter();
JapaneseFighter fighter2 = new JapaneseFighter();
Karate karat = new Karate();
Judo judo = new Judo();
Jiujitsu jj = new Jiujitsu();
fighter1.setMartialArt(karat);
System.out.println(fighter1.toAttack());
System.out.println(fighter1.toDefend());
fighter1.setMartialArt(judo);
System.out.println(fighter1.toAttack());
System.out.println(fighter1.toDefend());
fighter2.setMartialArt(judo);
System.out.println(fighter2.toAttack());
System.out.println(fighter2.toDefend());
fighter2.setMartialArt(jj);
System.out.println(fighter2.toAttack());
System.out.println(fighter2.toDefend());
//Fim da luta
// Inicio nova Luta!!
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public abstract class Fighter {
MartialArt martialArt;
public void setMartialArt(MartialArt martialArt){
this.martialArt = martialArt;
}
public abstract String toAttack();
public abstract String toDefend();
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class Jiujitsu implements MartialArt {
@Override
public String toAttack() {
return "Jiu-Jitsu Ataque!";
}
@Override
public String toDefend() {
return "Jiu-Jitsu Defesa!";
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class BrazilianFighter extends Fighter {
public BrazilianFighter(){
System.out.println("Lutador Brasileiro!");
}
@Override
public String toAttack() {
return martialArt.toAttack();
}
@Override
public String toDefend() {
return martialArt.toDefend();
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.padroes.strategyFighter;
/**
*
* @author linus
*/
public class JapaneseFighter extends Fighter{
public JapaneseFighter(){
System.out.println("Lutador Japonês!");
}
@Override
public String toAttack() {
return martialArt.toAttack();
}
@Override
public String toDefend() {
return martialArt.toDefend();
}
}