zoukankan      html  css  js  c++  java
  • Abstract factory

    package designPattern;
    //Abstract factory
    //定义生产各个产品的接口
    class MazeFactory{

        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();
        }
    }
    //client use abstract factory and abstract product
    //
    class MazeGame1{
        public Maze CreateMaze(MazeFactory factory)
        {
            Maze aMaze = factory.MakeMaze();
            
            Room r1 = factory.MakeRoom(1);
            Room r2 = factory.MakeRoom(2);
            
            Door theDoor = factory.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);
            
            return aMaze;
        }
    }
    //concrete Factory make concrete product
    //实现一系列的产品的创建
    class EnhantedMazeFactory extends MazeFactory
    {
        public Wall MakeRoom(int n)
        {
            //concrete product of room
            return new EnhantedRoom(n, CastSpell());
        }
        public Door MakeDoor(Room r1, Room r2){
            return new DoorNeedingSpell(r1, r2);
        }
        protected Spell CastSpell();
    }

    class BombMazeFactory extends MazeFactory{
        public Wall MakeWall()
        {
            return new BombWall();
        }
        public Room MakeRoom(int n)
        {
            return new BombRoom(n);
        }
    }

    public class AbstractFactory {
        public static void main(String[] args)
        {
            MazeGame1 game;
            BombMazeFactory factory;
            
            game.CreateMaze(factory);
        }
    }
  • 相关阅读:
    进入javascript的学习了
    详解CSS display:inline-block的应用(转)
    CSS学习第一天
    HTML小结~
    表单提交中的input、button、submit的区别(转来学习)
    第二天又是充实的一天
    java开班啦~步入程序员的第一步
    sql 两大类 DDL数据定义语言 和DCL数据控制语言
    单行函数(日期函数)
    单行函数
  • 原文地址:https://www.cnblogs.com/lidan/p/2239482.html
Copyright © 2011-2022 走看看