Monster Factory-Factory mode

Source: Internet
Author: User

In the prototype model simulation game monster scene, the user every click a button to simulate the game scene changes, the programmer all need to manually new one object, the coupling is too high, we need to understand each monster class implementation method.

Factory mode provides a separation of interfaces and algorithms to reduce coupling. The caller wants to create an object by delegating the name of the object you want to create to the factory, which is returned by the factory to the caller based on the object name. This is the same as the production of products in our real life, we do not need to understand the specific process of product generation, only need to entrust the order to the factory, the factory is responsible for producing products and delivery to us.

In the above simulation game, after the user switches the scene, the factory continues to produce monsters for the player to entertain (as if it is a strange expression) according to the scene, the class diagram is as follows:

650) this.width=650; "Src=" Http:// -wmp_4-s_4175870029.png "title=" Factory mode. png "alt=" wkiol1hofidg1e-uaaav9mvkoho658.png-wh_50 "/>

Specific code:

Var monster = class.extend ({_blood:100,_att:0,ctor:function (Blood,att) {var self =  this;self._blood = blood;self._att = att;},attack:function (player) {Console.log ("pair"  +  "Cause"  + this._att +  "point Damage");},is_die:function () {return this._blood  > 0;},attacked:function (ATT) {This._blood -= att;}}); Var mouse = monster.extend ({ctor:function (Blood,att) {this._super (Blood,att);},attack:function ( Player) {Console.log ("Mouse class to"  + player +  "cause"  + this._att +  "point Damage"); Attacked:function (att) {console.log ("Mouse class defense, resulting"  +  (att - 1)  +  "damage");}); Var sharp = monster.extend ({ctor:function (Blood,att) {_super (Blood,att);},attack:function (player {Console.log ("Shark class to"  + player +  "cause"  + this._att +  "point Damage")},attacked: function (att) {console.log ("Shark class defense, resulting"  +  (att - 2)  +  "point Damage");}); Var snack = monster.extend ({ctor:function (Blood,att) {_super (Blood,att);},attack:function (player {Console.log ("snake-like"  + player +  "cause"  + this._att +  "point Damage");},attacked: function (att) {console.log ("snake defense, resulting"  +  (att - 3)  +  "damage");}); Var monsterfactory = class.extend ({factory:{"house": {name: "Mouse", blood:100,att:20}, "Forest": { Name: "Snack", blood:120,att:10}, "Sea": {name: "Shark", Blood:100,att:10}},ctr_monster:function (scene) {Var obj  = this.factory[scene];return eval ("new "  + +  "(" +obj.blood+   "," +obj.att+ ");});

Caller Call Code:

(function () {var factory = new Monsterfactory ();    var mouse = Factory.ctr_monster ("house");   Mouse.attack ("Xiao Zhang");       })(); The advantages of the factory model are obvious, which is to reduce coupling, increase scalability, add or modify a scene-generated monster, just add or modify the values in the factory configuration. Java or.    NET can also be implemented by a reflection mechanism similar to the above configuration-based functionality. The shortcomings of the factory model are also obvious, new products need new factories, increase the number of classes, to a certain extent, increase the complexity of the system.

This article from "Go one stop two look back three" blog, please make sure to keep this source

Monster Factory-Factory 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: 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.