设计模式(2)
设计模式(2)
jit-hakase 发表于4个月前
设计模式(2)
  • 发表于 4个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 结构型

#设计模式 ##结构型模式

###适配器模式(Adapater) 把一个类的接口改变成客户端所期待的另一种接口, 使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 应用此设计模式的类库:Android开发中的ArrayAdapter SimpleAdapter BaseAdapter.

interface Target {
	public void greet();
}

class Adaptee {
	public void hello() {
		System.out.println("adaptee...");
	}
}

class Adapter implements Target {
	
	private Adaptee adaptee;
	
	public Adapter(Adaptee adaptee) {
		this.adaptee = adaptee;
	}
	@Override
	public void greet() {
		System.out.println("before hello...");
		adaptee.hello();
		System.out.println("after hello...");
	}
}

public class Main {
	
	public static void main(String[] args) {

		new Adapter(new Adaptee()).greet();
	}
}

###装饰器模式(Decorator) 装饰Apple类并给pringName方法添加日志

类似python的装饰器, python的装饰器更容易使用.

interface Fruit {  
	String getName();
	void printName();
}

class Apple implements Fruit {
	@Override
	public String getName() {
		return "apple";
	}
	@Override
	public void printName() {
		System.out.println(this.getName());
	}
}

class AppleDecorator implements Fruit {

	private Fruit apple;

	public AppleDecorator(Fruit apple) {
		this.apple = apple;
	}
	@Override
	public String getName() {
		return apple.getName();
	}
	@Override
	public void printName() {
		System.out.println("log: call getName...");
		System.out.println(this.getName());
		System.out.println("log: done getName...");
	}
}

public class Main {

	public static void main(String[] args) {

		Fruit apple = new Apple();
		Fruit appleDecorator = new AppleDecorator(apple);
		appleDecorator.printName();
	}
}

###代理模式(Proxy)

与装饰器模式类似, 主要区别在装饰器模式通常先要创造原始对象, 再使用装饰器对它装饰. 而代理模式则在内部直接创建了对象

interface Fruit {
	String getName();
	void printName();
}

class Apple implements Fruit {
	@Override
	public String getName() {
		return "apple";
	}
	@Override
	public void printName() {
		System.out.println(this.getName());
	}
}

class AppleProxy implements Fruit {
	
	private Fruit apple;
	
	public AppleProxy() {
		apple = new Apple();
	}
	@Override
	public String getName() {
		return apple.getName();
	}
	@Override
	public void printName() {
		System.out.println("log: call getName...");
		apple.printName();
		System.out.println("log: done getName...");
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		Fruit appleProxy = new AppleProxy();
		appleProxy.printName();
	}
}

###外观模式(Facade) 使用Facade类提供高层接口, 解决类与类直接的依赖关系, 降低它们的耦合度.

interface Hardware {  
	void startup();
	void shutdown();
}

class CPU implements Hardware {
	@Override
	public void startup() {
		System.out.println("CPU startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("CPU shutdown...");
	}
}

class Memory implements Hardware {
	@Override
	public void startup() {
		System.out.println("memory startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("memory shutdown...");
	}
}

class Computer implements Hardware {
	
	private CPU cpu;
	private Memory memory;
	
	public Computer() {
		cpu = new CPU();
		memory = new Memory();
	}
	@Override
	public void startup() {
		cpu.startup();
		memory.startup();
	}
	@Override
	public void shutdown() {
		memory.shutdown();
		cpu.shutdown();
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		Computer computer = new Computer();
		computer.startup();
		computer.shutdown();
	}
}

###桥接模式(Bridge) JDBC使用了桥接模式

interface DBMS {  
	void startup();
	void shutdown();
}

class MySQL implements DBMS {
	@Override
	public void startup() {
		System.out.println("mysql startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("mysql shutdown...");
	}
}

class Oracle implements DBMS {
	@Override
	public void startup() {
		System.out.println("oracle startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("oracle shutdown...");
	}
}

class DMBSBridge implements DBMS {
	
