Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (var animal in zoo) {
- switch (typeof(animal)) {
- case "dog":
- echo animal.bark();
- break;
- case "cat":
- echo animal.meow();
- break;
- }
- }
- foreach (var animal in zoo) {
- echo animal.speak();
- }
- class RequestHandler {
- public void handleRequest(int action) {
- switch(action) {
- case LOGIN:
- doLogin();
- break;
- case LOGOUT:
- doLogout();
- break;
- case QUERY:
- doQuery();
- break;
- }
- }
- }
- interface Command {
- public void execute();
- }
- class LoginCommand implements Command {
- public void execute() {
- // do what doLogin() used to do
- }
- }
- class RequestHandler {
- private Map<Integer, Command> commandMap; // injected in, or obtained from a factory
- public void handleRequest(int action) {
- Command command = commandMap.get(action);
- command.execute();
- }
- }
- class House {
- private int state;
- public void enter() {
- switch (state) {
- case INSIDE:
- throw new Exception("Cannot enter. Already inside");
- case OUTSIDE:
- state = INSIDE;
- ...
- break;
- }
- }
- public void exit() {
- switch (state) {
- case INSIDE:
- state = OUTSIDE;
- ...
- break;
- case OUTSIDE:
- throw new Exception("Cannot leave. Already outside");
- }
- }
- abstract class HouseState {
- public HouseState enter() {
- throw new Exception("Cannot enter");
- }
- public HouseState leave() {
- throw new Exception("Cannot leave");
- }
- }
- class Inside extends HouseState {
- public HouseState leave() {
- return new Outside();
- }
- }
- class Outside extends HouseState {
- public HouseState enter() {
- return new Inside();
- }
- }
- class House {
- private HouseState state;
- public void enter() {
- this.state = this.state.enter();
- }
- public void leave() {
- this.state = this.state.leave();
- }
- }
- void InitMap(){
- Map[key1] = Object/Action;
- Map[key2] = Object/Action;
- }
- Object/Action DoStuff(Object key){
- return Map[key];
- }
- switch
- IState
- switch(*IP) {
- case OPCODE_ADD:
- ...
- break;
- case OPCODE_NOT_ZERO:
- ...
- break;
- case OPCODE_JUMP:
- ...
- break;
- default:
- fixme(*IP);
- }
- opcode_table[*IP](*IP, vm);
- ... // in somewhere else:
- void opcode_add(byte_opcode op, Vm* vm) { ... };
- void opcode_not_zero(byte_opcode op, Vm* vm) { ... };
- void opcode_jump(byte_opcode op, Vm* vm) { ... };
- void opcode_default(byte_opcode op, Vm* vm) { /* fixme */ };
- OpcodeFuncPtr opcode_table[256] = {
- ...
- opcode_add,
- opcode_not_zero,
- opcode_jump,
- opcode_default,
- opcode_default,
- ... // etc.
- };
- public class Animal
- {
- public abstract void Speak();
- }
- public class Dog : Animal
- {
- public virtual void Speak()
- {
- Console.WriteLine("Hao Hao");
- }
- }
- public class Cat : Animal
- {
- public virtual void Speak()
- {
- Console.WriteLine("Meauuuu");
- }
- }
- foreach (var animal in zoo)
- {
- echo animal.speak();
- }
- if a = 1 then
- do something else
- if a = 2 then
- do something else
- else
- if a = 3 then
- do the last thing
- endif
- endif
- endif
- if a = 1 then
- do something
- endif
- if a = 2 then
- do something else
- endif
- if a = 3 then
- do something more
- endif
- procedure processA(a:int)
- if a = 1 then
- do something
- procedure_return
- endif
- if a = 2 then
- do something else
- procedure_return
- endif
- if a = 3 then
- do something more
- procedure_return
- endif
- end_procedure
- private void doLog(LogLevel logLevel, String msg) {
- String prefix;
- switch (logLevel) {
- case INFO:
- prefix = "INFO";
- break;
- case WARN:
- prefix = "WARN";
- break;
- case ERROR:
- prefix = "ERROR";
- break;
- default:
- throw new RuntimeException("Oops, forgot to add stuff on new enum constant");
- }
- System.out.println(String.format("%s: %s", prefix, msg));
- }
- for i from 1 to 1000 {statement1; statement2}
- if something=false then {statement1; statement2}
- while isOKtoLoop {statement1; statement2}
Add Comment
Please, Sign In to add comment