Strategy

C++ Code:

#include <iostream>

class AlgorithmInterface
{
public:
	virtual int CalAns(int lhs, int rhs) = 0;
};

class SubMethod : public AlgorithmInterface
{
public:
	virtual int CalAns(int lhs, int rhs) { return lhs - rhs; }
};

class PlusMethod : public AlgorithmInterface
{
public:
	virtual int CalAns(int lhs, int rhs) { return lhs + rhs; }
};

class MulMethod : public AlgorithmInterface
{
public:
	virtual int CalAns(int lhs, int rhs) { return lhs * rhs; }
};

class Strategy
{
private:
	char _algType;

public:
	Strategy(char t) : _algType( t ) {}
	AlgorithmInterface* GetAlgorithmInterfacePtr(void);
};

AlgorithmInterface* Strategy::GetAlgorithmInterfacePtr(void)
{
	//default is MulMethod
	if( '-' == _algType )
		return new SubMethod();
	else if( '+' == _algType )
		return new PlusMethod();
	else if( '*' == _algType )
		return new MulMethod();
	return NULL;
}

class Context
{
private:
	int 		_lhs;
	int 		_rhs;
	Strategy*	_strategy;

public:
	Context(int lhs, int rhs, char algType) : \
	_lhs(lhs), _rhs(rhs) { _strategy = new Strategy(algType); }
	~Context(void) { delete _strategy; }

	int GetAns(void) 
	{ 
		AlgorithmInterface* ptr = _strategy->GetAlgorithmInterfacePtr();
		if( NULL == ptr )
			return -1;
		return ptr->CalAns(_lhs, _rhs);
	}
};

int main(int argc, char const *argv[])
{
	int lhs 		= 10201;
	int rhs 		= 20102;

	Context dat(lhs, rhs, '+');
	std::cout << dat.GetAns() << std::endl;

	Context dat1(lhs, rhs, '*');
	std::cout << dat1.GetAns() << std::endl;


	Context dat2(lhs, rhs, '/');
	std::cout << dat2.GetAns() << std::endl;

	return 0;
}
Python Code:
class PlusMethod(object):
	def CalAns(self, lhs, rhs):
		return lhs + rhs
	
class SubMethod(object):
	def CalAns(self, lhs, rhs):
		return lhs - rhs
	
class MulMethod(object):
	def CalAns(self, lhs, rhs):
		return lhs * rhs
	
class Strategy(object):
	def __init__(self, algType):
		self._algType = algType
	
	def GetMethodRef(self):
		if '+' == self._algType:
			return PlusMethod()
		elif '-' == self._algType:
			return SubMethod()
		elif '*' == self._algType:
			return MulMethod()
		else:
			return None
		
class Context(object):
	def __init__(self, lhs, rhs, algType):
		self._lhs		= lhs
		self._rhs		= rhs
		self._strategy 	= Strategy(algType)
		
	def CalAns(self):
		methodRef = self._strategy.GetMethodRef()
		if not methodRef:
			return -1
		return methodRef.CalAns(self._lhs, self._rhs)
	
if "__main__" == __name__:
	lhs = 10201
	rhs = 20102
	
	print Context(lhs, rhs, '+').CalAns()
	print Context(lhs, rhs, '*').CalAns()
	print Context(lhs, rhs, '/').CalAns()
	
	
	
以上只是个人对策略模式的理解,分别使用静态语言与动态语言实现,仅供参考,如有问题欢迎指正
### 如何配置 UpdateStrategy 在 Kubernetes 中 在 Kubernetes 的 `StatefulSet` 或者 `DaemonSet` 资源中可以指定 `updateStrategy` 来控制更新的方式。对于 `Deployment` 类型,则使用 `strategy` 字段。 #### Deployment 更新策略 对于 `Deployment`,有两种主要类型的更新策略: - **Recreate**: 这种方式会在新版本的 Pod 创建之前终止所有的现有 Pod 实例[^1]。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest strategy: type: Recreate ``` - **RollingUpdate**: 默认情况下使用的滚动更新模式会逐步替换老版本的 Pods,而不是一次性全部销毁再重新创建。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 25% maxSurge: 25% ``` 上述例子展示了如何通过 YAML 文件来设定不同的更新策略。其中 `maxUnavailable` 和 `maxSurge` 参数用于更细粒度地调整滚动更新的行为,前者指定了允许的最大不可用实例比例,后者则设定了额外可超出期望副本数目的最大数量。 #### StatefulSet 和 DaemonSet 的更新策略 除了 `Deployment` 外,`StatefulSet` 和 `DaemonSet` 同样支持类似的更新机制,但是具体行为有所不同。例如,在 `StatefulSet` 上可以通过设置 `.spec.updateStrategy.type=OnDelete` 让用户手动删除旧版 Pod 才触发更新;而`.spec.updateStrategy.type=RollingUpdate` 则实现了自动化的滚动升级逻辑。 ```yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 updateStrategy: type: RollingUpdate ... ``` 同样适用于 `DaemonSet`: ```yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: fluentd-elasticsearch spec: updateStrategy: type: RollingUpdate ... ``` 这些配置使得管理员能够灵活应对不同场景下应用程序和服务的需求变化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值