Summary of Chapter 9 of the fourth edition of Java programming thought

This chapter is very important. It involves three design patterns and some of the interfaces. Mastering these is the key point

1. Strategy design mode

Difference between template method and template method mode:

There is another example in the article:

Note: let me break it down and explain this example.

A common method is defined as an interface in which there is no implementation of the common method.

In the strategy class, a method execute is defined, and its parameters are interface classes with common methods.

The user context defines this common method when calling the execute method of strategy. This greatly improves flexibility. Because public methods can also be defined later. Instead of defining it when you create a class.

Now type this example into code

Think: if a piece of code, there will always be a lot of if Else or case, we should consider using the policy design pattern

2. Decoupling

Next, let's see how the apply class handles it?

The above example demonstrates the benefits of using interfaces to decouple classes and methods. It also involves a design pattern called adapter design pattern. There are three adapter design patterns, specifically object adapter pattern

3. Adapter design pattern

Reference article: http://blog.csdn.net/zxt0601/article/details/52848004

Definition: the adapter pattern converts the interface of a class into another interface representation expected by the client. The main purpose is compatibility, so that two classes that cannot work together due to interface mismatch can work together. Its alias is wrapper.

It belongs to structural mode

It is mainly divided into three categories: class adapter mode, object adapter mode and interface adapter mode.

The following agreements are made in this paper

One sentence describes the adapter pattern: SRC - > adapter - > dist, that is, SRC is given to the adapter in some form (class, object, interface), and the adapter finally outputs dist.

Usage scenario

Type I: adapter mode

One sentence description of the usage method: the adapter class inherits the SRC class, implements the dist interface, and completes the adaptation of SRC - > dist

Take the charger as an example: the charger itself is an adapter. The input voltage is 220V and the output voltage is 5V.

First, there is a SRC class, which itself outputs 220V voltage

Then, there is a target use voltage of 5V

Define an adapter to convert 220V voltage into 5V voltage for users

A mobile phone needs to be charged. The charging voltage is 5V

When using a mobile phone, you need to convert the 220V voltage at home to 5V output Call adapter

Output:

Summary:

I personally don't like the single inheritance mechanism of Java. Therefore, the class adapter needs to inherit the SRC class, which is a disadvantage, because it requires that the DST must be an interface, which has certain limitations; And the methods of SRC class will be exposed in the adapter, which also increases the cost of use.

But also because it inherits the SRC class, it can rewrite the methods of the SRC class according to requirements, which enhances the flexibility of the adapter.

The second type: Object Adapter Pattern

The basic idea is the same as that of the class adapter, except that the adapter class is modified. This time, it does not inherit the SRC class, but holds the SRC class instance object to solve the compatibility problem.

One sentence summary: hold SRC class, implement dist class interface, and realize SRC - > dist adaptation

(according to the "composite reuse rule", try to use combination instead of inheritance in the system)

Test results:

Summary:

Object adapter and class adapter are actually the same idea, but they are implemented in different ways. According to the composite Reuse Principle, composition is greater than inheritance, so it solves the limitation that class adapters must inherit SRC, and no longer requires DST to be an interface. Similarly, it has lower cost and more flexibility.

Type III interface adapter mode

It is also called default adapter pattern or default adapter pattern in the literature. Definition: when you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface and provide a default implementation (empty method) for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements. It is applicable to the case that an interface does not want to use all its methods.

Let's go directly to your favorite source code support link:

Source code support link:

Let's take a look at its composition first: 1) factory class role: This is the core of this pattern, which contains certain business logic and judgment logic to create products. 2) abstract product role: it is generally the parent class or implemented interface inherited by specific products. 3) Specific product role: the object created by the factory class is an instance of this role. In Java, it is implemented by a concrete class. Let's start with the opening and closing principle (open for expansion; closed for modification) analyze the simple factory mode. When the customer no longer meets the existing model number, he wants a fast new car. As long as the car complies with the contract formulated by the abstract product, he can be used by the customer as long as he notifies the factory class. Therefore, for the product part, it complies with the opening and closing principle; but the factory It seems that some are not ideal, because each new type of vehicle is added, the corresponding creation business logic must be added in the factory class (the createbmw (int type) method needs to add a case), which obviously violates the opening and closing principle. It can be imagined that the factory is very passive for the addition of new products. For such a factory class, we call it omnipotent or God class.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>