靜態(tài)工廠方法模式
// 二者共同的接口
public interface Human {
public void eat();
public void sleep();
public void beat();
}
// 創(chuàng)建實現(xiàn)類 Male
public class Male implements Human {
public void eat() {
System.out.println("Male can eat.");
}
public void sleep() {
System.out.println("Male can sleep.");
}
public void beat() {
System.out.println("Male can beat.");
}
}
// 創(chuàng)建實現(xiàn)類 Female
public class Female implements Human {
public void eat() {
System.out.println("Female can eat.");
}
public void sleep() {
System.out.println("Female can sleep.");
}
public void beat() {
System.out.println("Female can beat.");
}
}
// 多個工廠方法
public class HumanFactory {
public static Male createMale() {
return new Male();
}
public static Female createFemale() {
return new Female();
}
}
// 工廠測試類
public class FactoryTest {
public static void main(String[] args) {
Human male = HumanFactory.createMale();
male.eat();
male.sleep();
male.beat();
}
}
抽象工廠模式
// 抽象食物
interface Food {
public String getFoodName();
}
// 抽象餐具
interface TableWare {
public String getToolName();
}
// 抽象工廠
interface KitchenFactory {
public Food getFood();
public TableWare getTableWare();
}
// 具體食物 Apple 的定義如下
class Apple implements Food {
public String getFoodName() {
return "apple";
}
}
// 具體餐具 Knife 的定義如下
class Knife implements TableWare {
public String getToolName() {
return "knife";
}
}
// 以具體工廠 AKitchen 為例
class AKitchen implements KitchenFactory {
public Food getFood() {
return new Apple();
}
public TableWare getTableWare() {
return new Knife();
}
}
// 吃貨要開吃了
public class Foodaholic {
public void eat(KitchenFactory k) {
System.out.println("A foodaholic is eating "+ k.getFood().getFoodName()
+ " with " + k.getTableWare().getToolName() );
}
public static void main(String[] args) {
Foodaholic fh = new Foodaholic();
KitchenFactory kf = new AKitchen();
fh.eat(kf);
}
}
適配器模式
// 國標插頭
public interface CnPluginInterface {
void chargeWith2Pins();
}
// 實現(xiàn)國標插座的充電方法
public class CnPlugin implements CnPluginInterface {
public void chargeWith2Pins() {
System.out.println("charge with CnPlugin");
}
}
// 在國家中內(nèi)充電
public class Home {
private CnPluginInterface cnPlugin;
public Home() { }
public void setPlugin(CnPluginInterface cnPlugin) {
this.cnPlugin = cnPlugin;
}
// 充電
public void charge() {
// 國標充電
cnPlugin.chargeWith2Pins();
}
}
// 英標插頭
public interface EnPluginInterface {
void chargeWith3Pins();
}
// 實現(xiàn)英標插座的充電方法
public class EnPlugin implements EnPluginInterface {
public void chargeWith3Pins() {
System.out.println("charge with EnPlugin");
}
}
// 適配器
public class PluginAdapter implements CnPluginInterface {
private EnPluginInterface enPlugin;
public PluginAdapter(EnPluginInterface enPlugin) {
this.enPlugin = enPlugin;
}
// 這是重點吓著,適配器實現(xiàn)了英標的插頭芹助,然后重載國標的充電方法為英標的方法
@Override
public void chargeWith2Pins() {
enPlugin.chargeWith3Pins();
}
}
// 適配器測試類
public class AdapterTest {
public static void main(String[] args) {
EnPluginInterface enPlugin = new EnPlugin();
Home home = new Home();
PluginAdapter pluginAdapter = new PluginAdapter(enPlugin);
home.setPlugin(pluginAdapter);
// 會輸出 “charge with EnPlugin”
home.charge();
}
}
裝飾者模式
// 抽象類 Girl
public abstract class Girl {
String description = "no particular";
public String getDescription(){
return description;
}
}
// 美國女孩
public class AmericanGirl extends Girl {
public AmericanGirl() {
description = "+AmericanGirl";
}
}
// 國產(chǎn)妹子
public class ChineseGirl extends Girl {
public ChineseGirl() {
description = "+ChineseGirl";
}
}
// 裝飾者
public abstract class GirlDecorator extends Girl {
public abstract String getDescription();
}
// 下面以美國女孩示例
// 給美國女孩加上金發(fā)
public class GoldenHair extends GirlDecorator {
private Girl girl;
public GoldenHair(Girl g) {
girl = g;
}
@Override
public String getDescription() {
return girl.getDescription() + "+with golden hair";
}
}
// 加上身材高大的特性
public class Tall extends GirlDecorator {
private Girl girl;
public Tall(Girl g) {
girl = g;
}
@Override
public String getDescription() {
return girl.getDescription() + "+is very tall";
}
}
// 檢驗一下
public class Test {
public static void main(String[] args) {
Girl g1 = new AmericanGirl();
System.out.println(g1.getDescription());
GoldenHair g2 = new GoldenHair(g1);
System.out.println(g2.getDescription());
Tall g3 = new Tall(g2);
System.out.println(g3.getDescription());
// 你也可以一步到位
// Girl g = new Tall(new GoldenHair(new AmericanGirl()));
}
}
觀察者模式
// Subject 主題接口
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyAllObservers();
}
// 觀察者接口
public interface Observer {
public void update(Subject s);
}
// 視頻網(wǎng)站某狐 實現(xiàn) Subject 接口
public class VideoSite implements Subject{
// 觀察者列表 以及 更新了的視頻列表
private ArrayList<Observer> userList;
private ArrayList<String> videos;
public VideoSite(){
userList = new ArrayList<Observer>();
videos = new ArrayList<String>();
}
@Override
public void registerObserver(Observer o) {
userList.add(o);
}
@Override
public void removeObserver(Observer o) {
userList.remove(o);
}
@Override
public void notifyAllObservers() {
for (Observer o: userList) {
o.update(this);
}
}
public void addVideos(String video) {
this.videos.add(video);
notifyAllObservers();
}
public ArrayList<String> getVideos() {
return videos;
}
public String toString(){
return videos.toString();
}
}
// 實現(xiàn)觀察者,即看視頻的美劇迷們
public class VideoFans implements Observer {
private String name;
public VideoFans(String name){
this.name = name;
}
@Override
public void update(Subject s) {
System.out.println(this.name + ", new videos are available! ");
// print video list
System.out.println(s);
}
}
// 測試一下
public class Main {
public static void main(String[] args) {
VideoSite vs = new VideoSite();
vs.registerObserver(new VideoFans("LiLei"));
vs.registerObserver(new VideoFans("HanMeimei"));
vs.registerObserver(new VideoFans("XiaoMing"));
// add videos
vs.addVideos("Video 1");
//vs.addVideos("Video 2");
}
}
單例模式
// 靜態(tài)內(nèi)部類
public class Wife {
private static class WifeHolder {
private static final Wife wife = new Wife();
}
private Wife() { }
public static Wife getWife() {
return WifeHolder.wife;
}
}