Wednesday, 6 March 2019

Strategy Design Pattern

Intent

Define a family of algorithms, encapsulate each one, and make theminterchangeable.
Strategy lets the algorithm vary independently fromclients that use it.

Also Known As

Policy

Applicability

Use the Strategy pattern when
  • many related classes differ only in their behavior. Strategiesprovide a way to configure a class with one of many behaviors.
  • you need different variants of an algorithm. For example, you might definealgorithms reflecting different space/time trade-offs.Strategies can be used when these variants are implemented as a classhierarchy of algorithms 
  • an algorithm uses data that clients shouldn't know about. Use theStrategy pattern to avoid exposing complex, algorithm-specific datastructures.
  • a class defines many behaviors, and these appear as multipleconditional statements in its operations. Instead of manyconditionals, move related conditional branches into their ownStrategy class. 

Structure

 

Program

===================some title ========================

package Staragy;

/**
 *
 * @author Sonu
 */
public interface ISnak {
   
    public String snakeName();
}

====================some title =======================

package Staragy;

/**
 *
 * @author Sonu
 */
public interface ISnakeBehavior {
   
    public String behavior();
}

====================some title =======================

package Staragy;

/**
 *
 * @author Sonu
 */
public class NonPosinistion implements ISnakeBehavior{

    @Override
    public String behavior() {
       
        return "Non Position.";
    }
   
}

=====================some title ======================

package Staragy;

/**
 *
 * @author Sonu
 */
public class Posiginus implements ISnakeBehavior{

    @Override
    public String behavior() {
        return "Posigin.";
    }
   
}

========================some title ===================

package Staragy;

/**
 *
 * @author Sonu
 */
public class Contex {
   
    ISnak iSnak;
    ISnakeBehavior iSnakeBehavior;
    public Contex(ISnak iSnak , ISnakeBehavior iSnakeBehavior){
       
        this.iSnak = iSnak;
        this.iSnakeBehavior = iSnakeBehavior;
    }
   
    public String  setsnack()
    {
        return iSnak.snakeName() +"is"+ iSnakeBehavior.behavior();
    }
}

=====================some title ======================

package Staragy;

/**
 *
 * @author Sonu
 */
public class Ratsnak implements ISnak{

    @Override
    public String snakeName() {
        return "Rat Snak.";
    }
   
}

===================some title =======================

package Staragy;

/**
 *
 * @author Sonu
 */
public class Cobara implements ISnak{

    @Override
    public String snakeName() {
        return "Cobara";
    }
   
}

=====================The main class ===================

package Staragy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Sonu
 */
public class Main {
   
    public static void main(String[] args) throws IOException {
       
        Contex c1;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1.Cobara");
        System.out.println("2.RatSnack");
        int snack =Integer.parseInt(br.readLine());
        System.out.println("1.Posigon");
        System.out.println("2.Non-posion");
        int behavior = Integer.parseInt(br.readLine());
       
        if(snack == 1){
            if(behavior == 1)
            {
                c1 = new Contex(new Cobara(), new Posiginus());
                System.out.println(""+c1.setsnack());
            }
            else
            {
                c1 = new Contex(new Cobara(), new NonPosinistion());
                System.out.println(""+c1.setsnack());
            }
        }
        else if(snack == 2){
            if(behavior == 1){
                c1 = new Contex(new Cobara(), new Posiginus());
                System.out.println(""+c1.setsnack());
            }
            else
            {
                c1 = new Contex(new Cobara(), new NonPosinistion());
                System.out.println(""+c1.setsnack());
            }
        }
    }
}

=================output==============================


0 comments:

Post a Comment