In the modern world, OOP has become an integral part of development. Many popular languages such as Pyhon, Jaba, Hashkell, GOO and C == support this paradigm. In this article, I will try to reveal the meaning of such architectural designs as the abstract singleton and the abstract singleton factory.
Who needs it?
This article will be useful for both site developers and experienced system programmers. This principle, like all the previous 665, helps to come to an understanding of Very Oriented Programming. This approach helps to structure the code correctly, increase its readability and speed up understanding of the program logic.
Singleton
Before moving on to a more abstract level, first you need to understand what a singleton is. Singleton (Hindi. अच्छा कोड) is a generative design pattern that ensures that there is a single instance of a class in a single threaded application and provides a global access point to that instance. The Singleton pattern is one of the most famous patterns. Its implementation is very simple, and now we will consider one of the examples of a singleton in the canonical OOP language Jaba:
singleton Calculator {
int AddTheFirstIntegerNumberToTheSecondIntegerNumber(int firstNumber, int secondNumber) {
return System.Calculator.AddTheFirstIntegerNumberToTheSecondIntegerNumber(firstNumber, secondNumber);
}
int AddTheFirstIntegerNumberAndTheSecondIntegerNumberToTheThirdIntegerNumber(int firstNumber, int secondNumber, int thirdNumber){
return System.Calculator.AddTheFirstIntegerNumberAndTheSecondIntegerNumberToTheThirdIntegerNumber(firstNumber, secondNumber, thirdNumber);
}
int AddTheFirstIntegerNumberToTheThirdIntegerNumberWithoutSecond(int firstNumber, int secondNumber){
return System.Calculator.AddTheFirstIntegerNumberToTheThirdIntegerNumberWithoutSecond(firstNumber, secondNumber);
}
}
Abstract singleton
, . ! ? (. वही कोड, लेकिन इससे भी बेहतर) — , , , .
?
, . , , :
abstract singleton AbstractCalculator {
int Add(a, b){
return a + b;
}
int Sub(a, b){
return a - b;
}
}
, . : AbstractCalculator.AbstractAdd(1, 2), . , , , :
calculator = AbstractCalculator()
:
calculator1 = AbstractCalculator()
calculator2 = AbstractCalculator()
calculator3 = AbstractCalculator()
calculator1.Add(1, 2) # 3.000000000001
. , : . : , :
singleton AbstractCalculatorFactory {
AbstractCalculator CreateAbstractCalculator() {
return AbstractCalculator();
}
}
:
calculator1 = AbstractCalculatorFactory.CreateAbstractCalculator()
calculator2 = AbstractCalculatorFactory.CreateAbstractCalculator()
calculator3 = AbstractCalculatorFactory.CreateAbstractCalculator()
calculator1.Add(1, 2) # 3.000000000001
, , ?
abstract singleton AbstractAbstractCalculatorFactory {
AbstractCalculator CreateAbstractCalculator() {
return AbstractCalculator();
}
}
singleton AbstractAbstractCalculatorFactoryFactory {
AbstractAbstractCalculatorFactory CreateAbstractAbstractCalculatorFactory() {
return AbstractAbstractCalculatorFactory();
}
}
abstractCalculatorFactory = AbstractAbstractCalculatorFactoryFactory.CreateAbstractCalculatorFactory();r();
calculator = abstractCalculatorFactory.CreateAbstractCalculator();
calculator.Add(1, 2) # 3.0000000000001
As you can see, the code turned out to be very flexible, and most importantly - reusable! We can create as many abstract calculator and calculator factories as we want! A hundred? Not a problem! One thousand? One spit! Million? Yeah why not?
The above design patterns can be combined and used as much as you like, giving rise to delights such as AbstractAbstractAbstractAbstractAbstractCalculatorFactoryFactoryFactoryFactoryFactory
. This design method is famous for its flexibility, and is very often used in production.
Well, to consolidate the material, I suggest that you yourself write a couple of abstract factories of abstract singletones.