Design Mode C ++ learning notes (Strategy Mode)

Source: Internet
Author: User

Inadvertently, I went from the Internet to an ebook, "24 design patterns and six design principles". I'm curious that there are 24 design patterns here, the Design Patterns written by GOF seems to have only 23. At first glance, it really attracted us, and the examples mentioned in it are very interesting. I am very grateful to the author for writing such a good example. My goal is to use C ++ for the example I mentioned. The author of this book is: cbf4life. For more details and instructions, refer to the original author's blog: cbf4life.cnblogs.com.

Here, we will only perform simple prompts and encoding.

1. Explanation

Main (), Zhao Yun

CContext, tips

IStrategy, Policy Interface

CBackDoor, one of the policies

CGivenGreenLight, policy 2

CBlockEnemy, Policy 3

Note: Put a policy in a tip. When using this tool, find the tip and use the strategy from the tip.

Note: The tip is just a simple loading and calling policy, and there is no logic in the tip. Policies have greater autonomy and run more logic.

Check the Code:

// Context. h

# Pragma once
# Include "IStrategy. h"
Class CContext
{
Public:
CContext (IStrategy * pStrategy );
~ CContext (void );
Void Operate (void );
Private:
IStrategy * m_pStrategy;
};

// Context. cpp

# Include "StdAfx. h"
# Include "Context. h"
CContext: CContext (IStrategy * pStrategy)
{
This-> m_pStrategy = pStrategy;
}
CContext ::~ CContext (void)
{
Delete this-> m_pStrategy;
}
Void CContext: Operate (void)
{
This-> m_pStrategy-> Operate ();
}

// IStrategy. h

# Pragma once

Class IStrategy
{
Public:
IStrategy (void );
Virtual ~ IStrategy (void );
Virtual void Operate (void) = 0;
};

// BackDoor. h

# Pragma once
# Include "istrategy. h"
Class CBackDoor:
Public IStrategy
{
Public:
CBackDoor (void );
~ CBackDoor (void );
Void Operate (void );
};

// BackDoor. cpp

# Include "StdAfx. h"
# Include "BackDoor. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CBackDoor: CBackDoor (void)
{
}
CBackDoor ::~ CBackDoor (void)
{
}
Void CBackDoor: Operate (void)
{
Cout <"Ask Qiao Guoluo for help and let Wu Guotai put pressure on Sun Quan" <endl;
}

// GivenGreenLight. h

# Pragma once
# Include "istrategy. h"
Class CGivenGreenLight:
Public IStrategy
{
Public:
CGivenGreenLight (void );
~ CGivenGreenLight (void );
Void Operate (void );
};

// GivenGreenList. cpp

# Include "StdAfx. h"
# Include "GivenGreenLight. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CGivenGreenLight: CGivenGreenLight (void)
{
}
CGivenGreenLight ::~ CGivenGreenLight (void)
{
}
Void CGivenGreenLight: Operate (void)
{
Cout <"ask Wu Guotai to open a green light and let it go! "<Endl;
}

// BlockEnemy. h

# Pragma once
# Include "istrategy. h"
Class CBlockEnemy:
Public IStrategy
{
Public:
CBlockEnemy (void );
~ CBlockEnemy (void );
Void Operate (void );
};

// BlockEnemy. cpp

# Include "StdAfx. h"
# Include "BlockEnemy. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CBlockEnemy: CBlockEnemy (void)
{
}
CBlockEnemy ::~ CBlockEnemy (void)
{
}
Void CBlockEnemy: Operate ()
{
Cout <"after Mrs sun is disconnected, block the pursuit of troops" <endl;
}

// Strategy. cpp

# Include "stdafx. h"
# Include "Context. h"
# Include "BackDoor. h"
# Include "GivenGreenLight. h"
# Include "BlockEnemy. h"
# Include <iostream>
Using std: cout;
Using std: endl;
Int _ tmain (int argc, _ TCHAR * argv [])
{
CContext * pContext;

Cout <"\ 14 \ n \ 17" <endl;
Cout <"---------- remove the first ---------- when I arrived at Wu Guo" <endl;
PContext = new CContext (new CBackDoor ());
PContext-> Operate ();
Delete pContext;

Cout <"\ 14 \ n \ 17" <endl;
Cout <"---------- Liu Bei is happy, and the second is split ----------" <endl;
PContext = new CContext (new CGivenGreenLight ());
PContext-> Operate ();
Delete pContext;

Cout <"\ 14 \ n \ 17" <endl;
Cout <"---------- What should I do if Sun Quan's soldier is behind me? Remove the third ---------- "<endl;
PContext = new CContext (new CBlockEnemy ());
PContext-> Operate ();
Delete pContext;

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}

One tip can only provide one trick. Zhao Yun can have multiple tips. It belongs to the object behavior mode. It's a very simple model. It seems like this is the simplest of the 24 models. Or, start with a simple and easy one. It's hard to start with everything. Find a simple start and encourage yourself to stick to it. Blog will also play this role and encourage yourself. Instead of simply abstracting the pattern class diagram, it only describes the relationship between classes used in the code. The real abstract class graph only contains the policy interface andOnePolicy implementation class, including the CContext class and Client.

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.