One factory model
The factory model is literally understandable, is that we need an instance where we don't need to go to new one, but we generate it through a factory, so the benefit of the design is that if a class is replaced, then we don't need to change it on the client, as long as we modify it in the Workshop class to reduce the dependencies between classes.
Code instance:
Package com.example.springboot_test.test; /** * Com.xinguangnet.tuchao.merchant.manage * *@Author: Wukn *@Date: 2018/2/5//** * Factory design mode * * * * * */interface Persion {//define interface public void eat ();//define a method} class Aa Implem
Ents persion {@Override public void eat () {System.out.println ("Aa----------");
} class Bb implements persion {@Override public void eat () {System.out.println ("Bb-----------");
} class Factorytest {public static persion Factoryclass (String str) {persion persion = null;
if (Str.equals ("Aa")) {persion = new Aa ();
} if (Str.equals ("Bb")) {persion = new Bb ();
return persion;
/** * Note that it is best to write a string to the front when judging a string, so that you can prevent null pointer exceptions *if ("Aa". Equals (str)) {persion = new Aa ();
} if ("BB". Equals (str)) {persion = new Bb (); * * */Static class TestClass {public static void main (string[] args) {persion persion = Fact
Orytest.factoryclass ("Aa");
Persion.eat ();
}
}}
Two-agent mode
Package com.example.springboot_test.test; /** * Com.xinguangnet.tuchao.merchant.manage * *@Author: Wukn *@Date: 2018/2/5 * * * Agent design mode//** * Define a rental interface this interface landlord and intermediary to achieve why to define a common interface, my understanding is that, whether the landlord or intermediary to rent out the house must send a rental advertising/public
Interface Proxytest {public void sent ();} Class Fangdong implements Proxytest {//for the landlord his purpose is to rent out the house, who rented the house he does not care @Override public void sent () {Sys TEM.OUT.PRINTLN (Landlord Direct rent ...)
");
} class Zhongjie implements Proxytest {//For the intermediary, his purpose is to rent out the landlord's house, intermediary will set rent and rent to who private Fangdong fangdong;
Public Zhongjie (Fangdong fangdong) {this.fangdong = Fangdong;
public void Money () {System.out.println ("pay");
public void Stay () {System.out.println ("check-in");
@Override public void sent () {money ();
Fangdong.sent ()//This is the real rental information, the content of the intermediary agent sent (); } class Testsent {public static void main (string[] args) {/** *//The person who rented the house found an intermediary, he did not know whether this House is direct rent, he just
Find an intermediary, intermediary to rent the landlord's house to the people who need to rent * * * proxytest proxytest = new Zhongjie (new Fangdong ()); Proxytest.sent ()//rental completed} *
/**
* In the beginning of the entire rental to complete the need to think about what each person completed,
* Landlord:
* landlord realized the public interface
* landlord will rent the house advertising message * *
intermediary:
* Intermediary implementation of the public interface
* intermediary agent of the landlord's rental advertising information
* intermediary to enrich the process of renting *
* Customer:
* find intermediary, pay rent * * * *
in the
above process, my understanding is that the so-called agent design, in fact, two of the same purpose of the same class public areas to implement a common business interface, these two classes one is the original class, one is the
* proxy class, The original class is actually instantiated in the proxy class, in this way in the proxy class can be the proxy class and the original class of public methods to enhance, there is a prior enhancement, and so on,
* for the client, only need to focus on the implementation of proxy classes and do not need to focus on specific business components, reduce the coupling between the modules. Help to adapt to more business scenarios.
*
*
Triple Adapter Mode
Package com.example.springboot_test.test; /** * Com.xinguangnet.tuchao.merchant.manage * *@Author: Wukn *@Date: 2018/2/5//** * Adapter Mode/interface Adapertest {/** * For an interface, if a class implements this interface, it must implement the method in this interface, * But in specific business scenarios, sometimes we don't need to implement the methods in the interface, we may only need a few methods, * but just an empty method body, and then use a normal class to inherit the interface, the specific code can be written in the ordinary class inside the method body, * This design idea is to set
Adapter mode.
* * public void A1 ();
public void A2 ();
Public String B1 ();
Public Integer B2 ();
Abstract class Aaa implements Adapertest {/** * Empty method body */@Override public void A1 () {}
@Override public String B1 () {return null;
Class Shoe extends Aaa {@Override public void A2 () {System.out.println ("ddd");
@Override Public Integer b2 () {integer A = 123;
return A;
}/** * * In the above program can actually have a question is why the abstract class can not implement the interface of all methods. * * My understanding is that since it is an abstract class, if you have to implement all the methods of the interface. Before that, there was no point in the specification of the interface, * Abstract class is defined in an abstract class if it is not inherited then the creation of this abstract class has no meaning, just imagine, if the abstract class all implement the method of the interface, * all operations in the abstract class, do not need to be inherited by other classes, then the
of the normative contradictions.
*
*
*
*
*
*/
The relationship between an abstract class and an interface