Design mode - strategic mode

Keywords: Mobile Programming ButterKnife

/**
 * Role superclass:
 * Follow the design principles, find out the parts that may need to change in the application, and separate them,
 * Don't mix with code that doesn't need to change.
 * We found that the display, attack, defend and run of each character could change, so we had to write this independently.
 * Then according to another design principle: programming for interface (super type), rather than for implementation, we transform the code into this:
 */

public abstract class Role {

    protected String name;

    private IDisplayBehavior iDisplayBehavior;
    private IDefendBehavior iDefendBehavior;
    private IRunBehavior iRunBehavior;
    private IAttackBehavior iAttackBehavior;

    public Role setiDisplayBehavior(IDisplayBehavior iDisplayBehavior) {
        this.iDisplayBehavior = iDisplayBehavior;
        return this;
    }

    public Role setiDefendBehavior(IDefendBehavior iDefendBehavior) {
        this.iDefendBehavior = iDefendBehavior;
        return this;
    }

    public Role setiRunBehavior(IRunBehavior iRunBehavior) {
        this.iRunBehavior = iRunBehavior;
        return this;
    }

    public Role setiAttackBehavior(IAttackBehavior iAttackBehavior) {
        this.iAttackBehavior = iAttackBehavior;
        return this;
    }

    public void display() {
        iDisplayBehavior.display();
    }

    public void defend() {
        iDefendBehavior.defend();
    }

    public void attack() {
        iAttackBehavior.attack();
    }

    public void run() {
        iRunBehavior.run();
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class RoleA extends Role {

    public  RoleA(String name){
        this.name = name;
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public interface IAttackBehavior {
    void attack();
}
public interface IDefendBehavior {
    void defend();
}
public interface IDisplayBehavior {
    void display();
}
public interface IRunBehavior {
    void run();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class DisplayYZ implements IDisplayBehavior {

    @Override
    public void display() {
        Log.e("---", "Look like 2");
    }
}

public class DefendTMS implements IDefendBehavior {

    @Override
    public void defend() {
        Log.e("---", "ability to sustain the thrusts of sharp weapons on one's bare skin");
    }
}
public class AttackXL implements IAttackBehavior {
    @Override
    public void attack() {
        Log.e("---", "Eighteen dragon subduing palms");
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class StrategyActivity extends AppCompatActivity {

    @BindView(R.id.bt_strategy)
    Button btStrategy;
    @BindView(R.id.activity_stategy)
    LinearLayout activityStategy;
    @BindView(R.id.tv_define)
    TextView tvDefine;
    @BindView(R.id.bt_strategy_text)
    Button btStrategyText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stategy);
        ButterKnife.bind(this);
        setTitle("Strategy mode");

        tvDefine.setText(EMTagHandler.fromHtml(AppConstant.STRATEGY_DEFINE));
        btStrategyText.setText("Create roles A,And set the appearance,attack,escape,defense");


        btStrategyText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RoleA roleA = new RoleA("---A");
                roleA.setiDisplayBehavior(new DisplayYZ())
                        .setiAttackBehavior(new AttackXL())
                        .setiDefendBehavior(new DefendTMS())
                        .setiRunBehavior(new RunJCTQ());
                roleA.display();// A look
                roleA.attack();// attack
                roleA.run();// escape
                roleA.defend();// defense
            }
        });
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

summary

  • Environment, holding references to strategy,
  • Abstract strategy an abstract role, usually implemented by an interface or an abstract class, that gives all the interfaces needed for a specific strategy
  • Concrete strategy: relevant algorithms or behaviors are wrapped

Usage scenarios

Let's say that now we want to design a shopping cart system for e-commerce websites selling all kinds of books. One of the simplest cases is to multiply the unit price of all goods by the quantity, but the actual situation is certainly more complicated. For example, this website may offer a 20% discount for all senior members, a 10% discount for intermediate members and no discount for junior members.

According to the description, the discount is based on one of the following algorithms:

Algorithm 1: there is no discount for junior members.

Algorithm 2: offer 10% discount to middle-level members.

Algorithm 3: provide 20% discount for senior members.

public interface MemberStrategy {
    /**
     * Calculate the price of books
     * @param booksPrice    The original price of the book
     * @return    Calculate the discounted price
     */
    public double calcPrice(double booksPrice);
}
public class PrimaryMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {
        
        System.out.println("No discount for junior members");
        return booksPrice;
    }

}

public class IntermediateMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {

        System.out.println("10 discount for intermediate members%");
        return booksPrice * 0.9;
    }

}


public class AdvancedMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {
        
        System.out.println("20 discount for senior members%");
        return booksPrice * 0.8;
    }
}


public class Price {
    //Holding a specific strategic target
    private MemberStrategy strategy;
    /**
     * Constructor, passing in a specific policy object
     * @param strategy    Specific policy objects
     */
    public Price(MemberStrategy strategy){
        this.strategy = strategy;
    }
    
    /**
     * Calculate the price of books
     * @param booksPrice    The original price of the book
     * @return    Calculate the discounted price
     */
    public double quote(double booksPrice){
        return this.strategy.calcPrice(booksPrice);
    }
}

public class Client {

    public static void main(String[] args) {
        //Select and create policy objects to use
        MemberStrategy strategy = new AdvancedMemberStrategy();
        //Create environment
        Price price = new Price(strategy);
        //Calculated price
        double quote = price.quote(300);
        System.out.println("The final price of the book is:" + quote);
    }

}

Posted by morphius on Fri, 13 Dec 2019 12:59:55 -0800