温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java模板模式的示例分析

发布时间:2021-08-10 09:12:04 来源:亿速云 阅读:109 作者:小新 栏目:开发技术

这篇文章主要介绍java模板模式的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

    Java设计模式-模板模式

    什么是模板模式?

    模板模式,顾名思义,就是通过模板拓印的方式。

    定义模板,就是定义框架、结构、原型。定义一个我们共同遵守的约定。

    定义模板,我们的剩余工作就是对其进行充实、丰润,完善它的不足之处。

    定义模板采用抽象类来定义,公共的结构化逻辑需要在抽象类中完成,只将非公共的部分逻辑抽象成抽象方法,留待子类充实实现。

    下面首先通过一个简单的程序来分析一下,例如:现在有三种类型:猪、机器人、人;

    • 猪具备三种功能:吃、睡、跑

    • 机器人又两个功能:吃、工作

    • 人具备四个功能:吃、睡、跑、工作。

    现在就要求设计一个程序,可以让这三类不同的类型,进行工作。现在给出的三个类实际上并没有任何联系

    UML图:

    java模板模式的示例分析

    源代码:

    abstract class Action{
    	public static final int EAT = 1;
    	public static final int SLEEP = 5;
    	public static final int RUN = 20;
    	public static final int WORK = 30;
    	public abstract void eat();
    	public abstract void sleep();
    	public abstract void run();
    	public abstract void work();
    	public void order(int flag){
    		switch(flag){
    			case EAT:
    				this.eat();
    				break;
    			case SLEEP:
    				this.sleep();
    				break;
    			case RUN:
    				this.run();
    				break;
    			case WORK:
    				this.work();
    				break;
    			case EAT+WORK:
    				this.eat();
    				this.work();
    				break;
    			case EAT+WORK+RUN+SLEEP:
    				this.eat();
    				this.sleep();
    				this.run();
    				this.work();
    				break;
    			case EAT+RUN+SLEEP:
    				this.eat();
    				this.sleep();
    				this.run();
    				break;
    		}
    	}
    }
    class Person extends Action{
    	public void eat(){
    		System.out.print("人吃,");
    	}
    	public void sleep(){
    		System.out.print("人睡,");
    	}
    	public void run(){
    		System.out.print("人跑,");
    	}
    	public void work(){
    		System.out.print("人工作,");
    	}
    }
    class Pig extends Action{
    	public void eat(){
    		System.out.print("猪吃,");
    	}
    	public void sleep(){
    		System.out.print("猪睡,");
    	}
    	public void run(){
    		System.out.print("猪跑,");
    	}
    	public void work(){}
    }
    class Robet extends Action{
    	public void eat(){
    		System.out.print("机器人吃,");
    	}
    	public void sleep(){}
    	public void run(){}
    	public void work(){
    		System.out.print("机器人工作,");
    	}
    }
    public class MoBan{
    	public static void main(String args[]){
    	/*	
    人吃,人睡,人跑,人工作,
    猪吃,猪睡,猪跑,
    机器人吃,机器人工作,
    	*/
    		Action ren = new Person();
    		ren.order(Action.EAT+Action.SLEEP+Action.RUN+Action.WORK);
    		System.out.println();
    		Action pig = new Pig();
    		pig.order(Action.EAT+Action.SLEEP+Action.RUN);
    		System.out.println();
    		Action robet = new Robet();
    		robet.order(Action.EAT+Action.WORK);
    	}
    }

    实际上通过此程序的定义结构你可以清楚的发现一个问题:

    • 抽象类在实际的使用过程之中会定义一些固化的模式,它只能接受几种特定的指令;但是每种指定的具体实现由子类负责完成,我们父类只是做了方法的约定。

    以上是“java模板模式的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI