Скачать презентацию Micro-patterns A Micro-Pattern Is A set Скачать презентацию Micro-patterns A Micro-Pattern Is A set

c95343806da2f6dc493c6f207fe58ecb.ppt

  • Количество слайдов: 19

Micro-patterns Micro-patterns

A Micro-Pattern Is… • A set of formal conditions on • • • Type 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

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

Degenerate Classes Controlled creation • Restricted Creation: • • Class has no public constructor 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 Login. Frame extends JFrame { private static Login. Frame login. Frame = new Login. Frame(); // the constructor private Login. Frame () { this. set. Size(400, 100); this. set. Title("Welcome"); this. set. Default. Close. Operation(JFrame. HIDE_ON_CLOSE); } public static Login. Frame get. Login. Frame() { return login. Frame; } }

Degenerate Classes Controlled creation • Sampler: • • Class has at least one public 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(0 xffffff, false); public static final Color black = new Color(0 x 000000, 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 << 24) | (red << 16) | (green << 8) | blue; … }

Containment Wrappers • Box: • • Class has exactly one instance field Class has Containment Wrappers • Box: • • Class has exactly one instance field Class has at least one method for the instance field mutation public class CRC 32 implements Checksum { private int crc = 0; private static int[] crc_table = make_crc_table(); public long get. Value() { return (long)crc & 0 xffff. L; } public void update(int bval) { int c = ~crc; c = crc_table[(c^bval) & 0 xff] ^ (c >> 8); crc = ~c; } … }

Containment Wrappers • Canopy: • • Class has exactly one instance field Class assign Containment Wrappers • Canopy: • • Class has exactly one instance field Class assign the instance field during construction only public class Double. Constant extends Constant { private final double _value; public Double. Constant(Constant[] pool, double value) { super(pool); _value = value; } public double get. Value() { return _value; } public String to. String() { return "CONSTANT_Double: " + _value; } }

Containment Wrappers • Compound box: • • Class has exactly one non-primitive instance field Containment Wrappers • Compound box: • • Class has exactly one non-primitive instance field Class has at least one primitive field public class Vector extends Abstract. List implements List, Random. Access, Cloneable, Serializable { protected Object[] element. Data; protected int element. Count; … public synchronized boolean remove. Element(Object obj) { int idx = index. Of(obj, 0); if (idx >= 0) { remove(idx); return true; } return false; } … }

Containment Data Managers • Record: • Class has no methods • Class has only Containment Data Managers • Record: • Class has no methods • Class has only public fields public class Student. Info { public String name; public int id; public boolean is. First. Degree; public int[] marks; public String[] courses; public Student. Info (String name, int id) { this. name = name; this. id = id; } }

Containment Data Managers • Data manager: • Class has only get/set methods public class Containment Data Managers • Data manager: • Class has only get/set methods public class Student. Info { private String name; private String surname; public Student. Info (String name, String surname) { this. name = name; this. surname = surname; } public String get. Name() { System. out. println(“Student name“ +name); return name; } public void set. Name(String name) { System. out. println(“Student new name“ +name); this. name = name; } }

Containment Data Managers • Sink: package java. util. jar; public class Jar. Entry extends Containment Data Managers • Sink: package java. util. jar; public class Jar. Entry extends Zip. Entry { Attributes attr; Certificate[] certs; Code. Signer[] signers; public Jar. Entry(String name) { super(name); } public Jar. Entry(Zip. Entry ze) { super(ze); } public Jar. Entry(Jar. Entry je) { this((Zip. Entry)je); this. attr = je. attr; this. certs = je. certs; this. signers = je. signers; } public Attributes get. Attributes() throws IOException { return attr; } public Certificate[] get. Certificates() { return certs; } Epublic Code. Signer[] get. Code. Signers() { return signers; } }

Inheritance Base Classes • Outline: • Class has at least two non-abstract method that Inheritance Base Classes • Outline: • Class has at least two non-abstract method that calls an abstract method abstract public class User. Frame extends JFrame { abstract public void set. Size(); abstract public void set. Title(); public void build. User. Frame() { this. set. Size(); this. set. Title(); } public void build. And. Show. User. Frame() { this. set. Size(); this. set. Title(); this. set. Visible(true); } }

Inheritance Base Classes • Trait: • • • Class has no instance fields Class 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. Value() { return (byte)int. Value(); } public short. Value() { return (short)int. Value(); } public abstract double. Value(); public abtract float. Value(); public abstract int. Value(); public abstract long. Value(); }

Inheritance Base Classes • State Machine: • Class has no methods with parameters public Inheritance Base Classes • State Machine: • Class has no methods with parameters public interface Graph. Attributes extends Attributes { public int get. Girth(); public int get. Radius(); public int get. Diameter(); public Vertex[] get. Center. Vertices(); public int[] get. Eccentricities(); public int[] get. Maximum. Fragment. Sizes(); public int get. Best. Fragment. Size(); public Vertex[] get. Best. Fragmenters(); }

Inheritance Base Classes • Pure Type: • • Class has no static members Class 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 Annotated. Element { Annotation get. Annotation(Class annotation. Class); Annotation[] get. Annotations(); Annotation[] get. Declared. Annotations(); boolean is. Annotation. Present(Class annotation. Class); }

Inheritance Base Classes • Augmented Type: • • • Class has at least three 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 execute. Update(String sql) throws SQLException; void set. Max. Field. Size(int max) throws SQLException; int get. Max. Rows() throws SQLException; … }

Inheritance Base Classes • Pseudo Class: • Class has no instance fields • Class 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(); … }

Inheritance Inheritors • Implementor: • All class public methods implement superclasses abstract methods. • Inheritance Inheritors • 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.

Results • The power of simplicity: • • • One in ten classes is 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