首頁 >web前端 >js教程 >在 React 中建立一款可靠的 Pokémon 遊戲:開發者的冒險!

在 React 中建立一款可靠的 Pokémon 遊戲:開發者的冒險!

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-11-10 00:54:02255瀏覽

Building a SOLID Pokémon Game in React: A Developer’s Adventure!

S:單一職責 - 一隻神奇寶貝,一份工作

問題:PokemonComponent 處理捕捉、戰鬥和顯示分數,違反了 SRP。

function PokemonComponent({ pokemon, onCatch, onBattle, score }) {
  return (
    <div>
      <h2>{pokemon.name}</h2>
      <button onClick={() => onCatch(pokemon)}>Catch</button>
      <button onClick={() => onBattle(pokemon)}>Battle</button>
      <div>Score: {score}</div>
    </div>
  );
}

解決方案:責任劃分。

function PokemonCatcher({ pokemon, onCatch }) {
  return <button onClick={() => onCatch(pokemon)}>Catch</button>;
}
function PokemonBattler({ pokemon, onBattle }) {
  return <button onClick={() => onBattle(pokemon)}>Battle</button>;
}
function ScoreBoard({ score }) {
  return <div>Score: {score}</div>;
}
function PokemonGame({ pokemon, onCatch, onBattle, score }) {
  return (
    <div>
      <h2>{pokemon.name}</h2>
      <PokemonCatcher pokemon={pokemon} onCatch={onCatch} />
      <PokemonBattler pokemon={pokemon} onBattle={onBattle} />
      <ScoreBoard score={score} />
    </div>
  );
}

O:開放/封閉 - 進化的神奇寶貝組件

問題:增加電源等功能需要修改現有組件。

:使用高階組件(HOC)。

function withPowerUp(PokemonComponent) {
  return function PoweredUpComponent(props) {
    const [isPoweredUp, setPowerUp] = useState(false);
    const powerUp = () => {
      setPowerUp(true);
      setTimeout(() => setPowerUp(false), 5000);
    };
    return (
      <div>
        <PokemonComponent {...props} isPoweredUp={isPoweredUp} />
        <button onClick={powerUp}>Power Up!</button>
      </div>
    );
  };
}

const Charmander = ({ isPoweredUp }) => (
  <div>Charmander {isPoweredUp && "(Powered Up!)"}</div>
);

const PoweredCharmander = withPowerUp(Charmander);

function PokemonApp() {
  return <PoweredCharmander />;
}

L:里氏替換 - 可互換的神奇寶貝

問題:交換組件會導致問題。

:使用基礎元件。

function BasePokemon({ attack, children }) {
  return (
    <div className="pokemon">
      <div>Attack: {attack}</div>
      {children}
    </div>
  );
}
function Pikachu({ attack }) {
  return (
    <BasePokemon attack={attack}>
      <h2>Pikachu</h2>
    </BasePokemon>
  );
}
function Charizard({ attack }) {
  return (
    <BasePokemon attack={attack}>
      <h2>Charizard</h2>
    </BasePokemon>
  );
}
function PokemonBattle() {
  return (
    <div>
      <BasePokemon attack="Tackle">
        <h2>Generic Pokémon</h2>
      </BasePokemon>
      <Pikachu attack="Thunderbolt" />
      <Charizard attack="Flamethrower" />
    </div>
  );
}

D:依賴倒置 - 依賴抽象

問題:元件與資料來源緊密耦合。

解決方案:使用上下文進行資料注入。

const PokemonContext = createContext();
function Pikachu() {
  const { attack } = useContext(PokemonContext);
}
<PokemonContext.Provider value={{ attack: "Thunderbolt" }}>
  <Pikachu />
</PokemonContext.Provider>

備忘單:堅實的原則

Principle Poké-Mantra Trainer’s Tip
Single Responsibility One Pokémon, one role. Split complex components into focused ones.
Open/Closed Evolve without changing. Use HOCs, render props for new features.
Liskov Substitution Components like Pokémon moves - interchangeable. Ensure components can be used interchangeably.
Dependency Inversion Depend on abstractions, not concretes. Use context or props for data management.

以上是在 React 中建立一款可靠的 Pokémon 遊戲:開發者的冒險!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn