🔍目的
仆人类被用于向一组类提供一些行为,区别于在每个类定义行为-或者当我们无法排除
公共父类中的这种行为,这些行为在仆人类被定义一次
🔍解释
真实世界例子
国王、王后和其他宫廷皇室成员需要仆人为他们提供饮食、准备饮料等服务
简单描述
确保一个仆人对象为一组被服务的对象提供一些特定的服务
维基百科
在软件工程中,仆人模式定义了一个对象,用于向一组类提供某些功能,而无需在每个类中定义该功能。 仆人是一个类,其实例(甚至只是类)提供了处理所需服务的方法,而仆人为其(或与谁)做某事的对象被视为参数
程序示例
创建能够为其他宫廷皇室成员提供服务的仆人类
/*** Servant.*/
public class Servant {public String name;/*** Constructor.*/public Servant(String name) {this.name = name;}public void feed(Royalty r) {r.getFed();}public void giveWine(Royalty r) {r.getDrink();}public void giveCompliments(Royalty r) {r.receiveCompliments();}/*** Check if we will be hanged.*/public boolean checkIfYouWillBeHanged(List<Royalty> tableGuests) {return tableGuests.stream().allMatch(Royalty::getMood);}
}
皇家是一个接口,它被国王和女王类实现,以获取仆人的服务
interface Royalty {void getFed();void getDrink();void changeMood();void receiveCompliments();boolean getMood();
}
创建国王类实现皇家接口
public class King implements Royalty {private boolean isDrunk;private boolean isHungry = true;private boolean isHappy;private boolean complimentReceived;@Overridepublic void getFed() {isHungry = false;}@Overridepublic void getDrink() {isDrunk = true;}public void receiveCompliments() {complimentReceived = true;}@Overridepublic void changeMood() {if (!isHungry && isDrunk) {isHappy = true;}if (complimentReceived) {isHappy = false;}}@Overridepublic boolean getMood() {return isHappy;}
}
创建女王类正在实现皇家接口
public class Queen implements Royalty {private boolean isDrunk = true;private boolean isHungry;private boolean isHappy;private boolean isFlirty = true;private boolean complimentReceived;@Overridepublic void getFed() {isHungry = false;}@Overridepublic void getDrink() {isDrunk = true;}public void receiveCompliments() {complimentReceived = true;}@Overridepublic void changeMood() {if (complimentReceived && isFlirty && isDrunk && !isHungry) {isHappy = true;}}@Overridepublic boolean getMood() {return isHappy;}public void setFlirtiness(boolean f) {this.isFlirty = f;}}
使用案例:
public class App {private static final Servant jenkins = new Servant("Jenkins");private static final Servant travis = new Servant("Travis");/*** Program entry point.*/public static void main(String[] args) {scenario(jenkins, 1);scenario(travis, 0);}/*** Can add a List with enum Actions for variable scenarios.*/public static void scenario(Servant servant, int compliment) {var k = new King();var q = new Queen();var guests = List.of(k, q);// feedservant.feed(k);servant.feed(q);// serve drinksservant.giveWine(k);servant.giveWine(q);// complimentservant.giveCompliments(guests.get(compliment));// outcome of the nightguests.forEach(Royalty::changeMood);// check your luckif (servant.checkIfYouWillBeHanged(guests)) {LOGGER.info("{} will live another day", servant.name);} else {LOGGER.info("Poor {}. His days are numbered", servant.name);}}
}
程序输出
Jenkins will live another day
Poor Travis. His days are numbered
🔍类图
🔍适用场景
使用仆人模式场景
- 当我们希望某些对象执行一个公共操作并且不想将该操作定义为每个类中的方法时
🔍Ending
Servant(仆人)模式,也称为Active Object 模式,是一种并发设计模式,用于提供并发对象之间的服务。这种模式将请求和其处理分离开来,以允许并发处理请求,而不需要显式的线程管理。
希望本文能够帮助读者更深入地理解服务模式,在实际项目中发挥其优势。谢谢阅读!
希望这份博客草稿能够帮助到你。如果有其他需要修改或添加的地方,请随时告诉我。