Prototype mode (PROTOTYPE) (Create mode)

Source: Internet
Author: User

Reference Documentation:

1. Design mode-the basis of reusable object-oriented software

2.http://www.cnblogs.com/itteacher/archive/2012/12/02/2797857.html (Java Design pattern IV: Prototype mode (Prototype pattern))


Prototype mode (PROTOTYPE) (Create mode)

Theoretical knowledge can be referenced in reference documents;

Look at the format of prototype:



Code implementation:

Mazeprototypefactory.java:
Package com.rick.designpattern.prototype;

/**
 * Created by MyPC on 2017/6/22.
 */Public
class Mazeprototypefactory extends Mazefactory {
    private Maze _prototypemaze;
    Private Wall _prototypewall;
    Private _prototyperoom;
    Private Door _prototypedoor;

    Public Mazeprototypefactory (Maze Maze, Wall Wall, the guest-guest, Door Door) {
        this._prototypemaze = Maze;
        This._prototypewall = wall;
        This._prototyperoom = the hostel;
        This._prototypedoor = door;
    }

    Public Maze Makemaxe () {
        return new Maze ();
    }

    Public Wall Makewall () {
        return _prototypewall.clone ();
    }

    public makeroom (int n) {
        return new (n);
    }

    Public Door Makedoor (guest r1, guest R2) {
        Door Door = _prototypedoor.clone ();
        Door.initialize (R1, r2);
        return door;
    }

}
Wall.java:
Package com.rick.designpattern.prototype;

/** *
 Wall
 * <p>
 * Created by MyPC on 2017/6/20.
 */Public
class Wall {public

    Wall () {

    } public

    Wall (Wall other) {
        
    } public

    Wall clone () {
        return new Wall (this);
    }
}
Door.java:
Package com.rick.designpattern.prototype;

/** *
 Door
 * <p>
 * Created by MyPC on 2017/6/20.
 */Public
class Door {
    private r1, R2;

    Public Door () {

    } publicly

    Door (Door other) {
        r1 = other.r1;
        r2 = other.r2;
    }

    public void Initialize (R1, guest R2) {
        this.r1 = r1;
        THIS.R2 = r2;
    }

    Public Door Clone () {
        return to new Door (this);
    }

    Public Door (R1, r2) {
        this.r1 = r1;
        THIS.R2 = r2;
    }
}
Bombedwall.java:
Package com.rick.designpattern.prototype;

/**
 * Created by MyPC on 2017/6/20.
 */Public
class Bombedwall extends Wall {
    private boolean _bomb;

    Public Bombedwall () {
    } publicly

    Bombedwall (Bombedwall other) {
        _bomb = Other._bomb;
    }

    Public Bombedwall Clone () {
        return to new Bombedwall (this);}
}
Client.java:
Package com.rick.designpattern.prototype;

/**
 * Created by MyPC on 2017/6/20.
 */Public
class Client {public

    static void Main (string[] args) {
        mazegame mazegame = new Mazegame ();
        Mazeprototypefactory mazeprototypefactory =
                new Mazeprototypefactory (New Maze (), New Wall (), new (1), New Door () );

        Mazegame.createmaze (mazeprototypefactory);

        Mazeprototypefactory MazePrototypeFactory2 =
                new Mazeprototypefactory (New Maze (), New Bombedwall (), New Roomwithabomb (1), New Door ());

        Mazegame.createmaze (MazePrototypeFactory2);
    }
}


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.