Merge pull request #15059 from wxy/20190715-Understanding-software-design-patterns

PRF:20190715 Understanding software design patterns
This commit is contained in:
Xingyu.Wang 2019-08-19 18:01:05 +08:00 committed by GitHub
commit 1a5a8a4a88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -69,17 +69,17 @@ package org.opensource.demo.singleton;
public class OpensourceSingleton { public class OpensourceSingleton {
    private static OpensourceSingleton uniqueInstance; private static OpensourceSingleton uniqueInstance;
    private OpensourceSingleton() { private OpensourceSingleton() {
    } }
    public static OpensourceSingleton getInstance() { public static OpensourceSingleton getInstance() {
        if (uniqueInstance == null) { if (uniqueInstance == null) {
            uniqueInstance = new OpensourceSingleton(); uniqueInstance = new OpensourceSingleton();
        } }
        return uniqueInstance; return uniqueInstance;
    } }
} }
``` ```
@ -102,20 +102,20 @@ package org.opensource.demo.singleton;
public class ImprovedOpensourceSingleton { public class ImprovedOpensourceSingleton {
    private volatile static ImprovedOpensourceSingleton uniqueInstance; private volatile static ImprovedOpensourceSingleton uniqueInstance;
    private ImprovedOpensourceSingleton() {} private ImprovedOpensourceSingleton() {}
    public static ImprovedOpensourceSingleton getInstance() { public static ImprovedOpensourceSingleton getInstance() {
        if (uniqueInstance == null) { if (uniqueInstance == null) {
            synchronized (ImprovedOpensourceSingleton.class) { synchronized (ImprovedOpensourceSingleton.class) {
                if (uniqueInstance == null) { if (uniqueInstance == null) {
                    uniqueInstance = new ImprovedOpensourceSingleton(); uniqueInstance = new ImprovedOpensourceSingleton();
                } }
            } }
        } }
        return uniqueInstance; return uniqueInstance;
    } }
} }
``` ```
@ -141,20 +141,20 @@ package org.opensource.demo.factory;
public class OpensourceFactory { public class OpensourceFactory {
    public OpensourceJVMServers getServerByVendor([String][18] name) { public OpensourceJVMServers getServerByVendor(String name) {
        if(name.equals("Apache")) { if(name.equals("Apache")) {
            return new Tomcat(); return new Tomcat();
        } }
        else if(name.equals("Eclipse")) { else if(name.equals("Eclipse")) {
            return new Jetty(); return new Jetty();
        } }
        else if (name.equals("RedHat")) { else if (name.equals("RedHat")) {
            return new WildFly(); return new WildFly();
        } }
        else { else {
            return null; return null;
        } }
    } }
} }
``` ```
@ -164,9 +164,9 @@ public class OpensourceFactory {
package org.opensource.demo.factory; package org.opensource.demo.factory;
public interface OpensourceJVMServers { public interface OpensourceJVMServers {
    public void startServer(); public void startServer();
    public void stopServer(); public void stopServer();
    public [String][18] getName(); public String getName();
} }
``` ```
@ -176,17 +176,17 @@ public interface OpensourceJVMServers {
package org.opensource.demo.factory; package org.opensource.demo.factory;
public class WildFly implements OpensourceJVMServers { public class WildFly implements OpensourceJVMServers {
    public void startServer() { public void startServer() {
        [System][19].out.println("Starting WildFly Server..."); System.out.println("Starting WildFly Server...");
    } }
    public void stopServer() { public void stopServer() {
        [System][19].out.println("Shutting Down WildFly Server..."); System.out.println("Shutting Down WildFly Server...");
    } }
    public [String][18] getName() { public String getName() {
        return "WildFly"; return "WildFly";
    } }
} }
``` ```
@ -209,9 +209,9 @@ package org.opensource.demo.observer;
public interface Topic { public interface Topic {
    public void addObserver([Observer][22] observer); public void addObserver(Observer observer);
    public void deleteObserver([Observer][22] observer); public void deleteObserver(Observer observer);
    public void notifyObservers(); public void notifyObservers();
} }
``` ```
@ -226,39 +226,39 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
public class Conference implements Topic { public class Conference implements Topic {
    private List&lt;Observer&gt; listObservers; private List<Observer> listObservers;
    private int totalAttendees; private int totalAttendees;
    private int totalSpeakers; private int totalSpeakers;
    private [String][18] nameEvent; private String nameEvent;
    public Conference() { public Conference() {
        listObservers = new ArrayList&lt;Observer&gt;(); listObservers = new ArrayList<Observer>();
    } }
    public void addObserver([Observer][22] observer) { public void addObserver(Observer observer) {
        listObservers.add(observer); listObservers.add(observer);
    } }
    public void deleteObserver([Observer][22] observer) { public void deleteObserver(Observer observer) {
        int i = listObservers.indexOf(observer); int i = listObservers.indexOf(observer);
        if (i &gt;= 0) { if (i >= 0) {
            listObservers.remove(i); listObservers.remove(i);
        } }
    } }
    public void notifyObservers() { public void notifyObservers() {
        for (int i=0, nObservers = listObservers.size(); i &lt; nObservers; ++ i) { for (int i=0, nObservers = listObservers.size(); i < nObservers; ++ i) {
            [Observer][22] observer = listObservers.get(i); Observer observer = listObservers.get(i);
            observer.update(totalAttendees,totalSpeakers,nameEvent); observer.update(totalAttendees,totalSpeakers,nameEvent);
        } }
    } }
    public void setConferenceDetails(int totalAttendees, int totalSpeakers, [String][18] nameEvent) { public void setConferenceDetails(int totalAttendees, int totalSpeakers, String nameEvent) {
        this.totalAttendees = totalAttendees; this.totalAttendees = totalAttendees;
        this.totalSpeakers = totalSpeakers; this.totalSpeakers = totalSpeakers;
        this.nameEvent = nameEvent; this.nameEvent = nameEvent;
        notifyObservers(); notifyObservers();
    } }
} }
``` ```
@ -269,8 +269,8 @@ public class Conference implements Topic {
``` ```
package org.opensource.demo.observer; package org.opensource.demo.observer;
public interface [Observer][22] { public interface Observer {
    public void update(int totalAttendees, int totalSpeakers, [String][18] nameEvent); public void update(int totalAttendees, int totalSpeakers, String nameEvent);
} }
``` ```
@ -281,27 +281,27 @@ public interface [Observer][22] {
``` ```
package org.opensource.demo.observer; package org.opensource.demo.observer;
public class MonitorConferenceAttendees implements [Observer][22] { public class MonitorConferenceAttendees implements Observer {
    private int totalAttendees; private int totalAttendees;
    private int totalSpeakers; private int totalSpeakers;
    private [String][18] nameEvent; private String nameEvent;
    private Topic topic; private Topic topic;
    public MonitorConferenceAttendees(Topic topic) { public MonitorConferenceAttendees(Topic topic) {
        this.topic = topic; this.topic = topic;
        topic.addObserver(this); topic.addObserver(this);
    } }
    public void update(int totalAttendees, int totalSpeakers, [String][18] nameEvent) { public void update(int totalAttendees, int totalSpeakers, String nameEvent) {
        this.totalAttendees = totalAttendees; this.totalAttendees = totalAttendees;
        this.totalSpeakers = totalSpeakers; this.totalSpeakers = totalSpeakers;
        this.nameEvent = nameEvent; this.nameEvent = nameEvent;
        printConferenceInfo(); printConferenceInfo();
    } }
    public void printConferenceInfo() { public void printConferenceInfo() {
        [System][19].out.println(this.nameEvent + " has " + totalSpeakers + " speakers and " + totalAttendees + " attendees"); System.out.println(this.nameEvent + " has " + totalSpeakers + " speakers and " + totalAttendees + " attendees");
    } }
} }
``` ```