This note is from B Standing mad God said Java obtain
<> What is a design pattern
<> The significance of learning design patterns
<>GoF23
<>oop Seven principles
(1) Opening and closing principle : A software entity should be open to extensions , Close for modification ;
(2) Richter substitution principle : Inheritance must ensure that the properties owned by the superclass are still valid in the subclass
(3) Dependency Inversion Principle : Interface oriented programming , Do not implement oriented programming .
(4) Single responsibility principle : Controls the granularity of classes , Decoupling objects , Improve its cohesion .
(5) Interface isolation principle : To establish the special interfaces for each class they need
(6) Dimitt's law : Only talk to your direct friends , Don't follow “ stranger ” speak .
(7) Synthetic Reuse Principle : Try to use association relations such as combination or aggregation to realize it first , Secondly, we should consider using inheritance relationship to implement .
<> Factory mode
1, effect : Separation of creator and caller
2,00P Seven principles :
(1) Opening and closing principle : A software entity should be open to extensions , Close for modification ;
(2) Dependence Inversion Principle : Programming for interfaces , Do not program against implementations ;
(3) Dimitt's law : Only communicate with your direct friends , And avoid communicating with strangers ;
3, Core essence :
(1) Instantiated objects do not use new, Replace with factory method
(2) The implementation class will be selected , Create objects for unified management and control . This decouples the caller from our implementation class .
Simple factory mode
1.Car Interface
package com.etc.factory.simple; public interface Car { void name(); }
2. vehicle WuLing
package com.etc.factory.simple; public class WuLing implements Car{ @Override
public void name() { System.out.println(" Wuling "); } }
3. vehicle Tesla
package com.etc.factory.simple; public class Tesla implements Car{ @Override
public void name() { System.out.println(" Tesla "); } }
4.CarFactory ( Create factory )
package com.etc.factory.simple; // Simple factory mode Static factory mode // Opening and closing principle public class CarFactory
{ // Method 1 public static Car getCar(String car) { // If you add another class , The following codes need to be modified , Modify logic if (car.
equals(" Wuling ")) { return new WuLing(); }else if (car.equals(" Tesla ")) { return new
Tesla(); }else { return null; } } // Something went wrong Add a car ? // Method 2 Without modifying the original logic public static
Car getwuling() { return new WuLing(); } public static Car getTesla() { return
new Tesla(); } }
5.Consumer
package com.etc.factory.simple; public class Consumer { public static void main
(String[] args) { // 1 General creation // Car car1=new WuLing(); // Car car2=new Tesla();
// 2 Create using factory Car car1 = CarFactory.getCar(" Wuling "); Car car2 = CarFactory.getCar(
" Tesla "); car1.name(); car2.name(); } }
Factory method model
1.car Interface
package com.etc.factory.method; public interface Car { void name(); }
2.CarFactory Interface
package com.etc.factory.method; // Factory method model public interface CarFactory { Car
getCar(); }
3. vehicle WuLing
package com.etc.factory.simple; public class WuLing implements Car{ @Override
public void name() { System.out.println(" Wuling "); } }
4. Create a separate factory for Wuling WuLingFactory
package com.etc.factory.method; public class WuLingFactory implements
CarFactory { @Override public Car getCar() { return new WuLing(); } }
5. vehicle Tesla
package com.etc.factory.simple; public class Tesla implements Car{ @Override
public void name() { System.out.println(" Tesla "); } }
6. to Tesla Create a separate factory TeslaFactory
package com.etc.factory.method; public class TeslaFactory implements CarFactory
{ @Override public Car getCar() { return new Tesla(); } }
7.Consumer
package com.etc.factory.method; public class Consumer { public static void main
(String[] args) { Car car1 = new WuLingFactory().getCar(); Car car2 = new
TeslaFactory().getCar(); car1.name(); car2.name(); } }
Structural complexity : simple
Code complexity : simple
Programming complexity : simple
Management complexity : simple
According to design principles : Factory method model !
According to the actual business : Simple factory mode !
Picture interpretation
<> Abstract factory pattern
Code demonstration
1.IPhoneProduct Interface
// Mobile product interface public interface IPhoneProduct { void start(); void shutdown(); void
callup(); void sendMS(); }
2.IRouteProduct Interface
// Router product interface public interface IRouteProduct { void start(); void shutdown(); void
openWife(); void setting(); }
3. Mi phones
package com.etc.factory.abstract1; public class XiaomiPhone implements
IPhoneProduct { @Override public void start() { System.out.println(" Turn on Xiaomi mobile phone "); }
@Override public void shutdown() { System.out.println(" Turn off Xiaomi mobile phone "); } @Override
public void callup() { System.out.println(" Xiaomi called "); } @Override public void
sendMS() { System.out.println(" Xiaomi sends text messages "); } }
4. Xiaomi router
package com.etc.factory.abstract1; public class XiaomiRouter implements
IRouteProduct{ @Override public void start() { System.out.println(" Start Xiaomi router "); }
@Override public void shutdown() { System.out.println(" Turn off Xiaomi router "); } @Override
public void openWife() { System.out.println(" Open Xiaomi wifi"); } @Override public void
setting() { System.out.println(" Set up Xiaomi router "); } }
5. Huawei Mobile
package com.etc.factory.abstract1; public class HuaweiPhone implements
IPhoneProduct { @Override public void start() { System.out.println(" Turn on Huawei mobile phone "); }
@Override public void shutdown() { System.out.println(" Turn off Huawei mobile phone "); } @Override
public void callup() { System.out.println(" Huawei calls "); } @Override public void
sendMS() { System.out.println(" Huawei sends text messages "); } }
6. Huawei router
package com.etc.factory.abstract1; public class HuaweiRouter implements
IRouteProduct{ @Override public void start() { System.out.println(" Start Huawei router "); }
@Override public void shutdown() { System.out.println(" Turn off Huawei router "); } @Override
public void openWife() { System.out.println(" Start Huawei wifi"); } @Override public void
setting() { System.out.println(" Set up Huawei router "); } }
7. Abstract factory
package com.etc.factory.abstract1; // Abstract product factory public interface IProductFactory {
// Production of mobile phones IPhoneProduct iphoneProduct(); // Production router IRouteProduct routeProduct(); }
8. Millet factory
package com.etc.factory.abstract1; public class XiaomiFactory implements
IProductFactory{ @Override public IPhoneProduct iphoneProduct() { return new
XiaomiPhone(); } @Override public IRouteProduct routeProduct() { return new
XiaomiRouter(); } }
9. Huawei factory
package com.etc.factory.abstract1; public class HuaweiFactory implements
IProductFactory{ @Override public IPhoneProduct iphoneProduct() { return new
HuaweiPhone(); } @Override public IRouteProduct routeProduct() { return new
HuaweiRouter(); } }
10. Client use
package com.etc.factory.abstract1; public class Client { public static void
main(String[] args) { System.out.println("======= Xiaomi series products =========");
XiaomiFactory xiaomiFactory = new XiaomiFactory(); IPhoneProduct iPhoneProduct =
xiaomiFactory.iphoneProduct(); iPhoneProduct.callup(); iPhoneProduct.sendMS();
IRouteProduct iRouteProduct = xiaomiFactory.routeProduct(); iRouteProduct.
openWife(); System.out.println("======= Huawei series products ========="); HuaweiFactory
huaweiFactory= new HuaweiFactory(); iPhoneProduct = huaweiFactory.iphoneProduct(
); iPhoneProduct.callup(); iPhoneProduct.sendMS(); iRouteProduct = huaweiFactory
.routeProduct(); iRouteProduct.openWife(); } }
<> Builder pattern
Code demonstration
1. Abstract builder
package com.etc.factory.builder; // Abstract builder : method public abstract class Builder {
abstract void buildA();// foundation abstract void buildB();// Reinforcement works abstract void buildC()
;// Lay wires abstract void buildD();// Whitewash // to be finished : Get products abstract Product getProduct(); }
2. product : house
package com.etc.factory.builder; // product : house public class Product { private String
buildA; private String buildB; private String buildC; private String buildD;
public String getBuildA() { return buildA; } public void setBuildA(String buildA
) { this.buildA = buildA; } public String getBuildB() { return buildB; } public
void setBuildB(String buildB) { this.buildB = buildB; } public String getBuildC(
) { return buildC; } public void setBuildC(String buildC) { this.buildC = buildC
; } public String getBuildD() { return buildD; } public void setBuildD(String
buildD) { this.buildD = buildD; } @Override public String toString() { return
"Product{" + "buildA='" + buildA + '\'' + ", buildB='" + buildB + '\'' + ",
buildC='" + buildC + '\'' + ", buildD='" + buildD + '\'' + '}'; } }
3. Specific builder
package com.etc.factory.builder; // Specific builder : worker public class Worker extends
Builder { private Product product; public Worker(){ product=new Product(); }
@Override void buildA() { product.setBuildA(" foundation "); System.out.println(" foundation "); }
@Override void buildB() { product.setBuildB(" Reinforcement works "); System.out.println(" Reinforcement works ");
} @Override void buildC() { product.setBuildB(" Lay wires "); System.out.println(" Lay wires ");
} @Override void buildD() { product.setBuildB(" Whitewash "); System.out.println(" Whitewash "); }
@Override Product getProduct() { return product; } }
4. Commander
package com.etc.factory.builder; // command : core Be responsible for directing the construction of a project , How to build a project , It's up to it public class
Director { // Instruct the workers to build the house in order public Product build(Builder builder){ builder.buildA()
; builder.buildB(); builder.buildC(); builder.buildD(); return builder.
getProduct(); } }
5. test
package com.etc.factory.builder; public class Test { public static void main(
String[] args) { // command Director director = new Director(); // command Specific worker completion product
Product build = director.build(new Worker()); // The order is determined by the workers System.out.println(build
.toString()); } }
Code re understanding
1. builder
package com.etc.factory.builder2; // builder public abstract class Builder {
abstract Builder buildA(String msg);// hamburger abstract Builder buildB(String msg);
// cola abstract Builder buildC(String msg);// French fries abstract Builder buildD(String msg
);// Dessert abstract Product getProduct(); }
2. product
package com.etc.factory.builder2; // product : Set meal public class Product { private
String BuildA=" hamburger "; private String BuildB=" cola "; private String BuildC=" French fries ";
private String BuildD=" Dessert "; public String getBuildA() { return BuildA; } public
void setBuildA(String buildA) { BuildA = buildA; } public String getBuildB() {
return BuildB; } public void setBuildB(String buildB) { BuildB = buildB; }
public String getBuildC() { return BuildC; } public void setBuildC(String buildC
) { BuildC = buildC; } public String getBuildD() { return BuildD; } public void
setBuildD(String buildD) { BuildD = buildD; } @Override public String toString()
{ return "Product{" + "BuildA='" + BuildA + '\'' + ", BuildB='" + BuildB + '\''
+ ", BuildC='" + BuildC + '\'' + ", BuildD='" + BuildD + '\'' + '}'; } }
3. Specific builder
package com.etc.factory.builder2; // Specific builder public class Worker extends Builder{
private Product product; public Worker(){ product=new Product(); } @Override
Builder buildA(String msg) { product.setBuildA(msg); return this; } @Override
Builder buildB(String msg) { product.setBuildB(msg); return this; } @Override
Builder buildC(String msg) { product.setBuildC(msg); return this; } @Override
Builder buildD(String msg) { product.setBuildD(msg); return this; } @Override
Product getProduct() { return product; } }
4. test
package com.etc.factory.builder2; public class Test { public static void main(
String[] args) { // waiter Worker worker = new Worker(); // Chain programming :
On the original basis , You can combine freely , If not combined , There are also default packages Product product = worker.buildA(" Family bucket ") .buildB(
" Sprite ").getProduct(); System.out.println(product.toString());
//Product{BuildA=' Family bucket ', BuildB=' Sprite ', BuildC=' French fries ', BuildD=' Dessert '} } }
<> Prototype mode
Code demonstration demo01
1.Video
package com.etc.prototype.demo01; import java.util.Date; /** * 1. Implement an interface
Cloneable * 2. Override a method clone() */ public class Video implements Cloneable{ private
String name; private Date createTime; @Override protected Object clone() throws
CloneNotSupportedException { return super.clone(); } public Video() { } public
Video(String name, Date createTime) { this.name = name; this.createTime =
createTime; } public String getName() { return name; } public void setName(
String name) { this.name = name; } public Date getCreateTime() { return
createTime; } public void setCreateTime(Date createTime) { this.createTime =
createTime; } @Override public String toString() { return "Video{" + "name='" +
name+ '\'' + ", createTime=" + createTime + '}'; } }
2.Bilibili
package com.etc.prototype.demo01; import java.util.Date; /** * client : clone */
public class Bilibili { public static void main(String[] args) throws
CloneNotSupportedException { // Prototype object v1 Date date = new Date(); Video v1 = new
Video(" Madness theory java", date); Video v2 = (Video) v1.clone(); System.out.println("v1=>"
+v1); System.out.println("v2=>"+v2); System.out.println("======="); date.setTime
(21312231); // Nothing has changed System.out.println("v1=>"+v1); System.out.println("v2=>"+v2
); /* v1=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:39:15 CST 2022}
v2=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:39:15 CST 2022} =======
v1=>Video{name=' Madness theory java', createTime=Thu Jan 01 13:55:12 CST 1970}
v2=>Video{name=' Madness theory java', createTime=Thu Jan 01 13:55:12 CST 1970} */ } } /*
//v1 clone v2 // Video v2 = new Video(" Madness theory java", date); Video v2 = (Video)
v1.clone(); System.out.println("v2=>"+v2);
System.out.println("v2=>hash:"+v2.hashCode()); * v1=>Video{name=' Madness theory java',
createTime=Sat Feb 19 14:33:53 CST 2022} v1=>hash:1028214719
v2=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:33:53 CST 2022}
v2=>hash:500977346 v2.setName("Clone: Madness theory java"); System.out.println("v2=>"+v2);
//v2=>Video{name='Clone: Madness theory java', createTime=Sat Feb 19 14:34:49 CST 2022} */
Code demonstration demo02 Deep cloning
1.Video
package com.etc.prototype.demo02; import java.util.Date; public class Video
implements Cloneable{ private String name; private Date createTime; @Override
protected Object clone() throws CloneNotSupportedException { Object obj = super.
clone(); // Implement deep cloning ~ Video v = (Video) obj; v.createTime = (Date) this.createTime.
clone();// Clone the properties of this object as well ~ return obj; } public Video() { } public Video(String
name, Date createTime) { this.name = name; this.createTime = createTime; }
public String getName() { return name; } public void setName(String name) { this
.name = name; } public Date getCreateTime() { return createTime; } public void
setCreateTime(Date createTime) { this.createTime = createTime; } @Override
public String toString() { return "Video{" + "name='" + name + '\'' + ",
createTime=" + createTime + '}'; } }
2.Bilibili
package com.etc.prototype.demo02; import java.util.Date; // Prototype mode + Factory mode ==》new
《=》 Prototype mode public class Bilibili { public static void main(String[] args) throws
CloneNotSupportedException { // Prototype object v1 Date date = new Date(); Video v1 = new
Video(" Madness theory java", date); Video v2 = (Video) v1.clone(); System.out.println("v1=>"
+v1); System.out.println("v2=>"+v2); System.out.println("======="); date.setTime
(21312231); System.out.println("v1=>"+v1); System.out.println("v2=>"+v2); /*
v1=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:43:22 CST 2022}
v2=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:43:22 CST 2022} =======
v1=>Video{name=' Madness theory java', createTime=Thu Jan 01 13:55:12 CST 1970}
v2=>Video{name=' Madness theory java', createTime=Sat Feb 19 14:43:22 CST 2022} */ } }
<> Adapter mode
Code demonstration
1.Adaptee
package com.etc.adapter; // Class to be adapted : Network cable public class Adaptee { public void
request(){ System.out.println(" Connect the network cable to the Internet "); } }
2.NetToUsb
package com.etc.adapter; // Abstract implementation of interface converter public interface NetToUsb { // effect : Processing requests
Network cable =》usb public void handleRequest(); }
3.Adapter
package com.etc.adapter; // Real adapter Connection required USB, Connecting network cable // inherit ( Class Adapter Single inheritance ) // combination ( object adapter : Commonly used )
public class Adapter extends Adaptee implements NetToUsb{ @Override public void
handleRequest() { super.request();// You can surf the Internet } }
4.Computer
package com.etc.adapter; // Client class : Want to surf the Internet Can't plug in the network cable public class Computer {
// Our computers need to be connected to an adapter to access the Internet public void net(NetToUsb netToUsb){ // Specific implementation of Internet access ~ Find an adapter
netToUsb.handleRequest(); } public static void main(String[] args) { // computer Adapter Network cable
Computer computer = new Computer(); Adaptee adaptee = new Adaptee(); Adapter
adapter= new Adapter(); Adapter2 adapter2=new Adapter2(adaptee); //
computer.net(adapter); computer.net(adapter2); } }
Upgraded version Adapter2
package com.etc.adapter; // Real adapter Connection required USB, Connecting network cable // inherit ( Class Adapter Single inheritance ) // combination ( object adapter : Commonly used )
public class Adapter2 implements NetToUsb{ private Adaptee adaptee; public
Adapter2(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void
handleRequest() { adaptee.request(); } }
<> Bridging mode
Code demonstration
1.Brand
package com.etc.bridge; // brand public interface Brand { void info(); }
2.Lenovo Lenovo brand
package com.etc.bridge; // Lenovo brand public class Lenovo implements Brand{ @Override
public void info() { System.out.print(" association "); } }
3. Apple brand
package com.etc.bridge; // Apple brand public class Apple implements Brand{ @Override
public void info() { System.out.print(" Apple "); } }
4. combination
package com.etc.bridge; // Abstract computer type class public abstract class Computer { // combination brand
protected Brand brand; public Computer(Brand brand) { this.brand = brand; }
public void info(){ brand.info();// Own brand } } class Desktop extends Computer{
public Desktop(Brand brand) { super(brand); } @Override public void info() {
super.info(); System.out.print(" Desktop "); } } class Laptop extends Computer{ public
Laptop(Brand brand) { super(brand); } @Override public void info() { super.info(
); System.out.print(" notebook "); } }
5. test
package com.etc.bridge; public class Test { public static void main(String[]
args) { // Apple notebook Computer computer = new Laptop(new Apple()); computer.info();
System.out.println(); // Lenovo desktop Computer computer2 = new Desktop(new Lenovo());
computer2.info(); } }
Technology