import java.util.HashMap;
import java.util.Map;
import java.util.Random;
// Define an interface for the tip strategies
interface TipStrategy {
String getTip(String tipCode);
}
// Define concrete tip strategies for different languages
class EnglishTipStrategy implements TipStrategy {
private static final Map<String, String> tipMap = new HashMap<>();
static {
tipMap.put("ENG1", "Always remember to write clean and readable code.");
tipMap.put("ENG2", "Use meaningful variable and method names to improve code clarity.");
tipMap.put("ENG3", "Document your code properly to make it easier for others to understand.");
tipMap.put("ENG4", "Favor composition over inheritance to achieve better code maintainability.");
tipMap.put("ENG5", "Don't repeat yourself (DRY principle) - refactor duplicated code into reusable methods or classes.");
}
@Override
public String getTip(String tipCode) {
return tipMap.getOrDefault(tipCode, "Tip not found for code: " + tipCode);
}
}
class SpanishTipStrategy implements TipStrategy {
private static final Map<String, String> tipMap = new HashMap<>();
static {
tipMap.put("ESP1", "Recuerda siempre escribir código limpio y legible.");
tipMap.put("ESP2", "Usa nombres de variables y métodos significativos para mejorar la claridad del código.");
tipMap.put("ESP3", "Documenta tu código adecuadamente para facilitar su comprensión por parte de los demás.");
tipMap.put("ESP4", "Prefiere la composición sobre la herencia para lograr una mejor mantenibilidad del código.");
tipMap.put("ESP5", "No te repitas (principio DRY) - refactoriza el código duplicado en métodos o clases reutilizables.");
}
@Override
public String getTip(String tipCode) {
return tipMap.getOrDefault(tipCode, "Tip not found for code: " + tipCode);
}
}
// Tip context class
class TipContext {
private TipStrategy tipStrategy;
public TipContext(TipStrategy tipStrategy) {
this.tipStrategy = tipStrategy;
}
public void setTipStrategy(TipStrategy tipStrategy) {
this.tipStrategy = tipStrategy;
}
public String getTip(String tipCode) {
return tipStrategy.getTip(tipCode);
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create a map to store tip strategies for different languages
Map<String, TipStrategy> tipStrategies = new HashMap<>();
tipStrategies.put("English", new EnglishTipStrategy());
tipStrategies.put("Spanish", new SpanishTipStrategy());
// Example: Selecting a language
String selectedLanguage = "English"; // Change this to select different languages
// Example: Selecting a tip code
String selectedTipCode = "ENG3"; // Change this to select different tip codes
// Get the tip strategy based on the selected language
TipStrategy tipStrategy = tipStrategies.get(selectedLanguage);
// Create a tip context with the selected tip strategy
TipContext tipContext = new TipContext(tipStrategy);
// Get and display the tip
System.out.println("Tip for " + selectedLanguage + " with code " + selectedTipCode + ": " + tipContext.getTip(selectedTipCode));
}
}
temp001
最新推荐文章于 2024-07-13 22:56:10 发布