Mode learning self-understanding and doubts (1)

zhaozj2021-02-16  67

?

The factory model is dedicated to it will have a lot of

Class of common interface

Instantiate

,

And don't have to know which one is to instite in advance

Pattern of a class.

?

The factory model has the following forms

:

Simple factory

(Simple Factory)

mode

Factory method

(Factory Method)

mode

Abstract factory

(Abstract Factory)

mode

Simple factory model

Parent class: product (interface)

Subcoundation: Specific products (inheritance products)

Factory: Responsible for creating specific products

In simple factory model

,

A factory class is in the center position of the product instantiation call

,

It decides that the product class should be case

Chemize

,

Like a traffic police station in the mobile vehicle stream

,

Decided to release which one of the vehicles flow in the direction.

For example, fruits, there are a variety of fruits

Gardener (factory) will use them. Different fruit examples through the factory

2. Factory method mode

?

Factory method model is a further abstract and promotion of simple factory models.

?

It is smarter than the simple factory model lies in

,

It no longer be used as a

The appearance of the body's traffic police

,

But

Transportation Police Party (factory)

The appearance appears.

The specific police become an executor of a factory method

.

?

?

Factory method mode

No longer only by one factory class to decide which product

Class should be instantiated

,

This decision is handed over to the subclass.

Some specific factories create some specific product (the product is not single, a variety of products)

Such as plants, score, vegetable

Gardener (factory) gardener (only fruit), gardener of vegetables

3. Abstract factory model

Factory subregion, product subregion

A specific plant will no longer create a specific product, but can create a variety of specific products.

For example, plant, fry fruit, vegetables, fruits and vegetables can be northern models; or southern species.

Gardener (factory) has the gardener of the South, the gardener of the northerners, they all have fruits and vegetables.

4. Document mode

?

A single-state class can only have an example. Such a class is often used

Conduct resource management.

?

?

For example: Many software have databases, in general,

entire

Software should use a joint channel instead of any

I want to open a join channel when I want it.

?

An example is usually obtained with getInstance ().

characteristic:

1, only one instance of the single class.

2, it must create a unique example of yourself.

3. It must provide yourself for all other classes.

Finally, the monitish class is not limited in theory and practice, but it is easy to extend to any limited instance of any limited instance.

Hungry Chinese single class

?

Since the constructor is private, this class cannot be inherited.

?

Package com.javapatterns.singleton.demos;

Public class eagersingleton {private

EAGERSIINGLETON () {}

Public static elersionleton getinstance () {

Return M_INSTANCE;

}

Private static final EAGERSINGLETON M_INSTANCE =

New

EAGERSITON ();

Lazy

The lazy monocular class will instantiate yourself during the first quoted

?

Structure is private, so this kind cannot be inherited

?

Public class lazySingleton {

?

Private lazySingleton () {}

?

/ **

?

* Static factory method, returning such unique examples. Because when you create an instance,

?

* Recommended resource initialization, so it is best to use synchronization mechanisms.

?

* /

?

Synchronized public static lazysingleton getInstance () {

?

IF (m_instance == null) {

?

m_instance = new lazysingleton ();

?

}

?

Return M_INSTANCE;

?

}

?

?

Private static lazysingleton m_instance = null;

?

}

Comparison of two types

?

Hungry Chinese monitish class will instantiate himself when loading itself.

?

?

Single from the perspective of resource utilization efficiency, this is slightly less than the lazy order.

?

?

From the perspective of speed and reaction time, it is slightly better than the lazy order.

?

?

Lazy monitony class must handle the access restrictions on the internal key segments of the function in multiple threads while instantiation.

Lite-state often uses when the database is connected

?

转载请注明原文地址:https://www.9cbs.com/read-15996.html

New Post(0)