खोज…


जावा में टेम्पलेट विधि कार्यान्वयन

टेम्पलेट विधि पैटर्न एक व्यवहार डिजाइन पैटर्न है जो एक ऑपरेशन में एल्गोरिथ्म के कार्यक्रम कंकाल को परिभाषित करता है, उपवर्गों के लिए कुछ चरणों को स्थगित करता है।

संरचना:

यहाँ छवि विवरण दर्ज करें

मुख्य नोट:

  1. टेम्प्लेट विधि इनहेरिटेंस का उपयोग करती है
  2. बेस क्लास द्वारा लागू टेम्प्लेट विधि को ओवरराइड नहीं किया जाना चाहिए। इस तरह, एल्गोरिथ्म की संरचना को सुपर क्लास द्वारा नियंत्रित किया जाता है, और विवरण उप-वर्गों में लागू किया जाता है

कोड उदाहरण:

import java.util.List;

class GameRule{

}
class GameInfo{
    String gameName;
    List<String> players;
    List<GameRule> rules;
}    

abstract class Game{
    protected GameInfo info;
    public Game(GameInfo info){
        this.info = info;
    }
    public abstract void createGame();
    public abstract void makeMoves();
    public abstract void applyRules();
    
    /* playGame is template method. This algorithm skeleton can't be changed by sub-classes. sub-class can change
       the behaviour only of steps like createGame() etc. */
       
    public void playGame(){
        createGame();
        makeMoves();
        applyRules();
        closeGame();
    }
    protected void closeGame(){
        System.out.println("Close game:"+this.getClass().getName());
        System.out.println("--------------------");
    }
}
class Chess extends Game{
    public Chess(GameInfo info){
        super(info);
    }
    public void createGame(){
        // Use GameInfo and create Game
        System.out.println("Creating Chess game");
    }
    public void makeMoves(){
        System.out.println("Make Chess moves");
    }
    public void applyRules(){
        System.out.println("Apply Chess rules");
    }
}
class Checkers extends Game{
    public Checkers(GameInfo info){
        super(info);
    }
    public void createGame(){
        // Use GameInfo and create Game
        System.out.println("Creating Checkers game");
    }
    public void makeMoves(){
        System.out.println("Make Checkers moves");
    }
    public void applyRules(){
        System.out.println("Apply Checkers rules");
    }
    
}
class Ludo extends Game{
    public Ludo(GameInfo info){
        super(info);
    }
    public void createGame(){
        // Use GameInfo and create Game
        System.out.println("Creating Ludo game");
    }
    public void makeMoves(){
        System.out.println("Make Ludo moves");
    }
    public void applyRules(){
        System.out.println("Apply Ludo rules");
    }
}

public class TemplateMethodPattern{
    public static void main(String args[]){
        System.out.println("--------------------");
    
        Game game = new Chess(new GameInfo());
        game.playGame();
        
        game = new Ludo(new GameInfo());
        game.playGame();
        
        game = new Checkers(new GameInfo());
        game.playGame();
    }
}

स्पष्टीकरण:

  1. Game एक abstract सुपर क्लास है, जो एक टेम्पलेट विधि को परिभाषित करता है: playGame()

  2. playGame() कंकाल playGame() बेस क्लास में परिभाषित किया गया है: Game

  3. Chess, Ludo और Checkers जैसी उप-कक्षाएं playGame() के कंकाल को नहीं बदल सकती हैं। लेकिन वे कुछ चरणों के व्यवहार को संशोधित कर सकते हैं जैसे

    createGame();
    makeMoves();
    applyRules();
    

उत्पादन:

--------------------
Creating Chess game
Make Chess moves
Apply Chess rules
Close game:Chess
--------------------
Creating Ludo game
Make Ludo moves
Apply Ludo rules
Close game:Ludo
--------------------
Creating Checkers game
Make Checkers moves
Apply Checkers rules
Close game:Checkers
--------------------


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow