Фабричный метод (Factory Method)
Материал из Вики ИТ мехмата ЮФУ
Другое название
Назначение
Описание
Реализация
Диаграмма классов
Участники
Код
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MazeCommon;
namespace MazeGameFactoryMethod
{
class MazeGame
{
public Maze CreateMaze()
{
Maze aMaze = MakeMaze();
Room r1 = MakeRoom(1);
Room r2 = MakeRoom(2);
Door d = MakeDoor(r1, r2);
aMaze.AddRoom(r1);
aMaze.AddRoom(r2);
r1.SetSide(Direction.North, MakeWall());
r1.SetSide(Direction.East, d);
r1.SetSide(Direction.South, MakeWall());
r1.SetSide(Direction.West, MakeWall());
r2.SetSide(Direction.North, MakeWall());
r2.SetSide(Direction.East, MakeWall());
r2.SetSide(Direction.South, MakeWall());
r2.SetSide(Direction.West, d);
return aMaze;
}
public virtual Maze MakeMaze()
{
return new Maze();
}
public virtual Wall MakeWall()
{
return new Wall();
}
public virtual Room MakeRoom(int n)
{
return new Room(n);
}
public virtual Door MakeDoor(Room r1, Room r2)
{
return new Door(r1, r2);
}
};
class ProgramFactoryMethod
{
static void Main(string[] args)
{
Console.WriteLine("FactoryMethod");
var game = new MazeGame();
Maze m = game.CreateMaze();
}
}
}