Changes between Version 1 and Version 2 of DesignPatterns


Ignore:
Timestamp:
Jan 27, 2015, 3:14:36 PM (10 years ago)
Author:
Timothy Lin
Comment:

info on singletons & chain of responsibility pattern

Legend:

Unmodified
Added
Removed
Modified
  • DesignPatterns

    v1 v2  
    33== Overarching Structural Patterns
    44
    5 === Command-Based FRC Robot (Event-Based)
     5=== Command-Based FRC Robot (Event-Driven)
    66Please see the [http://wiki.raidtech.net/wiki/Command%20Based%20Robot%20Notes Command Based Robot Notes] for detailed information on how to implement this design pattern.
    77
     
    2323This entails that inside the loop, there cannot be anything that requires a large amount of processing power or time. Each instruction should be relatively short and not take up not. '''There should be no while loops, or lengthy for loops.'''
    2424
     25Arduinos, for example, use this pattern. There is usually some sort of an initialization function, as well as a periodic, or repeated function.
     26{{{
     27#!div style="font-size: 80%"
     28  {{{#!java
     29  void init() {
     30    // initialization code
     31  }
     32
     33  void loop() {
     34    // periodically repeating code
     35  }
     36  }}}
     37}}}
     38
    2539== Communication-Related Patterns
     40
     41=== Chain of Responsibility
     42This pattern is a method of processing data using multiple classes. Each class is responsible for dealing with a particular type of data or output, and passes along any data it doesn't know how to deal with to the next class(es).
     43
     44See example:
     45{{{
     46#!java
     47public class ClawPolicy {
     48  /* instance variables declared; not shown */
     49
     50  public void execute() {
     51    if (modeBtn == AUTOMATED)
     52      clawAlgorithmPolicy.execute();
     53    else if (modeBtn == MANUAL)
     54      clawHandler.execute();
     55    else
     56      System.out.println("[ERROR: ClawPolicy] IllegalStateException for the mode button");
     57  }
     58}
     59}}}
    2660
    2761=== !Observer/Listener Pattern
    2862
    2963=== Singletons
     64A singleton is a class which can only ever have one instance of it, generally created by the class itself and accessed through a public accessor method.
     65
     66{{{
     67#!div class="important"
     68'''Reasons for Making a Class a Singleton'''
     69- class contains objects that should not be instantiated more than once
     70- instances of the class require a lot of processing power, time, or other machine resources
     71}}}
     72
     73See the following sections for various methods of implementing a singleton class in the Java programming language:
     74==== Singleton from Enum
     75A singleton can be written as an java enum with one constant: the instance.
     76
     77{{{
     78#!java
     79public enum DatabaseAccessObject {
     80  INSTANCE;
     81 
     82  private DatabaseModel dm;
     83  private SQLConnector sqlconnect = new SQLConnector(/* arguments not shown */);
     84
     85  private DatabaseAccessObject() {
     86    /* implementation not shown, but will take a long time with a large strain on the machine */
     87  }
     88
     89  /* other accessor and modifier methods not shown */
     90 
     91}
     92}}}
     93
     94Then, to access this object:
     95
     96{{{
     97#!java
     98...
     99DatabaseAccessObject dao = DatabaseAccessObject.INSTANCE;
     100
     101dao.find(/* args not shown*/);
     102...
     103}}}
     104
     105==== Private Constructor and Public getInstance()
     106A singleton can also be made using a static getInstance() method and setting the constructor to private.
     107
     108{{{
     109#!java
     110public class DAO {
     111  public static DAO instance = new DAO();
     112
     113  /**
     114   * Time consuming and resource intensive constructor.
     115   */
     116  private DAO() {
     117    // implementation not shown
     118  }
     119
     120  public static DAO getInstance() {
     121    return instance;
     122  }
     123
     124  /* other methods not shown */
     125}
     126}}}
     127
     128Then, to access the object:
     129
     130{{{
     131#!java
     132...
     133DAO dao = DAO.getInstance();
     134
     135dao.<some method>();
     136...
     137}}}
    30138
    31139== Abstraction Patterns