Three design patterns related to the factory 2020-11-08 15:03:29
Simple factory model : To put it bluntly , Indirectly out of new Create objects , It's about getting objects directly from the factory .
Simple factory : Not with new Create objects , Instead, the process of creating objects is centralized in the factory , This makes it easy to maintain the change creation process ; It also realizes the decoupling between the call creation object and the object .
Don't talk much , Let's start with the following UML Class diagram :
Automobile package com.yuan.factory.simple; package com.yuan.factory.simple; package com.yuan.factory.simple; package com.yuan.factory.simple; |
Factory method model :
Because of the simple factory model , dissatisfaction “ Open to expansion , Turn off for changes ” principle , So , There's the factory approach model . Then the factory approach pattern is on the simple factory model , Create a factory for each subclass , So it's scalable , Follow these principles .
Bus interface package com.yuan.factory.method; package com.yuan.factory.method; package com.yuan.factory.method; package com.yuan.factory.method; package com.yuan.factory.method; package com.yuan.factory.method; |
Abstract factory pattern :
Product family : Different types of products produced by the same manufacturer .
Same product type : A little ... A little ... A little
Abstract factory pattern (Abstract Factory Pattern) It's about creating other factories around one super factory . This super factory is also called the factory of other factories . This type of design pattern is a creation pattern , It provides the best way to create objects .
In abstract factory mode , Interface is the factory responsible for creating a related object , There is no need to explicitly specify their classes . Each generated factory can provide objects according to the factory pattern .