chess_flyweight_factory.dart 555 B

123456789101112131415161718192021222324
  1. import 'package:gobang/models/chess.dart';
  2. /// 棋子享元工厂(单例)
  3. class ChessFlyweightFactory {
  4. ChessFlyweightFactory._();
  5. static final ChessFlyweightFactory instance = ChessFlyweightFactory._();
  6. static ChessFlyweightFactory getInstance() => instance;
  7. final Map<String, Chess> _cache = {};
  8. Chess getChess(String type) {
  9. return _cache.putIfAbsent(type, () {
  10. switch (type) {
  11. case 'white':
  12. return WhiteChess();
  13. case 'black':
  14. default:
  15. return BlackChess();
  16. }
  17. });
  18. }
  19. }