Micro-patterns - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Micro-patterns

Description:

Title: Inheritance Created Date: 8/16/2006 12:00:00 AM Document presentation format: On-screen Show (4:3) Other titles: Arial Calibri Office Theme Micro-patterns A ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 20
Provided by: acil150
Category:

less

Transcript and Presenter's Notes

Title: Micro-patterns


1
Micro-patterns
2
A 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

3
Lecture
  • Designator
  • Taxonomy
  • Joiner
  • Pool
  • Function Pointer
  • Function Object
  • Stateless
  • Common State
  • Immutable

4
Degenerate 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
5
Degenerate 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
6
Containment 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
7
Containment 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
8
Containment 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
9
Containment 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
10
Containment 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
11
Containment Data Managers
  • Sink

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

12
Inheritance 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)
13
Inheritance 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()
14
Inheritance 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()
15
Inheritance 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)
16
Inheritance 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
17
Inheritance 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()

18
InheritanceInheritors
  • 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.

19
Results
  • 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
Write a Comment
User Comments (0)
About PowerShow.com