1.单例模式
package yll.singleModule;
/**
* 单例模式
* 负载均衡器的设计与实现
* @author yg
*
*/
public class ClientTest {
public static void main(String args[]) {
//创建四个LoadBalancer对象
LoadBalancer balancer1,balancer2,balancer3,balancer4;
balancer1 = LoadBalancer.getLoadBalancer();
balancer2 = LoadBalancer.getLoadBalancer();
balancer3 = LoadBalancer.getLoadBalancer();
balancer4 = LoadBalancer.getLoadBalancer();
//判断服务器负载均衡器是否相同
if (balancer1 == balancer2 && balancer2 == balancer3 && balancer3 == balancer4) {
System.out.println("服务器负载均衡器具有唯一性!");
}
//增加服务器
balancer1.addServer("Server 1");
balancer1.addServer("Server 2");
balancer1.addServer("Server 3");
balancer1.addServer("Server 4");
balancer2.addServer("Server 5");
//模拟客户端请求的分发
for (int i = 0; i < 10; i++) {
String server = balancer1.getServer();
System.out.println("分发请求至服务器: " + server);
}
}
}
package yll.singleModule;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 单例模式
* 负载均衡器的设计与实现
* @author yg
*
*/
class LoadBalancer{
//私有静态成员变量,存储唯一实例
private static LoadBalancer instance = null;
//服务器集合
private List serverList = null;
//私有构造函数
private LoadBalancer() {
serverList = new ArrayList();
}
//延迟加载 懒汉式单例
//公有静态成员方法,返回唯一实例
public static LoadBalancer getLoadBalancer() {
if (instance == null) {
synchronized(LoadBalancer.class) {
//双重检查锁定
if (instance == null) {
instance = new LoadBalancer();//创建单例实例
}
}
}
return instance;
}
//增加服务器
public void addServer(String server) {
serverList.add(server);
}
//删除服务器
public void removeServer(String server) {
serverList.remove(server);
}
//使用Random类随机获取服务器
public String getServer() {
Random random = new Random();
int i = random.nextInt(serverList.size());
return (String)serverList.get(i);
}
}
package yll.singleModule;
/**
* 一种更好的单例实现方法
* @author yg
*
*/
class Singleton {
private Singleton() {
}
//静态(static)内部类
private static class HolderClass {
private final static Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return HolderClass.instance;
}
public static void main(String args[]) {
Singleton s1, s2;
s1 = Singleton.getInstance();
s2 = Singleton.getInstance();
System.out.println(s1==s2);//true
}
}
2.工厂模式
package yll.factoryModule;
/**
* 工厂模式例子
* 图表工厂类:工厂类
* @author yg
*
*/
public class ChartFactory {
//静态工厂方法
public static Chart getChart(String type) {
Chart chart = null;
if (type.equalsIgnoreCase("histogram")) {
chart = new HistogramChart();
System.out.println("初始化设置柱状图!");
}
else if (type.equalsIgnoreCase("pie")) {
chart = new PieChart();
System.out.println("初始化设置饼状图!");
}
else if (type.equalsIgnoreCase("line")) {
chart = new LineChart();
System.out.println("初始化设置折线图!");
}
return chart;
}
}
package yll.factoryModule;
/**
* 抽象图表接口:抽象产品类
*
* @author yg
*
*/
interface Chart {
/*
* 展示方法
*/
public void display();
}
package yll.factoryModule;
/**
* 工厂模式
* @author g
*
*/
public class ChartTest {
public static void main(String[] args) {
Chart pieChart = null;
pieChart=ChartFactory.getChart("pie");
pieChart.display();
System.out.println("-----------------");
HistogramChart histogramChart = null;
histogramChart=(HistogramChart) ChartFactory.getChart("histogram");
histogramChart.display();
histogramChart.createHistogramChart();
System.out.println("-----------------");
LineChart lineChart=new LineChart();
lineChart.createLineChart();
lineChart.display();
}
}
package yll.factoryModule;
/**
* 柱状图
* @author yg
*
*/
public class HistogramChart implements Chart {
@Override
public void display() {
System.out.println("柱状图展示!");
}
public void createHistogramChart() {
System.out.println("创建柱状图!");
}
}
package yll.factoryModule;
/**
* 折线图
*
* @author yg
*
*/
public class LineChart implements Chart {
@Override
public void display() {
// TODO 自動生成されたメソッド・スタブ
System.out.println("折线图展示!");
}
public void createLineChart() {
System.out.println("创建折线图!");
}
}
package yll.factoryModule;
/**
* 饼状图
* @author yg
*
*/
public class PieChart implements Chart {
@Override
public void display() {
// TODO 自動生成されたメソッド・スタブ
System.out.println("饼状图展示!");
}
public void createPieChart() {
System.out.println("创建饼状图!");
}
}
3.原型模式
package yll.cloneModule;
/**
* 对象的克隆——原型模式
* @author yg
*
*/
public class ConcretePrototypeTest implements Prototype{
private String attr; // 成员属性
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setAttr(String attr) {
this.attr = attr;
}
public String getAttr() {
return this.attr;
}
public Prototype clone() { // 克隆方法
Prototype prototype = new ConcretePrototypeTest(); // 创建新对象
((ConcretePrototypeTest) prototype).setAttr(this.attr);
((ConcretePrototypeTest) prototype).setStudent(this.student);
return prototype;
}
public static void main(String[] args) {
Prototype obj1 = new ConcretePrototypeTest();
((ConcretePrototypeTest) obj1).setAttr("Sunny");
Student stu = new Student();
stu.setGender("FM");
stu.setIpAddress("AHUDFWE");
stu.setName("Meary");
((ConcretePrototypeTest) obj1).setStudent(stu);
Prototype obj2 = obj1.clone();
System.out.println("obj1:"+((ConcretePrototypeTest) obj1).getAttr());
System.out.println("obj1 Student:"+((ConcretePrototypeTest) obj1).getStudent().toString());
System.out.println("obj2:"+((ConcretePrototypeTest) obj2).getAttr());
System.out.println("obj2 Student:"+((ConcretePrototypeTest) obj2).getStudent().toString());
System.out.println(((ConcretePrototypeTest) obj1).getAttr() == ((ConcretePrototypeTest) obj2).getAttr());
System.out.println("Student equal :"+((ConcretePrototypeTest) obj1).getStudent().equals(((ConcretePrototypeTest) obj2).getStudent()));
Student stu2 = new Student();
((ConcretePrototypeTest) obj2).setAttr("AYR");
stu2.setGender("M");
stu2.setName("Jack");
((ConcretePrototypeTest) obj2).setStudent(stu2);
System.out.println("----------------------");
System.out.println("obj1 Attr:"+((ConcretePrototypeTest) obj1).getAttr());
System.out.println("obj1 Student:"+((ConcretePrototypeTest) obj1).getStudent().toString());
System.out.println("obj2 Attr:"+((ConcretePrototypeTest) obj2).getAttr());
System.out.println("obj2 Student:"+((ConcretePrototypeTest) obj2).getStudent().toString());
System.out.println(((ConcretePrototypeTest) obj1).getAttr() == ((ConcretePrototypeTest) obj2).getAttr());
System.out.println("Student equal:"+((ConcretePrototypeTest) obj1).getStudent().equals(((ConcretePrototypeTest) obj2).getStudent()));
}
}
package yll.cloneModule;
/**
* 对象的克隆——原型模式
*
* @author yg
*
*/
public class CloneTest implements Cloneable {
private String attr; // 成员属性
private Student student;
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setAttr(String attr) {
this.attr = attr;
}
public String getAttr() {
return this.attr;
}
public CloneTest clone() { // 克隆方法
Object obj = null;
try {
obj = super.clone();
return (CloneTest) obj;
} catch (CloneNotSupportedException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
System.out.println("不支持复制!");
return null;
}
}
public static void main(String[] args) {
CloneTest obj1, obj2;
obj1 = new CloneTest();
Student stu = new Student();
obj1.setStudent(stu);
obj2 = obj1.clone();
System.out.println("obj equal :" + (obj1 == obj2));
System.out.println("ATTR equal :" + (obj1.getAttr() == obj2.getAttr()));
System.out.println("Student equal :" + (obj1.getStudent() == obj2.getStudent()));
}
}
package yll.cloneModule;
public interface Prototype {
Prototype clone();
}
package yll.cloneModule;
public class Student {
String name;
String ipAddress;
String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student [name=" + name + ", ipAddress=" + ipAddress + ", gender=" + gender + "]";
}
}