HF-DP1: strategy pattern

This is the 1st pattern I study. In here, I will mark down my doubts, java study, DESIGN PRINCIPLES, DESIGN PATTERN.

There are 3 design principles in this pattern:

1. Identify the aspects of your application that vary and separate them from what stays the same (Codeproject)

2. Program to an interface, not an implementation (Stackoverflow)

3. Favor composition over inheritance (Stackoverflow)

 

Java study:

1. In C++, Absolute C++ said we need to encapsulate so that we separate codes into Interface (.hpp) and Implementation (.cpp). But in Java, just use:

public interface name{}

public class name{}

2. In strategy DP, encapsulate algorithms for one interface. and the duck base class needs to be inherited, and use abstract method. By defining an abstract method, the class needs to be defined as:

public abstract class Duck{}

3.

UML notations: (ref: codeproj link shown before)

HF-DP1: strategy pattern,布布扣,bubuko.com

时间: 2024-08-07 00:03:56

HF-DP1: strategy pattern的相关文章

设计模式之Strategy Pattern

Declaration 首先声明, 本篇blog的内容是参考Design pattern FAQ Part 3 ( Design pattern training series)这篇博文写的, 图片也是来自这篇博客. 部分是翻译, 加上自己的理解形成这篇博文. 希望和大家一起学习设计模式, 共同进步. Scene for Strategy Pattern Strategy Pattern,这个翻译成策略模式应该是没有问题的.策略模式希望做到的是在运行的情况下,根据对象的类型选择使用的算法.简单来

设计模式之一:策略模式(Strategy Pattern)

在介绍策略模式之前先说下两个设计原则: 1.  Identify the aspects of your application that vary and separate them from what  stays the same.找到系统中变化的部分,将变化的部分同其它稳定的部分隔开. 2.  Program to an interface, not an implementation.面向接口编程,而不要面向实现编程. 那什么是策略模式呢? The Strategy Pattern d

策略模式(Strategy Pattern)

策略模式(Strategy Pattern) 抛开晦涩的定义,首先看一个例子: 我们想要创建一个模拟鸭子的游戏,在这个游戏中,会有各种类型的鸭子,比如mallard duck,red head duck,rubber duck(除了rubber duck(橡皮鸭),看见这其余两种鸭子很好奇,于是查找相关图片,发现mallard duck是绿头鸭,red head duck是红头鸭,自己生活中还没有见过,有趣,哈哈!三种鸭子图片如下所示). 回归话题,在这个模拟鸭子的游戏中,各种鸭子均有两种能力,

设计模式 - 策略模式(Strategy Pattern) 具体解释

策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全部, 禁止转载, 如有须要, 请站内联系. 策略模式: 定义了算法族, 分别封装起来, 让它们之间能够相互替换, 此模式让算法的变化独立于使用算法的客户. 对于父类的子类族须要常常扩展新的功能, 为了使用父类比較灵活的加入子类, 把父类的行为写成接口(interface)的形式; 使用set()方法

How Scala killed the Strategy Pattern

How Scala killed the Strategy Pattern By Alvin Alexander. Last updated: Mar 23, 2014 table of contents [hide] The OOP Strategy Pattern Two immediate thoughts How Scala killed the Strategy Pattern Understanding the 'execute' method Dude, you used 'met

Learning OpenCV Lecture 2 (Using the Strategy pattern in algorithm design)

ColorDetector.h: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> class ColorDetector { public: // empty constructor ColorDetector() : minDist(100) { // default parameter initialization here target[0] = target[1] = targe

设计模式 - 策略模式(Strategy Pattern) 详解

策略模式(Strategy Pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权所有, 禁止转载, 如有需要, 请站内联系. 策略模式: 定义了算法族, 分别封装起来, 让它们之间可以相互替换, 此模式让算法的变化独立于使用算法的客户. 对于父类的子类族需要经常扩展新的功能, 为了使用父类比较灵活的添加子类, 把父类的行为写成接口(interface)的形式; 使用set()方法,

Pattern Design - Strategy Pattern

If you can disassemble code  and  you want to apply one method of an object in another object that there is no inheritance between them, you can use Strategy Pattern. Aha, actually it is good strategy that we can wrap different kinds of algorithms, t

设计模式之策略模式(Strategy Pattern)

一.什么是策略模式(Strategy Pattern)? 从字面上理解,策略模式就是应用了某种“策略”的设计模式,而这个“策略”就是:把变化的部分封装起来. 二.举个例子 假定现在我们需要用类来描述Dog 首先,所有的Dog都有外形(比如Color),有行为(比如Run.Bark) 于是我们很自然地定义了这样一个基类Dog: public abstract class Dog { public abstract void display();//显示Dog的外形 public abstract