 
   def init(stash_pid) do
     current_number = OtpApplications3.Stash.get_value stash_pid
-    { :ok, {current_number, stash_pid} }
+    { :ok,
+      %State{current_number: current_number, stash_pid: stash_pid} }
   end
- def handle_call(:next_number, _from, {current_number, stash_pid}) do
-    { :reply, current_number, {current_number+1, stash_pid} }
+  def handle_call(:next_number, _from, state) do
+    { :reply, state.current_number, %{ state  | current_number: state.current_number + state.delta }}
   end
-  def handle_cast({:increment_number, delta}, {current_number, stash_pid}) do
-    { :noreply, {current_number + delta, stash_pid} }
+  def handle_cast({:increment_number, delta}, state) do
+    { :noreply,
+     %{state | current_number: state.current_number + delta, delta: delta}}
   end
-  def terminate(reason, {current_number, stash_pid}) do
-    OtpApplications3.Stash.save_value stash_pid, current_number
+  def terminate(reason, state) do
+    OtpApplications3.Stash.save_value state.stash_pid, state.current_number
     IO.puts "reason : #{inspect reason}"
+  end
+
+  def code_change("0", old_state = { current_number, stash_pid }, _extra) do
+    new_state = %State{current_number: current_number,
+      stash_pid: stash_pid,
+      delta: 1
+    }
+    Logger.info "Changing code from 0 to 1"
+    {:ok, new_state}
   end
 end
 

    {:ok, _pid} = GenServer.start_link(__MODULE__, stash_pid, name: __MODULE__)
  end
  def next_number do
    GenServer.call __MODULE__, :next_number
  end
  def increment_number(delta) do
    GenServer.cast __MODULE__, {:increment_number, delta}

defmodule OtpApplications3.Server do
  use GenServer
  require Logger

  @vsn "1"
  defmodule State do
    defstruct current_number: 0, stash_pid: nil, delta: 1
  end

  ###
  # 外部API
