Giter Site home page Giter Site logo

design-patterns's Introduction

design-patterns

singleton pattern and visitor pattern in java & js implemented

Singleton Pattern

public class SingleObject {

	private static SingleObject instance = new SingleObject();
  
	// PRIVATE: can't create "new" object
	private SingleObject() {
	}

	// Get the only object available
	public static SingleObject getInstance() {
		return instance;
	}

	public void showMessage() {
		System.out.println("Hello World!");
	}
}

✔️ SingleObject object = SingleObject.getInstance();

❌ SingleObject object = new SingleObject();

Problems/Solutions

Not safe in Multi-Thread Environment:

  • loop can be excuted multiple times at the same moment (조건문이 동시에 두번 돌 수 있음)

  • shared value can't be maintained well-> use synchronized key word

    public synchronized static void print(String input) {
    		count++;
    		System.out.println(input + "count : "+ count);
    	}
    

Interface can't use static methods


Visitor Pattern

Without chaing the classes of the elements on which it operates, it lets you define a new operations

image


Source

https://www.tutorialspoint.com/design_pattern/design_pattern_quick_guide.htm

design-patterns's People

Contributors

krixstin avatar

Watchers

 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.