Java Memo mode (Liar design mode)

Source: Internet
Author: User

Memo mode, to the author's feelings, we are seldom used in the actual development, at least the author is useless. Generally in the game background development should often use it! Of course it's just

The author's inference. First look at the class diagram

Liar design mode-Class diagram

Just keep looking at the demo.

/*** Progress information classes that need to be saved*/ Public classrolesatememento{PrivateInteger ATK; PrivateInteger Vit; PrivateInteger attack;  Publicrolesatememento (Integer atk, integer vit, integer attack) {Super();  This. ATK =ATK;  This. Vit =Vit;  This. Attack =attack; }     PublicInteger getatk () {returnATK; }     Public voidsetatk (Integer ATK) { This. ATK =ATK; }     PublicInteger Getvit () {returnVit; }     Public voidSetvit (Integer vit) { This. Vit =Vit; }     PublicInteger Getattack () {returnattack; }     Public voidSetattack (Integer attack) { This. Attack =attack; }}
/***/Publicclass  rolerstatecaretaker    {private rolesatememento Rolesatememento;      Public Rolesatememento Getrolesatememento () {        return  rolesatememento;    }      Public void Setrolesatememento (Rolesatememento rolesatememento) {        this. Rolesatememento=  Rolesatememento;}    }
/*** Character class*/ Public classRolerinfo {PrivateInteger ATK; PrivateInteger Vit; PrivateInteger attack;  Publicrolerinfo (Integer atk, integer vit, integer attack) {Super();  This. ATK =ATK;  This. Vit =Vit;  This. Attack =attack; }     Public Staticrolerinfo Initroler () {return NewRolerinfo (100, 100, 100); }     Public voidshowstate () {System.out.println ("Current blood Volume:" +vit); System.out.println ("Current attack:" +ATK); System.out.println ("Current defenses:" +attack); }     PublicRolesatememento Saverolerinfo () {return NewRolesatememento (ATK, Vit, attack); }     Public voidRestoremenmento (Rolesatememento rolesatememento) { This. Vit =Rolesatememento.getvit ();  This. ATK =rolesatememento.getatk ();  This. Attack =Rolesatememento.getattack (); }     PublicInteger getatk () {returnATK; }     Public voidsetatk (Integer ATK) { This. ATK =ATK; }     PublicInteger Getvit () {returnVit; }     Public voidSetvit (Integer vit) { This. Vit =Vit; }     PublicInteger Getattack () {returnattack; }     Public voidSetattack (Integer attack) { This. Attack =attack; }}
/*** Client*/ Public classTest { Public Static voidMain (string[] args) {rolerinfo role=Rolerinfo.initroler (); System.out.println ("----------before----------battle boss.");        Role.showstate (); //Keep ProgressRolerstatecaretaker caretaker =NewRolerstatecaretaker ();        Caretaker.setrolesatememento (Role.saverolerinfo ()); //Battle BossROLE.SETATK (0); Role.setattack (0); Role.setvit (0); System.out.println ("----------vs Boss----------");        Role.showstate (); //Recovery ArchiveRole.restoremenmento (Caretaker.getrolesatememento ()); System.out.println ("----------Recovery----------");    Role.showstate (); }}

Run results

Before----------Battle boss--------------------battle boss----------0 0  0----------Recovery----------100

A simple look at the memory class and the class responsible for preserving memory is actually a few attributes, and then there are set,get methods, not complex, the main logic is in the role class. I was looking at the design pattern before

has been misunderstood as to what role classes inside the attributes and memory classes inside the same attributes, and not let the character class inherit memory class? Later, the reason is that there are many attributes in the character that do not need to be saved. Make an analogy.

The sex of a character doesn't have to be denatured after the battle boss, so sex doesn't need to be saved, so there's no inheritance, memory is just the property that needs to be saved.

The above is the author's understanding of the memorandum model, hoping to help the learning of small partners. In addition, I strongly recommend to learn the small partner to look at the big talk design patterns, and then understand the essence of their own in the manual write code.

Java Memo mode (Liar design mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.