ChessFlyweightFactory.dart 737 B

123456789101112131415161718192021222324252627282930313233
  1. import 'dart:collection';
  2. import 'package:gobang/flyweight/Chess.dart';
  3. /// 棋子的享元工厂,采用单例模式
  4. class ChessFlyweightFactory {
  5. ChessFlyweightFactory._();
  6. static ChessFlyweightFactory? _factory;
  7. static ChessFlyweightFactory getInstance() {
  8. if (_factory == null) {
  9. _factory = ChessFlyweightFactory._();
  10. }
  11. return _factory!;
  12. }
  13. HashMap<String, Chess> _hashMap = HashMap<String, Chess>();
  14. Chess getChess(String type) {
  15. Chess chess;
  16. if (_hashMap[type] != null) {
  17. chess = _hashMap[type]!;
  18. } else {
  19. if (type == "white") {
  20. chess = WhiteChess();
  21. } else {
  22. chess = BlackChess();
  23. }
  24. _hashMap[type] = chess;
  25. }
  26. return chess;
  27. }
  28. }