Giter Site home page Giter Site logo

design-pattern-java's Introduction

Design Pattern DEMO with Java

Behavioral

  • Decouple sender and receiver from request
  • Runtime configuration
  • Hierarchical in nature
  • Be careful with long chain
  • Encapsulate each request as an object
  • Decouple sender from processor
  • Few drawbacks
  • Often used for undo functionality

Design

  • Context 待解譯的語句
  • AbstractExpression 所有規則都要實作的介面
  • Interpret(Context) with
    • TerminalExpression 最小單位的規則
    • NonterminalExpression 可以再展開的規則組合

Example in Java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InterpreterDemo {
    
    public static void main(String[] args) {
        String input = "Apple, orange, and lemons!";
        
        Pattern p = Pattern.compile("(orange|Orange|lemon|Lemon|apple|Apple|grape|Grape)");
        Matcher m = p.matcher(input);
        
        while (m.find()) {
            System.out.println("Found a " + m.group() + ".");
        }
    }
    
}

Design

  • Interface based
  • Factory Method based
  • Independent, but fail fast
  • Enumerators are fail safe
  • Iterator, ConcreteIterator

Example in Java

import java.util.ArrayList;

public class IteratorDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();

        names.add("Vincent");
        names.add("Ravi");
        names.add("Joan");

        Iterator<String> namesIterator = names.iterator();

        while(namesIterator.hasNext()) { 
            String name = namesIterator.next();
            System.out.println(name);
            namesIterator.remove();
        }
    }
}

design-pattern-java's People

Contributors

raviwu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.