Details und andere Muster werden in ** Grundlegendes zu Entwurfsmustern durch Vergleichen von Implementierungen in JavaScript und Java ** geschrieben. Ich habe ein Beispiel für JavaScript geschrieben, indem ich mir Java angesehen habe. Unterschiede in Funktionen wie Klassentyp / Prototypentyp, Typstärke und Zugriffsmodifikatoren werden nicht ausgenutzt. Bitte beachten Sie.
Strategy Strategie bedeutet "Strategie" Eine Strategie zur Lösung eines Problems, dh ein Algorithmus, kann gegen einen anderen Algorithmus ausgetauscht werden Das Strategiemuster ist ein Muster, das es einfacher macht, dasselbe Problem unterschiedlich zu lösen.
Main.java
public class Main {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java Main randomseed1 randomseed2");
System.out.println("Example: java Main 314 15");
System.exit(0);
}
int seed1 = Integer.parseInt(args[0]);
int seed2 = Integer.parseInt(args[1]);
Player player1 = new Player("Taro", new WinningStrategy(seed1));
Player player2 = new Player("Hana", new ProbStrategy(seed2));
for (int i = 0; i < 1; i++) {
Hand nextHand1 = player1.nextHand();
Hand nextHand2 = player2.nextHand();
if (nextHand1.isStrongerThan(nextHand2)) {
System.out.println("Winner:" + player1);
player1.win();
player2.lose();
} else if (nextHand2.isStrongerThan(nextHand1)) {
System.out.println("Winner:" + player2);
player1.lose();
player2.win();
} else {
System.out.println("Even...");
player1.even();
player2.even();
}
}
System.out.println("Total result:");
System.out.println(player1.toString());
System.out.println(player2.toString());
}
}
Strategy.java
public interface Strategy {
public abstract Hand nextHand();
public abstract void study(boolean win);
}
WinningStrategy.java
import java.util.Random;
public class WinningStrategy implements Strategy {
private Random random;
private boolean won = false;
private Hand prevHand;
public WinningStrategy(int seed) {
random = new Random(seed);
}
public Hand nextHand() {
if (!won) {
prevHand = Hand.getHand(random.nextInt(3));
}
return prevHand;
}
public void study(boolean win) {
won = win;
}
}
ProbStrategy.java
import java.util.Random;
public class ProbStrategy implements Strategy {
private Random random;
private int prevHandValue = 0;
private int currentHandValue = 0;
private int[][] history = {
{1, 1, 1,},
{1, 1, 1,},
{1, 1, 1,},
};
public ProbStrategy(int seed) {
random = new Random(seed);
}
public Hand nextHand() {
int bet = random.nextInt(getSum(currentHandValue));
int handvalue = 0;
if (bet < history[currentHandValue][0]) {
handvalue = 0;
} else if (bet < history[currentHandValue][0] + history[currentHandValue][1]) {
handvalue = 1;
} else {
handvalue = 2;
}
prevHandValue = currentHandValue;
currentHandValue = handvalue;
return Hand.getHand(handvalue);
}
private int getSum(int hv) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += history[hv][i];
}
return sum;
}
public void study(boolean win) {
if (win) {
history[prevHandValue][(currentHandValue + 1) % 3]++;
history[prevHandValue][(currentHandValue + 2) % 3]++;
}
}
}
Player.java
public class Player {
private String name;
private Strategy strategy;
private int wincount;
private int losecount;
private int gamecount;
public Player(String name, Strategy strategy) {
this.name = name;
this.strategy = strategy;
}
public Hand nextHand() {
return strategy.nextHand();
}
public void win() {
strategy.study(true);
wincount++;
gamecount++;
}
public void lose() {
strategy.study(false);
losecount++;
gamecount++;
}
public void even() {
gamecount++;
}
public String toString() {
return "[" + name + ":" + gamecount + " games, " + wincount + " wind, " + losecount + "lose" + "]";
}
}
Hand.java
public class Hand {
public static final int HANDVALUE_GUU = 0;
public static final int HANDVALUE_CHO = 1;
public static final int HANDVALUE_PAA = 2;
public static final Hand[] hand = {
new Hand(HANDVALUE_GUU),
new Hand(HANDVALUE_CHO),
new Hand(HANDVALUE_PAA),
};
private static final String[] name = {
"Schmiere", "Choki", "Par",
};
private int handvalue;
private Hand(int handvalue) {
this.handvalue = handvalue;
}
public static Hand getHand(int handvalue) {
return hand[handvalue];
}
public boolean isStrongerThan(Hand h) {
return fight(h) == 1;
}
public boolean isWeakerThan(Hand h) {
return fight(h) == -1;
}
private int fight(Hand h) {
if (this == h) {
return 0;
} else if ((this.handvalue + 1) % 3 == h.handvalue) {
return 1;
} else {
return -1;
}
}
public String toString() {
return name[handvalue];
}
}
JavaScript
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Strategy</title>
</head>
<body>
<script src="Main.js"></script>
<script src="WinningStrategy.js"></script>
<script src="ProbStrategy.js"></script>
<script src="Player.js"></script>
<script src="Hand.js"></script>
</body>
</html>
Main.js
MAIN = {};
MAIN.init = function() {
'use strict';
var player1 = new Player("Taro", new WinningStrategy());
var player2 = new Player("Hana", new ProbStrategy());
for (var i = 0; i < 1000; i++) {
var nextHand1 = player1.nextHand();
var nextHand2 = player2.nextHand();
console.log(Hand.jankenName[nextHand1.handvalue], Hand.jankenName[nextHand2.handvalue]);
if (nextHand1.isStrongerThan(nextHand2)) {
console.log("Winner:" + player1.name);
player1.win();
player2.lose();
} else if (nextHand2.isStrongerThan(nextHand1)) {
console.log("Winner:" + player2.name);
player2.win();
player1.lose();
} else {
console.log("Even...");
player1.even();
player2.even();
}
}
console.log("Total result:");
console.log(player1.toString());
console.log(player2.toString());
}
window.addEventListener("load", MAIN.init);
Player.js
var Player = function(name, strategy) {
this.name = name;
this.strategy = strategy;
this.wincount = 0;
this.losecount = 0;
this.gamecount = 0;
}
Player.prototype = {
constructor: "Player",
nextHand: function() {
return this.strategy.nextHand();
},
win: function() {
this.strategy.study(true);
this.wincount++;
this.gamecount++;
},
lose: function() {
this.strategy.study(false);
this.losecount++;
this.gamecount++;
},
even: function() {
this.gamecount++;
},
toString: function() {
return "[" + this.name + ":" + this.gamecount + " games, " + this.wincount + " wind, " + this.losecount + " lose" + "]";
}
}
WinningStrategy.js
var WinningStrategy = function() {
this.random = Math.floor(Math.random() * 3);
this.won = false;
this. prevHand;
}
WinningStrategy.prototype = {
constructor: "WinningStrategy",
nextHand: function() {
return Hand.getHand(this.random);
},
study: function(win) {
if (win) {
won = win;
}
}
};
ProbStrategy.js
var ProbStrategy = function() {
this.random = Math.floor(Math.random() * 3);
this.prevHandValue = 0;
this.currentHandValue = 0;
this.history = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
];
};
ProbStrategy.prototype = {
constructor: "ProbStrategy",
nextHand: function() {
let bet = Math.floor(Math.random() * this.getSum(this.currentHandValue));
let handvalue = 0;
if (bet < this.history[this.currentHandValue][0]) {
handvalue = 0;
} else if (bet < this.history[this.currentHandValue][0] + this.history[this.currentHandValue][1]) {
handvalue = 1;
} else {
handvalue = 2;
}
this.prevHandValue = this.currentHandValue;
this.currentHandValue = handvalue;
return Hand.getHand(handvalue);
},
getSum: function(hv) {
let sum = 0;
for (var i = 0; i < 3; i++) {
sum += this.history[hv][i];
}
return sum;
},
study: function(win) {
if (win) {
this.history[this.prevHandValue][(this.currentHandValue + 1) % 3]++;
this.history[this.prevHandValue][(this.currentHandValue + 2) % 3]++;
}
}
}
Hand.js
var Hand = function(handvalue) {
this.handvalue = handvalue;
};
Hand.prototype = {
constructor: "Hand",
isStrongerThan: function(h) {
return this.fight(h) == 1;
},
fight(h) {
if (this == h) {
return 0;
} else if ((this.handvalue + 1) % 3 == h.handvalue) {
return 1;
} else {
return -1;
}
}
};
Hand.HANDVALUE_GUU = 0;
Hand.HANDVALUE_CHO = 1;
Hand.HANDVALUE_PAA = 2;
Hand.hand = [
new Hand(Hand.HANDVALUE_GUU),
new Hand(Hand.HANDVALUE_CHO),
new Hand(Hand.HANDVALUE_PAA),
];
Hand.jankenName = ["Schmiere", "Choki", "Par"];
Hand.getHand = function(handvalue) {
return Hand.hand[handvalue];
}
Definieren Sie nur die Schnittstelle (API) Beispielprogramm → Strategie (Schnittstelle)
Die Rolle der Implementierung der Schnittstelle der Strategierolle Programmieren Sie eine konkrete Strategie Beispielprogramm → WinningStrategy (Klasse) ProbStrategy(class)
Rollen, die die Strategie-Rolle verwenden Haben Sie eine ConcreateStrategy-Rolle Beispielprogramm → Spieler (Klasse)
Grundsätzlich schreibe ich den Algorithmus in die Methode Das Strategiemuster trennt jedoch bewusst den Algorithmus-Teil. Durch Verwendung des Algorithmus durch Delegation Wenn Sie den Algorithmus ändern möchten, ändern Sie ConcreateStrategy Wenn Sie den Algorithmus ändern möchten, können Sie den Kontext ändern
Es ist auch möglich, sich dynamisch zu ändern, indem der Algorithmus getrennt wird
Ich denke, es gibt Situationen, in denen Sie es heute auf Ihrem Smartphone oder Computer sehen können. Wenn Sie verschiedene Algorithmen mit einer schwachen CPU wie einem Smartphone und einer leistungsstarken CPU wie einem Personal Computer verwenden möchten Aktivieren Sie mithilfe des Strategiemusters
[Einführung in Entwurfsmuster, die in der erweiterten und überarbeiteten Java-Sprache gelernt wurden](https://www.amazon.co.jp/%E5%A2%97%E8%A3%9C%E6%94%B9%E8%A8%82% E7% 89% 88Java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% B6% E3% 82% A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5% 85% A5% E9% 96% 80-% E7% B5 % 90% E5% 9F% 8E-% E6% B5% A9 / dp / 4797327030)
Recommended Posts