状态模式:允许对象在内部状态改变时改变它的的行为.对象看起来好像修改了化的类.
将以超市购物为例--说明状态模式
所有状态抽象类
public abstract class BuyStat {
//在超市状态
public void inShop() {
System.out.println("welcome come our shop,please start shoping");
}
//选购商品状态
public void select() {
System.out.println("you not in shop");
}
//支付状态
public void pay() {
System.out.println("you don't have merchandise,so don't pay!");
}
//商品打包
public void packing() {
System.out.println("you don't have merchandise,so don't packing!");
}
//抽奖
public void winner(){
System.out.println("you don't winner!");
}
}
在超市内
public class InShopStat extends BuyStat {
private Shop shop;
public InShopStat(Shop shop) {
this.shop = shop;
}
@Override
public void inShop() {
// TODO Auto-generated method stub
System.out.println("welcome come our shop,please start shoping");
//设置下一个状态
shop.setStat(shop.getSelectStat());
}
}
选购商品
public class SelectStat extends BuyStat {
private Shop shop;
public SelectStat(Shop shop) {
this.shop = shop;
}
@Override
public void select() {
// TODO Auto-generated method stub
System.out.println("I'm shop merchandise!");
//已选购了商品
shop.setHasMer(true);
//设置下一个状态
shop.setStat(shop.getPayStat());
}
}
支付
public class PayStat extends BuyStat {
private Shop shop;
public PayStat(Shop shop) {
this.shop = shop;
}
@Override
public void inShop() {
// TODO Auto-generated method stub
super.inShop();
}
@Override
public void packing() {
// TODO Auto-generated method stub
if (shop.isHasMer()) {
System.out.println("you don't pay!");
} else {
System.out.println("no merchandise don't packing");
}
}
@Override
public void pay() {
// TODO Auto-generated method stub
//确认是否已选购商品
if (shop.isHasMer()) {
System.out.println("ok,your pay success!");
} else {
System.out.println("you don't have merchandise,so don't pay");
}
shop.setStat(shop.getPackingStat());
}
@Override
public void select() {
// TODO Auto-generated method stub
System.out.println("time is pay,don't select merchandise");
}
}
打包
public class PackingStat extends BuyStat {
private Shop shop;
public PackingStat(Shop shop) {
this.shop = shop;
}
@Override
public void packing() {
// TODO Auto-generated method stub
if (shop.isHasMer()) {
System.out.println("time is packing");
shop.setStat(shop.getWinnerStat());
//清空已购商品状态
shop.setHasMer(false);
} else {
System.out.println("don't packing,so your not merchandise");
shop.setStat(shop.getInShopStat());
}
}
@Override
public void pay() {
// TODO Auto-generated method stub
System.out.println("pay is end");
}
@Override
public void select() {
// TODO Auto-generated method stub
System.out.println("don't select merchandise");
}
}
抽奖
public class WinnerStat extends BuyStat {
Random random = new Random();
private Shop shop;
public WinnerStat(Shop shop) {
this.shop = shop;
}
@Override
public void packing() {
// TODO Auto-generated method stub
System.out.println("your merchandise been packing");
}
@Override
public void pay() {
// TODO Auto-generated method stub
System.out.println("your merchandise been pay");
}
@Override
public void select() {
// TODO Auto-generated method stub
System.out.println("don't select merchandise");
}
@Override
public void winner() {
// TODO Auto-generated method stub
int num = random.nextInt(10);
if (num == 8) {
System.out.println("you are a winner");
} else {
System.out.println("sorry,you are not winner");
}
//初始化
shop.setStat(shop.getInShopStat());
}
}
超市
public class Shop {
private BuyStat selectStat;
private BuyStat payStat;
private BuyStat packingStat;
private BuyStat inShopStat;
private BuyStat winnerStat;
private boolean hasMer;
private BuyStat stat = null;
public Shop() {
selectStat = new SelectStat(this);
payStat = new PayStat(this);
packingStat = new PackingStat(this);
inShopStat = new InShopStat(this);
winnerStat = new WinnerStat(this);
stat = inShopStat;
}
public void inShop() {
stat.inShop();
}
public void select() {
stat.select();
}
public void pay() {
stat.pay();
}
public void packing() {
stat.packing();
}
public void winner(){
stat.winner();
}
public boolean isHasMer() {
return hasMer;
}
public void setHasMer(boolean hasMer) {
this.hasMer = hasMer;
}
public BuyStat getStat() {
return stat;
}
public void setStat(BuyStat stat) {
this.stat = stat;
}
public BuyStat getSelectStat() {
return selectStat;
}
public BuyStat getPayStat() {
return payStat;
}
public BuyStat getPackingStat() {
return packingStat;
}
public BuyStat getInShopStat() {
return inShopStat;
}
public BuyStat getWinnerStat() {
return winnerStat;
}
}
测试
public class ShopTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shop shop = new Shop();
shop.inShop();
shop.select();
shop.pay();
shop.packing();
shop.winner();
}
}
使用状态值主要作用是便于以的新增和修改状态,其他的状态不会受到影响.改变了以前将状态设置为静态变量.扩展时修改很复杂现象.更加容易扩展.
以上相关例子还有一些缺陷,真是作用是理解这个模式的设计原理,能类代理以前的静态变量状态值,以后更容易维护.
分享到:
相关推荐
在IT行业中,设计模式是软件开发中的重要概念,它们代表了在特定场景下解决常见问题的最佳实践。策略模式是设计模式的一种,它允许我们在运行时动态地改变对象的行为。在这个"php设计模式-策略模式-例题学习"的...
标题“tv-w07-my-firsthand-experience-with-ransomware_copy1.zip”暗示了这是一个关于个人经历与勒索软件(Ransomware)遭遇的故事。勒索软件是一种恶意软件,它会加密用户的重要文件,然后要求支付赎金以解密这些...
描述:“my-firsthand-experience-with-ransomware_copy1.pdf”文档是由Capital One公司的网络安全部门经理Birat Niraula所作的一次演讲,他详细介绍了自己与勒索软件的亲身经历,以及勒索软件的攻击途径、近期的...
通过一系列精心设计的练习题和配套答案,你可以系统地学习和掌握SQL的关键概念和语法。 1. SQL基础:首先,你需要了解SQL的基本结构,包括SELECT、FROM、WHERE等关键字,它们是构建查询语句的基础。例如,`SELECT ...
"GM-HandTool-2019-05"便是这样一款专为MapleStory(枫之谷)游戏设计的实用工具集,它旨在帮助玩家和开发者更好地管理和修改游戏文件,从而提升游戏体验。 MapleStory,这款由Nexon开发并发行的2D横向卷轴式网络...
Auto Hand-3.2 - VR Physics Interaction 测试可用
短学期课程设计-数据库原理_Second-hand-book-trading-system
本文主要聚焦于一个具体的安全威胁——勒索软件(Ransomware),并通过名为“TV-W07-my-firsthand-experience-w”的PDF文档,分享了Birat Niraula先生在Capital One任职期间对这一威胁的亲身经历和应对策略。...
UL 60745-1-2022-06-11 Hand-Held Motor-Operated Electric Tools
在本文中,我们将深入探讨一个名为“real-hand-held”的背景抠图模型,其相关权重文件存储于“real-hand-held-20200511T154014Z-001.zip”压缩包中。这个模型专门针对手持物体的情况,提供高质量的抠图效果。 首先...
"Hand-Drawn-Shader-Pack-V1.2.zip"正是一个专为Unity3D设计的手绘风格化Shader资源包,旨在帮助开发者轻松地将游戏或应用呈现出独特的手绘艺术风格。 手绘风格化Shader是一种模拟传统绘画技巧的技术,它可以使3D...
《基于jawide-hand-gesture-recognition的三种手势识别技术详解》 在当今信息化社会,人工智能(AI)已经深入到我们生活的各个角落,其中,手势识别技术作为一种非接触式的交互方式,逐渐受到广泛关注。本篇文章将...
-matlab-hand-written-num-recognization-master
AS-23-Pinto-Hand-Me-Your-Secret-MCU
【标题】"imclab-Hand-...综上所述,"imclab-Hand-Geometry-Recognition-System-Matlab-Code"项目涵盖了图像处理、模式识别、机器学习等多个领域的知识点,对于想深入了解这些领域的开发者来说,这是一个宝贵的资源。
This is a step by step hand-on lab for WF from Microsoft Switzerland. In this Hands-On-Lab you will create a State Machine Workflow and integrate it in an existing ASP.NET Web Application.
hand-keras-yolo3-recognize 模型训练参考:https://gitee.com/cungudafa/keras-yolo3 yolo3识别这里参考于:https://github.com/AaronJny/tf2-keras-yolo3
Mediapipe框架学习之 ———— 手势识别(单手) Android Studio 3.5 工程,打开即可用