1 The Properties Pattern Based on

Slides:



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

Singly linked lists Doubly linked lists
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.
Java Review Interface, Casting, Generics, Iterator.
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.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
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.
instance variables property procedures for the mintQuantity instance variable.
The Properties Pattern
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
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
Java Collections Framework A presentation by Eric Fabricant.
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.
Chapter 4 The Relational Model.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
Composition (Part 2) 1. Class Invariants  class invariant  some property of the state of the object that is established by a constructor and maintained.
Effective Java: Generics Last Updated: Spring 2009.
Data structures and algorithms in the collection framework 1 Part 2.
Chapters (ArrayList / Generic)
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.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
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.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
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.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Chapter 16 UML Class Diagrams.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Maps Nick Mouriski.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
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
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
© 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.
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
Chapter 6: The Stack Abstract Data Type
Collections Framework
Introduction to Data Structure
Preferences.
Preferences.
Hashing in java.util
Preferences.
Preferences.
Preferences.
Preferences.
Presentation transcript:

1 The Properties Pattern Based on

2 Introduction Many names –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

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

4 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

5 public class PropList { private PropList parent; private Map map = new HashMap (); 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); }

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

7 Motivation NoSql Databases (BigTable, HBase, …) –Relational DB: change resistant, do not scale well –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

8 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.

9 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

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

11 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

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

13 public class PropList { public interface Interceptor { public Object onPut(PropList p, String key, Object value); } private List interceptors = new ArrayList (); 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 : p.interceptors) value = i.onPut(p, key, value); map.put(key, value); }

14 Persistence Quite easy if all values are primitive –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 table on a relational DB: id, key, value

15 Motivation: Object-level inheritance Sometimes we want to extend an object! –Here’s an existing instance –I want some 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 ones to change as well –“I want that one” –“Inheritance by example”

16 Example: Disabling of Menus

17 Example: Disabling of Menus (cont.) No open file -> Nine menu items are disabled Class-level inheritance –A superclass, S, with setEnabled(boolean) method –Each menu item is an instance of this superclass –When a file is closed call setEnabled(false) on all nine instances Object-level inheriance –A PropList object, x, with an "enabled" property –Each menu items is a PropList object, with x being the parent –When a file is closed: x.put("enabled", false)