The Properties Pattern

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Copyright by Scott GrissomCh 5 Sophisticated Behavior Slide 1 Java Class Library a very large collection of classes for you to use as needed the challenge.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 110 Introduction to Programming Mr. Joshua Stough.
1 The Properties Pattern Based on
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
Stacks, Queues, and Deques
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Java Collections Framework A presentation by Eric Fabricant.
CS 2511 Fall UML Diagram Types  2 Main Types Structure Diagrams ○ Class Diagrams ○ Component Diagrams ○ Object Diagrams Behavior Diagrams ○
Data Storage: Part 1 (Preferences)
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
Classes and objects Practice 2. Basic terms  Classifier is an element of the model, which specifies some general features for a set of objects. Features.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Data structures and algorithms in the collection framework 1 Part 2.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Chapter 16 UML Class Diagrams.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Classes, Interfaces and Packages
Maps Nick Mouriski.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Collections Dwight Deugo Nesa Matic
C19: Collection Classes (don’t forget to look at all the online code examples)
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
CSE 143 Lecture 11: Sets and Maps reading:
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
abstract data types built on other ADTs
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 22 – April 9, 2002 Subprotocols – static and dynamic.
Chapter 6: The Stack Abstract Data Type
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Collections Framework
Hashing in java.util
Web Design & Development Lecture 6
Presentation transcript:

The Properties Pattern Based on http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html

Introduction Many names Bottom line Prototype Property List Properties Object Adaptive Object Model Bottom line Seems like a low-level hack Actually, a new way to look at the world

Map API get(key) put(key, value) has(key) remove(key) Also: keys()

Properties Pattern API Based on the Map API Enhancement: a parent() link Points at another Properties object Supplies additional properties Map method need to be adjusted

public class PropList { private PropList parent; private Map<String, Object> map = new HashMap<String, Object>(); public PropList(PropList parent) { this.parent = parent; } public Object get(String key) { if(map.containsKey(key)) return map.get(key); if(parent != null) return parent.get(key); return null; public void put(String key, Object value) { map.put(key, value);

public boolean has(String key) { return map.containsKey(key) || parent != null && parent.has(key); } public void remove(String key) { map.remove(key); public Collection<String> keys() { List<String> result = new ArrayList<String>(); result.addAll(map.keySet()); if(parent != null) result.addAll(parent.keys()); return result; public PropList parent() { return parent;

Motivation BigTables Flexible list of properties Relational DB are change resistant New storage facilities are based on triplets: key, attribute, value Property objects are the ideal in-memory counterpart Flexible list of properties Variable user content When the logic is not interested in specific properties It is interested in the relationship between the properties Examples: A movie list browser, protein-peptide browser

Motivation: Object-level inheritance Sometimes we want to extend an object! Here’s an existing instance I want the new instance to be essentially the same as the existing one Except for some additions, changes When the existing instance changes I want the new one to change as well “I want that one” “Inheritance by example”

Example: Object-level Inheritance On-line shop Inventory, customers, products, orders A basic order class Specifies customer, product, quantity Possible subclasses Presentation: HtmlFormattedOrder, TextFormattedOrder Logic: SpecialProductOrder, PremiumCustomerOrder We want the same object to be extended several times All extension should be built on top the same data

org.eclipse.jdt.core.dom.ASTNode Each AST node is capable of carrying an open-ended collection of client-defined properties. Newly created nodes have none. getProperty and setProperty are used to access these properties.

Implementation Representation of keys String (almost always) Arbitrary objects Instances of a Key class String + Numbers Representation of key-value pairs Linked list of pairs Hashtable

Deletion Three alternative Remove from current instance Remove also from its complete parent chain Remove replaces the value with a special “not here” value

public class PropList { private static final Object NOT_HERE = new Object(); public void remove(String key) { map.put(key, NOT_HERE); } public Object get(String key) { if(map.containsKey(key)) { Object o = map.get(key); if(o == NOT_HERE) o = null; return o; if(parent != null) return parent.get(key); return null; // Similar changes in has() method

Safety & Invariants A parent is notified of put() actions on any child Can veto the change

public class PropList { public interface Interceptor { public Object onPut(PropList p, String key, Object value); } private List<Interceptor> interceptors = new ArrayList<Interceptor>(); public void addInterceptor(Interceptor i) { interceptors.add(i); public void put(String key, Object value) { for(PropList p = this; p != null; p = p.parent) for(Interceptor i : interceptors) value = i.onPut(p, key, value); map.put(key, value);

Persistence Quite easy if all values are primitive (Otherwise) E.g.: One key/value pair per line (Otherwise) Maintain a unique id property (primitive or string) Saving If value is another PropList save its id Loading Build an id -> PropList map In each PropList replace properties that hold an id with the actual reference Maps nicely to a ternary tables on a relational DB: id, key, value