Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a behavioral model?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is a behavioral model". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Null object mode

It can be said that this model is rarely used, at least before I really studied it, or even heard of it. In the null object mode, an empty object replaces the check of the NULL object instance. Instead of checking for control, the NULL object reflects a relationship that does nothing.

AbstractClass: an abstract object with certain logic

RealClass: the real object

NullObject: empty object

Empty object UML

Null object mode

AbstractClass

Public abstract class AbstractClass {public abstract void print (); public abstract void isNullObject ();}

RealClass

Public class RealClass extends AbstractClass {@ Override public void print () {System.out.println ("real class: hello world");} @ Override public void isNullObject () {System.out.println ("i am real object");}}

NullClass

Public class NullClass extends AbstractClass {@ Override public void print () {System.out.println ("do nothing");} @ Override public void isNullObject () {System.out.println ("do nothing");}}

To facilitate the demonstration, a factory class has been added to randomly generate RealClass or NullClass

ObjectFactory

Public class ObjectFactory {public static AbstractClass getObjectClass () {if (Math.random () * 10 > 4) {return new RealClass ();} return new NullClass ();}}

Client

Public class Client {public static void main (String [] args) {for (int I = 0; I)

< 10; i++) { ObjectFactory.getObjectClass().isNullObject(); ObjectFactory.getObjectClass().print(); } }} 就是这么朴实无华,但是确实是用得比较少,上网一搜甚至很多标题都是被遗弃的设计模式。 访问者模式 访问者模式的目的是要把处理从数据结构中分离出来,其优点是增加新的操作很容易,因为增加新的操作就以为着增加一个新的访问者。 通俗地来说,把数据抽象成一类,把操作抽象成一类。拿王者荣耀举例,最典型的就是英雄算是数据,玩家就是访问者,新增一个玩家只需要新增一个具体访问者即可。 Element: 抽象出来的元素,里面有个accept方法来接受访问者的访问,对应例子就是英雄的抽象类 Concrete Element: 具体的元素,对应就是具体的某个英雄 Visitor: 访问者,一般需要包含访问所有具体元素的抽象方法,对应游戏就是玩家的抽象类 Concrete Element: 具体的访问者,对应到例子就是不同的具体的玩家 访问者UML

Visitor mode

Visitor

Public abstract class VisitorPlayer {public abstract void visit (ConcreteElementGuangYu guangYu); public abstract void visit (ConcreteElementMaKe maKe);}

Element

Public abstract class ElementHero {public abstract void accept (VisitorPlayer visitorPlayer); public void normalAttack () {System.out.println ("Pudong Rooster");} public abstract void firstSkill (); public abstract void secondSkill (); public abstract void thirdSkill ();}

ConcreteElement

Public class ConcreteElementGuangYu extends ElementHero {@ Override public void accept (VisitorPlayer visitorPlayer) {System.out.println ("you have chosen a hero"); visitorPlayer.visit (this);} @ Override public void firstSkill () {System.out.println ("going to the meeting alone");} @ Override public void secondSkill () {System.out.println ("Azure Dragon Yan Yue") } @ Override public void thirdSkill () {System.out.println ("Blade Iron Rider");}} public class ConcreteElementMaKe extends ElementHero {@ Override public void accept (VisitorPlayer visitorPlayer) {System.out.println ("you have chosen the hero"); visitorPlayer.visit (this);} @ Override public void firstSkill () {System.out.println ("gorgeous revolver") } @ Override public void secondSkill () {System.out.println ("roaming gun");} @ Override public void thirdSkill () {System.out.println ("frenzied on-screen comment");}}

ConcreteVisitor

Public class ConcreteVisitorPlayerA extends VisitorPlayer {@ Override public void visit (ConcreteElementGuangYu guangYu) {guangYu.thirdSkill (); guangYu.secondSkill (); guangYu.normalAttack (); guangYu.firstSkill ();} @ Override public void visit (ConcreteElementMaKe maKe) {maKe.secondSkill (); maKe.firstSkill (); maKe.normalAttack () MaKe.normalAttack (); maKe.thirdSkill ();}} public class ConcreteVisitorPlayerB extends VisitorPlayer {@ Override public void visit (ConcreteElementGuangYu guangYu) {guangYu.normalAttack (); guangYu.normalAttack (); guangYu.firstSkill (); guangYu.normalAttack ();} @ Override public void visit (ConcreteElementMaKe maKe) {maKe.secondSkill (); maKe.firstSkill (); maKe.normalAttack () MaKe.firstSkill (); maKe.secondSkill ();}}

For ease of use, there is usually an ObjectStructure class, which is mainly used to build structural relationships

ObjectStructure

Public class ObjectStructure {private List elementHeroes = new ArrayList (); public void attach (ElementHero hero) {elementHeroes.add (hero);} public void detach (ElementHero hero) {elementHeroes.remove (hero);} public void accept (VisitorPlayer player) {for (ElementHero elementHero: elementHeroes) {elementHero.accept (player);}

Client

Public class Client {public static void main (String [] args) {ObjectStructure objectStructure = new ObjectStructure (); objectStructure.attach (new ConcreteElementGuangYu ()); objectStructure.attach (new ConcreteElementMaKe ()); ConcreteVisitorPlayerA concreteVisitorPlayerA = new ConcreteVisitorPlayerA (); ConcreteVisitorPlayerB concreteVisitorPlayerB = new ConcreteVisitorPlayerB (); System.out.println ("player A starts to operate"); objectStructure.accept (concreteVisitorPlayerA) System.out.println ("player B starts to operate"); objectStructure.accept (concreteVisitorPlayerB);}} this is the content of "what is a behavioral mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report