Search posterous

Search all posts and users. Type a name, type a favorite song title, whatever! See what comes up.
  

More posterous blogs











More recommended blogs »

Here are posterous posts filed under designpattern...

Adios says...

C

A struct in C can be used to define a class, and the strategy can be set using a function pointer. The following mirrors the Python example, and uses C99 features:

#include <stdio.h>
 
void print_sum(int n, int *array) {
  int total = 0;
  for (int i=0; i<n; i++) total += array[i];
  printf("%d", total);
}
 
void print_array(int n, int *array) {
  for (int i=0; i<n; i++) printf("%d ", array[i]);
}
 
int main(void) {
  typedef struct {
    void (*submit_func)(int n, int *array);
    char *label;
  } Button;
 
  // Create two instances with different strategies
  Button button1 = {print_sum, "Add 'em"};
  Button button2 = {print_array, "List 'em"};
 
  int n = 10, numbers[n];
  for (int i=0; i<n; i++) numbers[i] = i;
 
  button1.submit_func(n, numbers);
  button2.submit_func(n, numbers);
 
  return 0;
}

Filed under: design pattern

WanCW says...

The following rules should apply when determining the value of a pattern:

  1. Knowing when to apply a pattern requires understanding what problems the pattern solves
  2. Knowing how to apply a pattern requires understanding how the pattern works
  3. Knowing your environment is essential to knowing what problems a pattern solves
  4. Knowing when to apply a pattern is more important than knowing how to apply the pattern

不要用鐵槌敲螺絲。

Filed under: Design Pattern

Rob Enslin says...

Box.net's UX activity progress design pattern is what makes box stand out from the crowd. Box 'get it' and truly understand how to design a user experience many other websites should mimic.

The specific elements that inform the user of what's going on:

1. background page faded - focus on activity

2. 'Uploading 6 files' - user knows what's happening and how many files are being uploaded

3. Visual progress bar - shows relative progress that's dynamic with 1) visual animation (horizontal bar) and percentage complete (value of 100%)

4. Upload metadata: upload speed, total file size, upload rate and time left to upload

5. Up-sell 'Tip' for quicker upload speeds - offers alternative options while the user waits

6. 'Hide Popup' - further options available to user while files are being uploaded

Filed under: design pattern

rosscoops says...

The Utility design pattern is easy in ruby, you just need a private constructor and static methods...

class Utils
  private
  def __initialize
  end
  
  public
  def self.helper_func1
  end

  def self.helper_func2
  end
end

Then anywhere else in your code you can say...

require 'utils'
Utils::helper_func1
Utils::helper_func2

Filed under: design pattern

rosscoops says...

The Observer Design Pattern (http://en.wikipedia.org/wiki/Observer_pattern) is also implemented in the Ruby library through a mix-in called observer. Here's a somewhat dumb example of how to use it:

require 'observer'

class Talker
  include Observable
  
  def run
    changed
    notify_observers("Hello!")
  end
end

class Listener
  def initialize(talker)
    talker.add_observer(self)
  end
  
  def update(msg)
    puts "Talker says #{msg}"
  end
end

t = Talker.new
l = Listener.new(t)
t.run

... which produces:

Talker says Hello!

Filed under: design pattern

rosscoops says...

The singleton design pattern (http://en.wikipedia.org/wiki/Singleton_pattern) is easy to implement in Ruby. The Ruby library already includes a mix-in module with this functionality, called singleton:

require 'singleton'

class Example
  include Singleton
end

puts Example.instance
puts Example.instance

...which returns the following, note both calls to Example.instance return the same object:

#<Example:0x220b0>
#<Example:0x220b0>

Filed under: design pattern