	private DBMS dbms;
	
	@Override
	public void startup() {
		dbms.startup();
	}
	@Override
	public void shutdown() {
		dbms.shutdown();
	}
	public DBMS getDBMS() {
		return dbms;
	}
	public void setDBMS(DBMS dbms) {
		this.dbms = dbms;
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		DMBSBridge dbDriver = new DMBSBridge();
		DBMS mysql = new MySQL();
		dbDriver.setDBMS(mysql);
		dbDriver.startup();
		
		DBMS oracle = new Oracle();
		dbDriver.setDBMS(oracle);
		dbDriver.startup();
	}
}

###组合模式(Composite) 通常是树型, 可以是不同类的组合, 也可以是单个类的组合.

import java.util.ArrayList;
import java.util.List;

class Menu {
	
	private String menuName;
	private int menuLevel;
	private List<Menu> subMenus = new ArrayList<>();
	
	public Menu(String menuName) {
		this.menuName = menuName;
		this.menuLevel = 1;
	}
	
	public void addMenu(Menu menu) {
		menu.menuLevel = this.menuLevel + 1;
		subMenus.add(menu);
	}
	
	public void deleteMenu(Menu menu) {
		subMenus.remove(menu);
	}

	private void printMenuLevel() {
		
		for (int i = 2; i <= menuLevel; i++) {
			System.out.print("---");
		}
		if (menuLevel != 1) {
			System.out.print(">");
		}
	}
	
	public void printMenu() {
		
		printMenuLevel();
		
		System.out.println(menuName);
		for (Menu menu : subMenus) {
			menu.printMenu();
		}
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		Menu mainMenu = new Menu("Main");
		Menu subMenuOne = new Menu("subMenuOne");
		Menu subMenuTwo = new Menu("subMenuTwo");
		mainMenu.addMenu(subMenuOne);
		mainMenu.addMenu(subMenuTwo);
		
		Menu subSubMenuOne = new Menu("subSubMenuOne");
		Menu subSubMenuTwo = new Menu("subSubMenuTwo");
		Menu subSubMenuThree = new Menu("subSubMenuThree");
		subMenuOne.addMenu(subSubMenuOne);
		subMenuTwo.addMenu(subSubMenuTwo);
		subMenuTwo.addMenu(subSubMenuThree);
		
		mainMenu.printMenu();
	}
}

###享元模式(Flyweight) 初始化大量对象以便于使用的设计模式, 类似原型.

设计一种奇怪的小写字母字体集, 长度和宽度在程序初始化时随机决定.

import java.util.HashMap;
import java.util.Map;

class Letter {
	
	private Character ch;
	private int width, height;
	
	public Letter(Character ch, int width, int height) {
		this.ch = ch;
		this.width = width;
		this.height = height;
	}
	public Letter() {}

	public Character getCh() { return ch; }
	public int getWidth() { return width; }
	public int getHeight() { return height; }
	@Override
	public String toString() {
		return "Letter [ch=" + ch + ", width=" + width + ", height=" + height + "]";
	}
}

class LetterFactory {
	
	private static LetterFactory letterFactory = new LetterFactory();
	
	private Map<Character, Letter> letters = new HashMap<>();
	
	private LetterFactory() {
		for (Character ch = 'a'; ch <= 'z'; ch++) {
			int width = (int) (Math.random()*10+10);
			int height = (int) (Math.random()*10+10);
			
			letters.put(ch, new Letter(ch, width, height));
		}
	}
	
	public Letter getLetter(char ch) {
		Letter letter = letters.get(ch);
		return letter;
	}
	
	public static LetterFactory getInstance() {
		return letterFactory;
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		LetterFactory factory = LetterFactory.getInstance();
		
		Letter letter = factory.getLetter('a');
		System.out.println(letter);
	}
}
标签: Java
  • 点赞
  • 收藏
  • 分享
粉丝 0
博文 23
码字总数 29680