Design patterns represent best practices
The introduction
- Create pattern.
- Main solution: Create different instances under different conditions.
- Key code: interface specification.
- Users only want to think about the top requirements, give me what I want! What to do? Outsource (factories)!
scenario
- Objects that can be created with new without using the factory pattern.
- Users don’t care about the details of instance creation, composition, and presentation.
- Anywhere complex objects are generated, the factory method pattern can be used.
1. Simple factory mode
1.1 graphic
1.2 Code Examples
interface Color { fun paint() } class Red : Color { override fun paint() { println("Color Red::paint() method") } } class Green : Color { override fun paint() { println("Color Green::paint() method") } } class ColorFactory { fun getColor(colorType: String): Color? { return when (colorType) { "RED" -> { Red() } "GREEN" -> { Green() } else -> null } } } class FactoryColorDemo { companion object { @JvmStatic fun main(args: Array<String>) {val colorFactory = colorFactory () And call its paint method val red = colorFactory.getColor(" red ") red? .paint() // Get the Green object and call its paint method val Green = colorFactory.getColor(" Green ") Green? .paint() } } }Copy the code
1.3 Diagram (Add products in Simple Factory Mode)
1.4 the advantages and disadvantages
- Adding a new product subclass requires adding a new if-else in the factory.
- Poor scalability, against the open and close principle, but also affects readability.
- Suitable for use in situations where the business is simpler and factory classes do not change often.
2. Factory mode
2.1 graphic
2.2 Code Examples
interface Color { fun paint() } class Red : Color { override fun paint() { println("Color Red::paint() method") } } class Green : Color { override fun paint() { println("Color Green::paint() method") } } interface ColorFactory { fun createColor(): Color } class RedColorFactory : ColorFactory { override fun createColor(): Color { return Red() } } class GreenColorFactory : ColorFactory { override fun createColor(): Color { return Green() } } class FactoryColorDemo { companion object { @JvmStatic fun main(args: Array<String>) {// Get the Red object, And call it the way the val paint redColorFactory = redColorFactory (val) red = redColorFactory. CreateColor () red. The paint () / / to get Green The object, And call it the way the val paint greenColorFactory = greenColorFactory (val). Green = greenColorFactory createColor (green). The paint ()}} }Copy the code
2.3 Diagram (Factory mode to add products)
2.4 the advantages and disadvantages
- A subclass corresponds to a factory class, class inflation.
- Large number of classes, high code complexity.
3. Abstract factory pattern
3.1 graphic
3.2 Code Examples
interface Color { fun paint() } class Red : Color { override fun paint() { println("Color Red::paint() method") } } class Green : Color { override fun paint() { println("Color Green::paint() method") } } interface Shape { fun draw() } class Rectangle : Shape { override fun draw() { println("Shape Rectangle::draw() method") } } class Circle : Shape { override fun draw() { println("Shape Circle::draw() method") } } interface LogoFactory { fun createColor(): Color fun createShape(): Shape } class HappyLogoFactory : LogoFactory { override fun createColor(): Color { return Red() } override fun createShape(): Shape { return Circle() } } class AngryLogoFactory : LogoFactory { override fun createColor(): Color { return Green() } override fun createShape(): Shape { return Rectangle() } } class FactoryColorDemo { companion object { @JvmStatic fun main(args: Array<String>) {var happyLogoFactory = happyLogoFactory () val happyColor = HappyLogoFactory. CreateColor () val happyShape = happyLogoFactory. CreateShape () happyColor. Paint () happyShape. The draw () / / access Anger Logo (barrage) green object val angryLogoFactory = angryLogoFactory () val angryColor = angryLogoFactory. CreateColor (val) angryShape = angryLogoFactory.createShape() angryColor.paint() angryShape.draw() } } }Copy the code
3.3 Diagram (Adding products in Abstract Factory mode)
3.4 Diagram (Adding factories in Abstract Factory mode)
3.5 Diagram (Abstract Factory Model summary)
3.6 the advantages and disadvantages
- When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family.
- Product families are difficult to expand, but product levels are easy to expand.
4. Summary
- Simple Factory: A unique factory class that creates product objects based on input parameters.
- Factory method: multiple factory classes, extend the product subclass, add the corresponding factory.
- Abstract factories: Multiple factory classes, product subclasses grouped, factories grouped into product families.
Xiaobian blog series
Design pattern family bucket