7-API Design and Framework Version 1.2 - 2016-12-01
7-API Design and Framework Version 1.2 - 2016-12-01
Contents
• API Design
• Framework
1
4/10/2017
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
What is an API?
2
4/10/2017
3
4/10/2017
• Easy to learn
• Easy to use, even without documentation
• Hard to misuse
• Easy to read and maintain code that uses it
• Sufficiently powerful to satisfy requirements
• Easy to evolve
• Appropriate to audience
4
4/10/2017
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
Gather requirements–skeptically
5
4/10/2017
6
4/10/2017
7
4/10/2017
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
8
4/10/2017
9
4/10/2017
10
4/10/2017
11
4/10/2017
Documentation matters
Document religiously
12
4/10/2017
13
4/10/2017
• Do what is customary
– Obey standard naming conventions
– Avoid obsolete parameter and return types
– Mimic patterns in core APIs and language
• Take advantage of API-friendly features
– Generics, varargs, enums, functional interfaces
• Know and avoid API traps and pitfalls
– Finalizers, public static final arrays, etc.
• Don’t transliterate APIs
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
14
4/10/2017
Minimize mutability
15
4/10/2017
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
16
4/10/2017
17
4/10/2017
18
4/10/2017
19
4/10/2017
20
4/10/2017
21
4/10/2017
Contents
• API Design
– Introduction to APIs: Application Programming
Interfaces
– The Process of API Design
– General Principles
– Class Design
– Method Design
– Exception Design
• Framework
22
4/10/2017
23
4/10/2017
Contents
• API Design
• Framework
– Describe example well-known example frameworks
– Know key terminology related to frameworks
– White-box vs Black-Box frameworks
– Use vs Reuse: domain engineering
– Framework mechanics
24
4/10/2017
25
4/10/2017
26
4/10/2017
Class-level reuse
27
4/10/2017
Contents
• API Design
• Framework
– Describe example well-known example frameworks
– Know key terminology related to frameworks
– White-box vs Black-Box frameworks
– Use vs Reuse: domain engineering
– Framework mechanics
Terminology: Libraries
28
4/10/2017
Terminology: Frameworks
29
4/10/2017
30
4/10/2017
31
4/10/2017
32
4/10/2017
Framework or library?
• Java Collections
• Eclipse
• The Java Logging Framework
• Java Encryption Services
• Wordpress
• Ruby on Rails
33
4/10/2017
More terms
More terms
34
4/10/2017
Contents
• API Design
• Framework
– Describe example well-known example frameworks
– Know key terminology related to frameworks
– White-box vs Black-Box frameworks
– Use vs Reuse: domain engineering
– Framework mechanics
Whitebox frameworks
35
4/10/2017
Blackbox frameworks
36
4/10/2017
37
4/10/2017
38
4/10/2017
Contents
• API Design
• Framework
– Describe example well-known example frameworks
– Know key terminology related to frameworks
– White-box vs Black-Box frameworks
– Use vs Reuse: domain engineering
– Framework mechanics
39
4/10/2017
40
4/10/2017
Domain engineering
41
4/10/2017
Contents
• API Design
• Framework
– Describe example well-known example frameworks
– Know key terminology related to frameworks
– White-box vs Black-Box frameworks
– Use vs Reuse: domain engineering
– Framework mechanics
42
4/10/2017
Running a framework
43
4/10/2017
• Observer design
public class Application {
pattern is commonly private List<Plugin> plugins;
used public Application(List<Plugin> plugins) {
this.plugins = plugins;
• Plugins can register for (Plugin p : plugins)
for events p.setApplication(this);
}
• Multiple plugins can public Message processMsg(Message msg) {
react to same events for (Plugin p : plugins)
• Different interfaces for msg = p.process(msg);
...
different events return msg;
possible }
}
44
4/10/2017
• plugin.xml
– Main configuration file
– XML format
– Lists extension points
• Editor extension
– extension point:
org.eclipse.ui.editors
– file extension
– icon used in corner of
editor
– class name
– unique id
• refer to this editor
• other plugins can extend
with new menu items,
etc.!
• At last, code!
• XMLEditor.java
– Inherits TextEditor behavior
• open, close, save, display, select,
• cut/copy/paste, search/replace, …
• REALLY NICE not to have to
implement this
• But could have used ITextEditor
interface if we wanted to
– Extends with syntax highlighting
• XMLDocumentProvider partitions
into tags and comments
• XMLConfiguration shows how to
color partitions
45
4/10/2017
Learning a framework
• Documentation
• Tutorials, wizards,
and examples
• Other client
applications and
plugins
• Communities, email
lists and forums
46
4/10/2017
Summary
References
• Course: Principles of Software Construction,
Carnegie Mellon University
• Craig Larman. Applying UML and Patterns: An
Introduction to Object‐Oriented Analysis and Design
and Iterative Development. 3rd Edition. Prentice Hall.
2004. ISBN 0‐13‐148906‐2
• Bloch, Joshua. Effective Java, Second Edition. Addison-
Wesley, ISBN 978-0321356680
• Alan Shalloway and James Trott. Design Patterns
Explained: A New Perspective on Object-Oriented
Design (2nd Ed.)
• Gamma et al. Design Patterns: Elements of Reusable
Object-Oriented Software. Addison Wesley. ISBN 0-
201-63361-2
47
4/10/2017
48