策略模式 之c#,c++,java,php

策略模式是目前我工作中比较常用的模式

下面贴出搜集各种语言的版本,记得我当初刚开始的时候,只会C#,别的语言看不明白,不知道现在还有没有人有我当年的烦恼!

所以在这里我提供c#,C++,java,php四种版本,本来想写python版本,可惜自己不太懂,怕弄错让大家笑话

首先来个策略模式的大致讲解,定义一系列的算法,把他们一个个封装起来,并且使他们可相互替换,本模式使得算法可独立于使用它的客户而变化

客户端知道要调用具体哪个算法,由客户端决定使用的算法,算法可以独立于客户端自由的变动

如下图:

C#版本策略模式:

using System;  
using System.Collections.Generic;
using System.Text;

namespace 策略模式
{
class Program
{
static void Main(string[] args)
{
Context context;
context
= new Context(new CreateStrategyA());
context.ContextInterface();
context
= new Context(new CreateStrategyB());
context.ContextInterface();
context
= new Context(new CreateStrategyC());
context.ContextInterface();
Console.Read();
}
}
abstract class Strategy
{
public abstract void AlgoInterface();
}
class CreateStrategyA : Strategy
{
public override void AlgoInterface()
{
Console.WriteLine(
"Called CreateStrategyA.AlgoInterface()");
}
}
class CreateStrategyB : Strategy
{
public override void AlgoInterface()
{
Console.WriteLine(
"Called CreateStrategyB.AlgoInterface()");
}
}
class CreateStrategyC : Strategy
{
public override void AlgoInterface()
{
Console.WriteLine(
"Called CreateStrategyC.AlgoInterface()");
}
}
class Context
{
Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
public void ContextInterface()
{
strategy.AlgoInterface();
}
}
}


 

c++版本策略模式

 //策略基类   

class COperation  

{

public:

int m_nFirst;

int m_nSecond;

virtual double GetResult()

{

double dResult=0;

return dResult;

}

};



//策略具体类—加法类

class AddOperation : public COperation

{

public:

AddOperation(
int a,int b)

{

m_nFirst
=a;

m_nSecond
=b;

}

virtual double GetResult()

{

return m_nFirst+m_nSecond;

}

};



class Context

{

private:

COperation
* op;

public:

Context(COperation
* temp)

{

op
=temp;

}

double GetResult()

{

return op->GetResult();

}

};



//客户端

int main()

{

int a,b;

char c;

cin
>>a>>b;

  cout
<<”请输入运算符:;

cin
>>c;

switch(c)

{

case+’:

Context
*context=new Context(new AddOperation(a,b));

cout
<<context->GetResult()<<endl;

break;

default:

break;

}

return 0;

}

java版本策略模式

public interface Person {   

public void speakLanguage();

}



public class Chinese implements Person {

public void speakLanguage() {

System.err.println(
"I speak Chinese!");

}

}


public class StrategyPerson {

private Person person;

public StrategyPerson(Person person){

this.person = person;

}

public void speakLanguage(){

person.speakLanguage();

}

}

public class TestMain {

public static void main(String[] args){

Person person
= new Chinese();

StrategyPerson sp
= new StrategyPerson(person);

sp.speakLanguage();

}

}

php版本策略模式

 

<?php
interface IStrategy
{
function filter( $record );
}

class FindAfterStrategy implements IStrategy
{
private $_name;

public function __construct( $name )
{
$
this->_name = $name;
}

public function filter( $record )
{
return strcmp( $this->_name, $record ) <= 0;
}
}

class RandomStrategy implements IStrategy
{
public function filter( $record )
{
return rand( 0, 1 ) >= 0.5;
}
}

class UserList
{
private $_list = array();

public function __construct( $names )
{
if ( $names != null )
{
foreach( $names as $name )
{
$
this->_list []= $name;
}
}
}

public function add( $name )
{
$
this->_list []= $name;
}

public function find( $filter )
{
$recs
= array();
foreach( $this->_list as $user )
{
if ( $filter->filter( $user ) )
$recs []
= $user;
}
return $recs;
}
}

$ul
= new UserList( array( "Andy", "Jack", "Lori", "Megan" ) );
$f1
= $ul->find( new FindAfterStrategy( "J" ) );
print_r( $f1 );

$f2
= $ul->find( new RandomStrategy() );
print_r( $f2 );
?>

有了解python 的童鞋,希望可以帮忙补充进来

posted @ 2011-07-25 17:58  艾伦  阅读(744)  评论(0编辑  收藏  举报