Discussion on Design Pattern 01-strategy Pattern

Source: Internet
Author: User

 

I,Definition

I recently started to study the head first design model. This book is refreshing and difficult to get sleepy. Today, I will share with you the policy model.

1.Definition:

Policy mode definedAlgorithmFamily, respectively, so that they can replace each other. This pattern makes the algorithm changes independent of the customers who use the algorithm.

The policy mode structure, where:

    • Strategy can be declared as an interface or abstract class, which is mainly used to abstract different algorithms or behaviors;
    • Concretestrategya is the specific implementation of the above interface or abstract class, implementing different algorithms or behaviors respectively;
    • Context contains the strategy instance and its operations.

2.Design Pattern principles:

    • Multi-Purpose Combination and less inheritance.
    • Identify potential changes in the application and separate them from those that do not need to be changed.CodePut together.

II,Instance

Seeing this pattern reminds me of the fairy monsters in travel to the West. Every battle of the world will use their respective artifacts, so they can see them clearly. In this way, we can become a classic topic. I'm afraid there will be no good looking if they are all the same. Here is a simple example.

1. Scenario 1:Simple appearance

The gods should first introduce themselves and adopt a style similar to "I am ..." The sentence; then the artifact is shown, "Look at my ...".

Implementation: We use an abstract class role that contains the introduce () and fight () methods. Then define several specific classes for implementation.

    • Abstract role

Public abstract class role

{

Public abstract void introduce ();

Public abstract void fight ();

}

    • Specific role

/// Sun Wukong

Public class sunwukong: Role

{

Public override void introduce ()

{

Console. writeline ("I am Sun Wukong! ");

}

Public override void fight ()

{

Console. writeline ("Look at my treasure! ");

}

}

/// Princess tie fan

Public class tieshangongzhu: Role

{

Public override void introduce ()

{

Console. writeline ("I'm Princess tie fan! ");

}

Public override void fight ()

{

Console. writeline ("look at me and let you fly to the sky! ");

}

}

}

    • Program. CS call

Class Program

{

Static void main (string [] ARGs)

{

Console. writeline ("simple appearance :");

// Wukong

Sunwukong Wukong = new sunwukong ();

Wukong. Introduce ();

Wukong. Fight ();

// Princess tie fan

Tieshangongzhu gongzhu = new tieshangongzhu ();

Gongzhu. Introduce ();

Gongzhu. Fight ();

Console. Read ();

}

2.Scenario 2: Wukong snatched the weapon of Princess tie fan

We have implemented different gods to fight with different weapons, but this may happen. For example, Sun Wukong snatched a fan from the Iron fan, or, a biggee has the upper hand and snatched Wukong's golden hoop. In this case, it is difficult to implement this method. Therefore, we abstract the fight behavior as an interface to separate it from the specific master.

    • The ifight interface defines the fight () method and implements it in each specific behavior class.

/// Abstracted combat Interface

Public interface ifight

{

Void fight ();

}

/// Battle

Public class jingubang: ifight

{

Public void fight ()

{

Console. writeline ("Look at my treasure! ");

}

}

// Fight with fans

Public class Tieshan: ifight

{

Public void fight ()

{

Console. writeline ("look at me and let you fly to the sky! ");

}

}

 

// Empty-handed combat
Public class kongshou: ifight
{
Public void fight ()
{
Console. writeline ("finished! I have no weapons. ");
}
}

 

    • Abstract role: Contains ifight behavior instances, weapon change methods, and fight () methods.

Public abstract class role

{

Public ifight fight; // an instance of the ifight Interface

Public abstract void introduce ();

Public void setfight (ifight fight) // change the weapon

{

This. Fight = fight;

}

Public void fight () // call the fight () method

{

Fight. Fight ();

}

// Obtain weapons
Public void getweaponfrom (role)
{
This. Fight = role. fight;
Role. Fight = new kongshou ();
}

}

    • The specific role type. The combat weapon is initialized in the constructor.

/// Sun Wukong

Public class sunwukong: Role

{

Public sunwukong ()

{// Initialize the weapon

Base. Fight = new jingubang ();

}

Public override void introduce ()

{

Console. writeline ("I am Sun Wukong! ");

}

}

/// Princess tie fan

Public class tieshangongzhu: Role

{

Public tieshangongzhu ()

{

Base. Fight = new Tieshan ();

}

Public override void introduce ()

{

Console. writeline ("I'm Princess tie fan! ");

}

}

    • Program. CS class call:

Class Program

{

Static void main (string [] ARGs)

{

Console. writeline ("simple appearance :");

Sunwukong Wukong = new sunwukong ();

Wukong. Introduce ();

Wukong. Fight ();

Tieshangongzhu gongzhu = new tieshangongzhu ();

Gongzhu. Introduce ();

Gongzhu. Fight ();

Console. writeline ("** Wukong snatched the princess's fans **");

Wukong. getweaponfrom (gongzhu );

Gongzhu. Introduce ();
Gongzhu. Fight ();
Wukong. Introduce ();
Wukong. Fight ();
Console. Read ();}

}

OK. I want to discuss with you the new design mode. If any description is inappropriate, please submit it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.