Title: Micro-patterns
1Micro-patterns
2A Micro-Pattern Is
- A set of formal conditions on
- Type
- Attributes
- Structure
- of Java class and its components
- Additionally, it should be
- Purposeful
- Limiting
- Automatically detectable
- Simple
3Lecture
- Designator
- Taxonomy
- Joiner
- Pool
- Function Pointer
- Function Object
- Stateless
- Common State
- Immutable
4Degenerate Classes Controlled creation
- Restricted Creation
- Class has no public constructor
- Class has at least one static field of the same
type as class
import javax.swing.JFrame public class
LoginFrame extends JFrame private static
LoginFrame loginFrame new LoginFrame() //
the constructor private LoginFrame ()
this.setSize(400, 100) this.setTitle("Welco
me") this.setDefaultCloseOperation(JFrame.HIDE_
ON_CLOSE) public static LoginFrame
getLoginFrame() return loginFrame
5Degenerate Classes Controlled creation
- Sampler
- Class has at least one public constructor
- Class has at least one static field of the same
type as class
public class Color implements Paint, Serializable
public static final Color white new
Color(0xffffff, false) public static final
Color black new Color(0x000000, false)
public Color(int red, int green, int blue)
this(red, green, blue, 255) public
Color(int red, int green, int blue, int alpha)
value (alpha ltlt 24) (red ltlt 16)
(green ltlt 8) blue
6Containment Wrappers
- Box
- Class has exactly one instance field
- Class has at least one method for the instance
field mutation
public class CRC32 implements Checksum private
int crc 0 private static int crc_table
make_crc_table() public long getValue()
return (long)crc 0xffffffffL public
void update(int bval) int c crc c
crc_table(cbval) 0xff (c gtgt 8) crc
c
7Containment Wrappers
- Canopy
- Class has exactly one instance field
- Class assign the instance field during
construction only
public class DoubleConstant extends Constant
private final double _value public
DoubleConstant(Constant pool, double value)
super(pool) _value value public
double getValue() return _value
public String toString() return
"CONSTANT_Double " _value
8Containment Wrappers
- Compound box
- Class has exactly one non-primitive instance
field - Class has at least one primitive field
public class Vector extends AbstractList
implements List, RandomAccess, Cloneable,
Serializable protected Object elementData
protected int elementCount public
synchronized boolean removeElement(Object obj)
int idx indexOf(obj, 0) if (idx gt 0)
remove(idx) return true return
false
9Containment Data Managers
- Record
- Class has no methods
- Class has only public fields
public class StudentInfo public String
name public int id public boolean
isFirstDegree public int marks public
String courses public StudentInfo (String
name, int id) this.name name this.id
id
10Containment Data Managers
- Data manager
- Class has only get/set methods
public class StudentInfo private String
name private String surname public
StudentInfo (String name, String surname)
this.name name this.surname
surname public String getName()
System.out.println(Student name
name) return name public void
setName(String name) System.out.println(Stud
ent new name name) this.name name
11Containment Data Managers
package java.util.jar public class JarEntry
extends ZipEntry Attributes attr Certificate
certs CodeSigner signers public
JarEntry(String name) super(name) public
JarEntry(ZipEntry ze) super(ze) public
JarEntry(JarEntry je) this((ZipEntry)je) th
is.attr je.attr this.certs
je.certs this.signers je.signers public
Attributes getAttributes() throws IOException
return attr public Certificate
getCertificates() return certs Epublic
CodeSigner getCodeSigners() return signers
12Inheritance Base Classes
- Outline
- Class has at least two non-abstract method that
calls an abstract method
abstract public class UserFrame extends JFrame
abstract public void setSize() abstract
public void setTitle() public void
buildUserFrame() this.setSize()
this.setTitle() public void
buildAndShowUserFrame() this.setSize()
this.setTitle() this.setVisible(true)
13Inheritance Base Classes
- Trait
- Class has no instance fields
- Class has at least one abstract method
- Class has al least one non-abstract method
package java.lang import java.io.Serializable
public abstract class Number implements
Serializable public byte byteValue() return
(byte)intValue() public short shortValue()
return (short)intValue() public abstract
double doubleValue() public abtract float
floatValue() public abstract int
intValue() public abstract long longValue()
14Inheritance Base Classes
- State Machine
- Class has no methods with parameters
public interface GraphAttributes extends
Attributes public int getGirth() public int
getRadius() public int getDiameter() public
Vertex getCenterVertices() public int
getEccentricities() public int
getMaximumFragmentSizes() public int
getBestFragmentSize() public Vertex
getBestFragmenters()
15Inheritance Base Classes
- Pure Type
- Class has no static members
- Class has no fields
- Class has at least one method
- Class has only abstract methods
public interface AnnotatedElement
Annotation getAnnotation(Class
annotationClass) Annotation
getAnnotations() Annotation
getDeclaredAnnotations() boolean
isAnnotationPresent(Class annotationClass)
16Inheritance Base Classes
- Augmented Type
- Class has at least three static final fields of
the same type only - Class has at least one method
- Class has only abstract methods
public interface Statement int
CLOSE_CURRENT_RESULT 1 int KEEP_CURRENT_RESULT
2 int CLOSE_ALL_RESULTS 3 int
SUCCESS_NO_INFO -2 int executeUpdate(String
sql) throws SQLException void
setMaxFieldSize(int max) throws SQLException
int getMaxRows() throws SQLException
17Inheritance Base Classes
- Pseudo Class
- Class has no instance fields
- Class has only abstract methods
public abstract class Dictionary public
Dictionary() public abstract Enumeration
elements() public abstract Object get(Object
key) public abstract Enumeration keys()
18InheritanceInheritors
- Implementor
- All class public methods implement superclasses
abstract methods. - Overrider
- All class public methods override superclasses
methods. - Extender
- All class public methods are additional to the
superclasses methods.
19Results
- The power of simplicity
- One in ten classes is a wrapper of a single
instance field - One in seven classes has no instance state
- One in seven classes has no mutable state
- One in seven classes is a sink
- The bottom line More than 40 of all classes
match at least one of the above descriptions - Benefits
- Type of documentation
- Reuse of proved design solutions
- Enhance the uniformity of code