JavaRush /Java Blog /Random EN /Design Patterns for Dummies
mrserfr
Level 33
Киев

Design Patterns for Dummies

Published in the Random EN group
Design Patterns for Dummies - 1 Good afternoon, Dear codegym people! As I studied java, more and more often I began to come across such a concept as design patterns (or patterns), but on the Internet there is very little information on this topic for ... novice programmers. Simply put - dummies (which I am). I decided to take a little time and try to talk about some of them as simply as possible. We also meet them as we progress on the codegym course:
  • Singleton
  • Wpapper / Decorator / Wrapper
  • Adapter / Adapter
  • Strategy / Strategy
I would like to say in advance that I could make a mistake in some place - please correct me, I will take it into account and correct it. So let's go... About templates in general //TODO Design Patterns for Dummies - 2 Singleton What is it? One of the generational patterns that ensures that a class has only one instance + provides a global access point to it. It can be thread-safe (it can work in a multi-threaded program) and not, with simple and lazy initialization *. *Lazy initialization. A technique in programming when some resource-intensive operation (creating an object, calculating a value) is performed immediately before its result is used. Thus, initialization is done "on demand" rather than ahead of time. (wiki) Why do you need it? A singleton is needed to know for sure that we have created ONE object of this class. Examples:
  • Logging
  • An object that is responsible for writing logs. We only need one copy, since we need to write the log using the same file.
  • Storing settings
  • Only one object with settings is needed, since the settings for the entire program are the same.
Procedure:
  1. Create a single instance of some type
  2. We provide access to it from the outside
  3. Prevent multiple instances of the same type from being created
There are several popular implementations of this pattern, I'll cover two of the simplest ones here :
  1. Lazy-initialized*, out of sync
  2. Synchronized
1. With lazy initialization, not synchronized public class NonSynchronizedSingleton { private static NonSynchronizedSingleton instance; private NonSynchronizedSingleton(){} public static NonSynchronizedSingleton getInstance(){ if(instance == null) instance = new NonSynchronizedSingleton(); return instance; } } We make our instance (object) of the class private to prohibit accessing it directly We private static NonSynchronizedSingleton instance; also make the constructor private so that our singleton cannot be created through the constructor private NonSynchronizedSingleton(){} Now we create a method (it will work as a getter) public static NonSynchronizedSingleton getInstance() Assign to it the public access modifier is global, that is, now we can use our singleton from anywhere. In this method, we request an object of this class - if the object does not exist yet, we create it, if it already exists, we get it . We refer to our object in this way NonSynchronizedSingleton.getInstance() Pros/Cons: This implementation is good for everything, except that it does not work in a multi-threaded environment, and therefore is only suitable for single-threaded applications. Why? Because several threads can simultaneously create objects of this class. 2. Synchronized public class SynchronizedSingleton { private static SynchronizedSingleton instance = new SynchronizedSingleton(); private SynchronizedSingleton(){} public static SynchronizedSingleton getInstance(){ return instance; } } Description: Create a variable private static SynchronizedSingleton instance = new SynchronizedSingleton(); But make it private so that no one can access it directly. The constructor is also private private SynchronizedSingleton(){} Well, to work with the object, we create a method (getter) that returns our object to us public static SynchronizedSingleton getInstance(){ Pros: We solved the problem of multithreading, since the static variable is initialized immediately during class initialization, and no matter how many threads access at the same time, they always get the same object. Cons: Now we do not have lazy initialization (The instance object will be created by the classloader during class initialization) Bottom line: public class Test { public static void main(String[] args) { NonSynchronizedSingleton nonSynchronizedSingleton = NonSynchronizedSingleton.getInstance(); NonSynchronizedSingleton secondNonSynchronizedSingleton = NonSynchronizedSingleton.getInstance(); SynchronizedSingleton synchronizedSingleton = SynchronizedSingleton.getInstance(); SynchronizedSingleton secondSynchronizedSingleton = SynchronizedSingleton.getInstance(); System.out.println(nonSynchronizedSingleton.hashCode()); System.out.println(secondNonSynchronizedSingleton.hashCode()); System.out.println(nonSynchronizedSingleton.equals(secondNonSynchronizedSingleton)); System.out.println(synchronizedSingleton.hashCode()); System.out.println(secondSynchronizedSingleton.hashCode()); System.out.println(synchronizedSingleton.equals(secondSynchronizedSingleton)); Car car1 = new Car(); Car car2 = new Car(); System.out.println(car1.hashCode()); System.out.println(car2.hashCode()); System.out.println(car1.equals(car2)); } public static class Car {} } We try to create more than one object in each implementation, but we get the same object at the output: hashcodes are equal, equals returns true. For example, I created a class public static class Car {} And compared hashcodes, equals of two objects of this class (no longer singleton) - hashcodes are different, equals returns false 1956725890 1956725890 true 356573597 356573597 true 1735600054 21685669 false Pros: //TODO Cons: //TODO Design Patterns for Dummies - 3 Wpapper / Decorator / Wrapper Description: //TODO What is Why do we need Examples Procedure: //TODO Code: //TODO Pros: //TODO Cons: //TODO Design Patterns for Dummies - 4 Adapter / Adapter Description: //TODO What is Why do you need Examples Procedure: //TODO Code: //TODO Pros: //TODO Cons: //TODO Design Patterns for Dummies - 5 Strategy / Strategy Description: //TODO What is Why do you need Examples Procedure: //TODO Code: //TODO Pluses: //TODO Cons: //TODO When studying topics, I used
  • http://www.programcreek.com/java-design-patterns-in-stories/
  • http://habrahabr.ru/post/103681/
  • http://habrahabr.ru/post/116577/
  • http://habrahabr.ru/post/129494/
  • http://cpp-reference.ru/patterns/catalog/
  • https://en.wikipedia.org/wiki/%D0%9E%D1%82%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%BD%D0%B0%D1 %8F_%D0%B8%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8 %D1%8F
  • http://habrahabr.ru/post/27108/
  • http://www.amse.ru/courses/cpp1/2010.03.05.html
Many thanks to the authors .
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION