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
- 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.
- Create a single instance of some type
- We provide access to it from the outside
- Prevent multiple instances of the same type from being created
- Lazy-initialized*, out of sync
- 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 Wpapper / Decorator / Wrapper Description: //TODO What is Why do we need Examples Procedure: //TODO Code: //TODO Pros: //TODO Cons: //TODO Adapter / Adapter Description: //TODO What is Why do you need Examples Procedure: //TODO Code: //TODO Pros: //TODO Cons: //TODO 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
GO TO FULL VERSION