zoukankan      html  css  js  c++  java
  • Factory Methods

    package designPattern;

    //creator
    class MazeGame{
        public Maze CreateMaze()
        {
            Maze aMaze = MakeMaze();
            
            Room r1 = MakeRoom(1);
            Room r2 = MakeRoom(2);
            
            Door theDoor = MakeDoor(r1, r2);

            aMaze.AddRoom(r1);
            aMaze.AddRoom(r2);

            r1.SetSide(North, MakeWall());
            r1.SetSide(East, theDoor);
            r1.SetSide(South, MakeWall());
            r1.SetSide(West, MakeWall());
            
            r2.SetSide(North, MakeWall());
            r2.SetSide(East, MakeWall());
            r2.SetSide(South, MakeWall());
            r2.SetSide(West, theDoor);
        }
        
        //factory Methods
        public Maze MakeMaze()
        {
            return new Maze();
        }
        public Door MakeDoor(Room r1, Room r2)
        {
            return new Door(r1, r2);
        }
        public Room MakeRoom(int n)
        {
            return new Room(n);
        }
        public Wall MakeWall()
        {
            return new Wall();
        }
    }

    //concrete creator
    class BombMazeGame extends MazeGame{
        public Wall MakeWall()
        {
            //concrete product
            return new BombWall();
        }
        public Room MakeRoom(int n)
        {
            return new BombRoom(n);
        }
    }

    //concrete creator
    class EnhantedMazeGame extends MazeGame{
        public Wall MakeRoom(int n)
        {
            return new EnhantedRoom(n, CastSpell());
        }
        public Door MakeDoor(Room r1, Room r2){
            return new DoorNeedingSpell(r1, r2);
        }
        protected Spell CastSpell();
    }

    public class FactoryMethods {

    }


  • 相关阅读:
    xrange和range区别
    bool([x]) 将x转换为Boolean类型
    bin(x) 将整数x转换为二进制字符串
    chr(i) 返回整数i对应的ASCII字符
    音乐欣赏
    迅雷下载百度云引发的“事故”
    swift获取图片路径出错
    记号笔写在白板上引起的尴尬而又无奈的事件
    swift 3新特性总结
    watch
  • 原文地址:https://www.cnblogs.com/lidan/p/2239480.html
Copyright © 2011-2022 走看看