1、享元模式
说到享元模式,一个想到的应该就是池技术了,String常量池、数据库连接池、缓冲池等等都是享元模式的应用,所以说享元模式是池技术的重要实现方式。
享元模式(Flyweight),运用共享技术有效地支持大量细粒度的对象。
享元模式使用场景
1 系统中存在大量相似对象
2 频繁使用到的对象或大概率使用到的对象,才有共享的必要。并且要保证线程安全。
package test1;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class test1 {
public static void main(String[] args) {
TreeNode treeNode1 = new TreeNode(3, 4, TreeFactory.getTree("xxx", "xxxxx"));
TreeNode treeNode2 = new TreeNode(3, 8, TreeFactory.getTree("xxx", "xxxxx"));
TreeNode treeNode3 = new TreeNode(4, 4, TreeFactory.getTree("yyy", "xxxxx"));
TreeNode treeNode4 = new TreeNode(5, 8, TreeFactory.getTree("yyy", "xxxxx"));
}
}
class TreeNode{
private int x;
private int y;
private Tree tree;
public TreeNode(int x, int y, Tree tree) {
this.x = x;
this.y = y;
this.tree = tree;
}
}
class Tree{
private final String name;
private final String data;
public Tree(String name, String data) {
System.out.println("name:" + name + " tree created.");
this.name = name;
this.data = data;
}
public String getName() {
return name;
}
public String getData() {
return data;
}
@Override
public String toString() {
return "tree{" +
"name='" + name + '\'' +
", data='" + data + '\'' +
'}';
}
}
class TreeFactory{
//线程安全
private static Map<String, Tree> map = new ConcurrentHashMap<>();
public static Tree getTree(String name,String data){
if (map.containsKey(name)) {
return map.get(name);
}
Tree tree = new Tree(name, data);
map.put(name, tree);
return tree;
}
}
2、门面模式
(1)原始:
package test1;
public class test2 {
public static void main(String[] args) {
Client1 client1 = new Client1();
Client2 client2 = new Client2();
Client3 client3 = new Client3();
client1.doSomething1();
client2.doSomething2();
client3.doSomething3();
}
}
class Client1 {
SubSystem1 subSystem1=new SubSystem1();
SubSystem2 subSystem2=new SubSystem2();
SubSystem3 subSystem3=new SubSystem3();
public void doSomething1(){
subSystem1.method1();
subSystem2.method2();
subSystem3.method3();
}
}
class Client2 {
SubSystem1 subSystem1=new SubSystem1();
SubSystem2 subSystem2=new SubSystem2();
SubSystem3 subSystem3=new SubSystem3();
public void doSomething2(){
subSystem1.method1();
subSystem2.method2();
subSystem3.method3();
}
}
class Client3 {
SubSystem1 subSystem1=new SubSystem1();
SubSystem2 subSystem2=new SubSystem2();
SubSystem3 subSystem3=new SubSystem3();
public void doSomething3(){
subSystem1.method1();
subSystem2.method2();
subSystem3.method3();
}
}
class SubSystem1{
public void method1(){
System.out.println("SubSystem1.method1 excuted.");
}
}
class SubSystem2{
public void method2(){
System.out.println("SubSystem2.method2 excuted.");
}
}
class SubSystem3{
public void method3(){
System.out.println("SubSystem3.method3 excuted.");
}
}
(2)改造后:门面模式
package test1;
public class test2 {
public static void main(String[] args) {
Client1 client1 = new Client1();
Client2 client2 = new Client2();
Client3 client3 = new Client3();
client1.doSomething1();
client2.doSomething2();
client3.doSomething3();
}
}
class Client1 {
Facade facade = new Facade();
public void doSomething1(){
facade.doSomethingFacade();
}
}
class Client2 {
Facade facade = new Facade();
public void doSomething2(){
facade.doSomethingFacade();
}
}
class Client3 {
Facade facade = new Facade();
public void doSomething3(){
facade.doSomethingFacade();
}
}
class Facade{
SubSystem1 subSystem1=new SubSystem1();
SubSystem2 subSystem2=new SubSystem2();
SubSystem3 subSystem3=new SubSystem3();
public void doSomethingFacade(){
subSystem1.method1();
subSystem2.method2();
subSystem3.method3();
}
}
class SubSystem1{
public void method1(){
System.out.println("SubSystem1.method1 excuted.");
}
}
class SubSystem2{
public void method2(){
System.out.println("SubSystem2.method2 excuted.");
}
}
class SubSystem3{
public void method3(){
System.out.println("SubSystem3.method3 excuted.");
}
}
3、适配器模式
充电器:220V转换为对应电压
(1) 对象适配器模式
本质使用的是 组合
package test1;
public class test3 {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.output5V();
}
}
class Adaptee{
public int output220V(){
return 220;
}
}
interface Target{
int output5V();
}
class Adapter implements Target{
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public int output5V() {
int i = adaptee.output220V();
System.out.println("原始电压转换为5v电压");
return 5;
}
}
(2) 类的适配器模式
本质使用的是 继承
package test1;
public class test3 {
public static void main(String[] args) {
Adapter adapter = new Adapter();
adapter.output5V();
}
}
class Adaptee{
public int output220V(){
return 220;
}
}
interface Target{
int output5V();
}
// 类的 适配器模式
class Adapter extends Adaptee implements Target{
@Override
public int output5V() {
int i = output220V();
System.out.println("原始电压转换为5v电压");
return 5;
}
}