博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java8之工厂模式
阅读量:4230 次
发布时间:2019-05-26

本文共 839 字,大约阅读时间需要 2 分钟。

interface Number {}class One implements Number {}class Two implements Number {}class Three implements Number {}class NumberFactory {    final static Map
> map = new HashMap<>(); static { map.put("one", One::new); map.put("two", Two::new); map.put("three", Three::new); } public static Number createNumber(String number) { Supplier
numberSupplier = map.get(number); if (numberSupplier != null) return numberSupplier.get(); throw new IllegalArgumentException("No such product " + number); }}public class Main { public static void main(String[] args) { Number number = NumberFactory.createNumber("one"); Number number1 = NumberFactory.createNumber("two"); System.out.println(number.getClass()); System.out.println(number1.getClass()); }}

转载地址:http://vmjqi.baihongyu.com/

你可能感兴趣的文章
Java Concurrency in Practice
查看>>
Red Hat Fedora 5 Unleashed
查看>>
AdvancED Flash Interface Design (Advanced Design)
查看>>
Quartz Job Scheduling Framework: Building Open Source Enterprise Applications
查看>>
C++ GUI Programming with Qt 4
查看>>
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications
查看>>
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
查看>>
Moodle E-learning Course Development
查看>>
VoIP For Dummies
查看>>
Administrator's Guide to SQL Server 2005
查看>>
Ajax Design Patterns
查看>>
DNS and BIND (5th Edition)
查看>>
Firewall Fundamentals
查看>>
Learning PHP and MySQL
查看>>
Agile Software Construction
查看>>
Computer Security Basics
查看>>
Sams Teach Yourself MySQL in 10 Minutes
查看>>
Information Systems : The State of the Field
查看>>
IPv6 Essentials
查看>>
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
查看>>