In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to understand the Java factory model, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
1. Simple factory model
What is a simple factory model?
First of all, through a case to understand.
I have a need to buy a car. There are different models to buy a car, so our common practice is that I will buy whatever car I have. The example is as follows.
Define a car interface
Public interface Car {void name ();}
Define an implementation class, such as BMW
Public class Baoma implements Car {@ Override public void name () {System.out.println (BMW);}}
test
Public class Test {public static void main (String [] args) {Car car3 = new Baoma (); car3.name ()
Result: BMW
Obviously, this is not in line with the idea of our factory design pattern, we should not let users go to new a car, in real life it is impossible to go to new a car and then buy their own, right or not, so we have to design a simple factory to meet our needs.
Simple factory class
Public class CarFactory {public static Car getCar (String car) {if (car.equals (BMW)) {return new Baoma ();} else if (car.equals ("Audi")) {return new AoDI ();} return null;}}
test
Car car = CarFactory.getCar ("BMW"); car.name (); Car car1 = CarFactory.getCar ("Audi"); car1.name ()
Result: BMW Audi
Well, at this time, we users do not need to care about how our car came from, how to build it, and what kind of car it is. We users just take out the car we need from the factory, and if not, add it from the factory.
This figure is an auxiliary understanding of the simple factory model.
To sum up, the simple factory model: used to produce any product in the same hierarchical structure (to add new products, you need to overwrite existing code)
Second, the factory method model
So in the simple factory class, you should find a problem. If we need to add a new car, we need to go into the factory to modify the factory code. If we modify our code, we have violated the opening and closing principle of our design pattern for such a long time. Bad, then we came to our factory method pattern.
An example of what is the factory method mode is as follows:
Among them, the car type and the car interface remain the same, we change the car factory.
We define a car interface and the method returns a car.
Public interface CarFactory {Car getCar ();}
Well, we can think that each car is equipped with a car factory, so when adding, we only need to add a new car factory to realize the car factory interface.
Audi factory
Public class AoDIFactory implements CarFactory {@ Override public Car getCar () {return new AoDI ();}}
I won't demonstrate the BMW factory.
test
Car baoma = new BaoMaFactory (). GetCar (); Car Aodi = new AoDIFactory (). GetCar (); baoma.name (); Aodi.name ()
So at this time, if we want to add a new car, we just need to add his car and his factory, and the user only needs to find the corresponding factory.
To sum up.
Factory method model: used to produce fixed products in the same hierarchical structure (support to add any product)
Third, abstract factory model
The factory method model introduced above considers the production of one kind of products, such as livestock farms only raise animals, television factories only produce televisions, computer software colleges only train computer software majors, and so on.
The same kind is called the same grade, that is to say: the factory method model only considers the production of products of the same grade, but in real life, many factories are comprehensive factories that can produce multi-grade (type) products, such as raising both animals and plants on the farm, the electrical factory produces both televisions and washing machines or air conditioners, and the university has both software and biology majors.
You need to understand a definition here:
Product family: a group of products at different levels produced by the same specific factory is called a product family.
As shown in the figure
3.1. The definition of abstract factory pattern
It is a pattern structure that provides an interface for the access class to create a set of related or interdependent objects, and the access class can get different levels of products of the same family without specifying the specific class of the desired product.
The following conditions are generally met when using abstract factory patterns
The associated multi-level products in the product family can be managed together within the class, without having to introduce several new classes to manage them.
When a product family is needed, the abstract factory ensures that the client always uses only product groups of the same product.
Abstract factory enhances the expansibility of the program, when adding a new product family, there is no need to modify the original code to meet the opening and closing principle.
Of course, there are drawbacks to using the abstract factory pattern:
When a new product needs to be added to the product family, all factory classes need to be modified. It increases the abstraction of the system and the difficulty of understanding.
3.2. The structure of abstract factory pattern
1. Abstract factory (Abstract Factory): provides an interface to create a product, which contains multiple methods to create a product, newProduct (), which can create multiple different levels of products.
two。 Concrete factory (Concrete Factory): mainly to implement multiple abstract methods in the abstract factory to complete the creation of concrete products.
3. Abstract product (Product): defines the specification of the product, describes the main features and functions of the product, abstract factory pattern has multiple abstract products.
4. Concrete product (ConcreteProduct): implements the interface defined by the abstract product role, created by the concrete factory, and has a many-to-one relationship with the concrete factory.
3.3 Abstract factory pattern code example
First of all, let's define abstract products.
such as
* @ Description car Interface * / public interface Car {void startCar (); void stopCar (); void washCar ();} * @ Description vehicle Audio Class * / public interface CarAudio {void startCarAudio (); void stopCarAudio ();}
Provide specific products
* @ Description Audi * / public class AodiCar implements Car {@ Override public void startCar () {System.out.println ("start Audi");} @ Override public void stopCar () {System.out.println ("turn off Audi");} @ Override public void washCar () {System.out.println ("wash Audi") } * @ Description Audi car Audio * / public class AodiCarAudio implements CarAudio {@ Override public void startCarAudio () {System.out.println ("turn on Audi car Audio");} @ Override public void stopCarAudio () {System.out.println ("turn off Audi car Audio") }} * @ Description BMW * / public class BaomaCar implements Car {@ Override public void startCar () {System.out.println ("start BMW");} @ Override public void stopCar () {System.out.println ("stop BMW");} @ Override public void washCar () {System.out.println ("wash BMW") }} * @ Description BMW car Audio * / public class BaomaCarAudio implements CarAudio {@ Override public void startCarAudio () {System.out.println ("turn on BMW car Audio");} @ Override public void stopCarAudio () {System.out.println ("turn off BMW car Audio");}}
Provide abstract factories
* @ Description Product Factory Interface * / public interface ProductFactory {Car ProductCar (); CarAudio ProductCarAudio ();}
Provide specific factories
* @ Description BMW Factory * / public class BaomaFactory implements ProductFactory {@ Override public Car ProductCar () {return new BaomaCar ();} @ Override public CarAudio ProductCarAudio () {return new BaomaCarAudio ();}} * @ Description aodi Factory * / public class AodiFactory implements ProductFactory {@ Override public Car ProductCar () {return new AodiCar () @ Override public CarAudio ProductCarAudio () {return new AodiCarAudio ();}}
Customer test class
* @ Description Consumer Class * / public class Customer {public static void main (String [] args) {System.out.println ("- Audi Series -"); / / get Audi Factory AodiFactory aodiFactory = new AodiFactory (); Car car = aodiFactory.ProductCar (); CarAudio carAudio = aodiFactory.ProductCarAudio (); car.startCar () Car.stopCar (); carAudio.startCarAudio (); System.out.println ("- BMW Series -"); BaomaFactory baomaFactory = new BaomaFactory (); Car carBaoma = baomaFactory.ProductCar (); CarAudio carAudioBaoma = baomaFactory.ProductCarAudio (); carBaoma.stopCar (); carBaoma.washCar (); carAudioBaoma.startCarAudio ();}}
Output result
-Audi series-
Start the Audi.
Close the Audi.
Turn on Audi's car stereo.
-BMW series-
Stop, BMW.
Wash BMW
Turn on the BMW car stereo
Class Diagram relationship in idea
If we want to add new products, we need to add methods in the main factory, and other classes will also be modified, confirming the shortcomings we wrote earlier.
To sum up:
The abstract factory pattern is to create other factories around a super factory, also known as the factory of other factories.
IV. Summary
The core essence of the factory model
Instantiated objects do not use new, but factory methods instead
We will select the implementation class and unify the management and control when creating the object, thus decoupling the caller from our implementation class
Simple factory model: used to produce any product in the same hierarchical structure (for adding new products, you need to overwrite existing code)
Factory method model: used to produce fixed products in the same hierarchical structure (support to add any product)
Abstract factory pattern: create other factories around a super factory, also known as the factory of other factories
On how to understand the Java factory model to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.