In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge about how to achieve the factory pattern of Java design pattern. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
The details are as follows:
The factory mode mainly provides a transition interface for creating objects, so as to shield and isolate the specific process of creating objects, so as to improve flexibility.
Factory models are divided into three categories:
1) simple factory model (Simple Factory): not conducive to the production of a series of products
2) Factory method pattern (Factory Method): also known as pleomorphic factory
3) Abstract factory pattern (Abstract Factory): also known as toolbox, which produces product families, but is not conducive to the generation of new products
1. Simple factory model
The simple factory pattern is also called the static factory method pattern. You can see from the renaming that this pattern must be very simple. The purpose of its existence is simple: to define an interface for creating objects.
In the simple factory pattern, a factory class is at the center of the call to the instantiation of the product class, which determines which product class should be instantiated, just as a traffic policeman stands in the flow of traffic. decide which direction the vehicle is allowed to flow in. Let's take a look at its composition:
1) Factory role: this is the core of this model, which contains certain business logic and judgment logic. In java, it is often implemented by a concrete class.
2) Abstract product role: it is generally the parent class of concrete product inheritance or the interface of implementation. It is implemented by an interface or abstract class in java.
3) specific product role: the object created by the factory class is an instance of this role. It is implemented by a concrete class in java.
Second, the factory method model
The factory method pattern is a further abstraction and generalization of the simple factory pattern, in which the factory method pattern is no longer determined by only one factory class which product class should be instantiated, this decision is left to the subclass of the abstract factory to do. Let's take a look at its composition:
1) Abstract factory role: this is the core of the factory method pattern, which is independent of the application. Is an interface that a specific factory role must implement or a parent class that must inherit. In java it is implemented by an abstract class or interface.
2) specific factory role: it contains code related to specific business logic. Called by the application to create the corresponding product-specific object.
3) Abstract product role: it is the parent class of concrete product inheritance or the interface of implementation. There are generally abstract classes or interfaces to implement in java.
4) specific product role: the object created by the specific factory role is an example of this role. It is implemented by a concrete class in java.
The factory method pattern uses multiple subclasses inherited from the abstract factory role to replace the "God class" in the simple factory pattern. As mentioned above, this shares the pressure on the object; and it makes the structure flexible-when a new product (that is, the car of the nouveau riche) is generated, as long as it is generated according to contracts provided by abstract product roles and abstract factory roles, it can be used by customers without having to modify any existing code. It can be seen that the structure of the factory role is also in line with the opening and closing principle!
The code is as follows:
/ / Abstract product role public interface Moveable {void run ();} / specific product role public class Plane implements Moveable {@ Override public void run () {System.out.println ("plane....");}} public class Broom implements Moveable {@ Override public void run () {System.out.println ("broom.");}} / Abstract Factory public abstract class VehicleFactory {abstract Moveable create () } / / specific factory public class PlaneFactory extends VehicleFactory {public Moveable create () {return new Plane ();}} public class BroomFactory extends VehicleFactory {public Moveable create () {return new Broom ();}} / / Test class public class Test {public static void main (String [] args) {VehicleFactory factory = new BroomFactory (); Moveable m = factory.create (); m.run ();}}
Third, abstract factory model
The code is as follows
/ / Abstract factory class public abstract class AbstractFactory {public abstract Vehicle createVehicle (); public abstract Weapon createWeapon (); public abstract Food createFood ();} / / concrete factory class, where Food,Vehicle,Weapon is the abstract class and public class DefaultFactory extends AbstractFactory {@ Override public Food createFood () {return new Apple ();} @ Override public Vehicle createVehicle () {return new Car ();} @ Override public Weapon createWeapon () {return new AK47 () }} / / Test class public class Test {public static void main (String [] args) {AbstractFactory f = new DefaultFactory (); Vehicle v = f.createVehicle (); v.run (); Weapon w = f.createWeapon (); w.shoot (); Food a = f.createFood (); a.printName ();}}
In the abstract factory pattern, abstract products (AbstractProduct) may be one or more, thus forming one or more product families (Product Family). In the case of only one product family, the abstract factory pattern actually degenerates to the factory method pattern.
These are all the contents of the article "how to implement the Factory pattern of the Java Design pattern". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.