工厂设计模式
equalsIgnoreCase()比较字符串和对象的方法,不计大小写
工厂设计模式就是根据if-else包装起来的方法,来生产出想要的东西。
public interface Shape { void draw(); } public class Rectangle implements Shape{ @Override public void draw() { System.out.println("我画出了一个矩形"); } } public class Circle implements Shape{ @Override public void draw() { System.out.println("我画出了一个圆"); } } public class Square implements Shape { @Override public void draw() { System.out.println("我画出了一个正方形"); } } public class ShapeFactory { public Shape getShape(String shape){ if(shape == null){ return null; }else if(shape.equalsIgnoreCase("Circle")){ return new Circle(); }else if(shape.equalsIgnoreCase("Rectangle")){ return new Rectangle(); }else if(shape.equalsIgnoreCase("Square")){ return new Square(); } return null; } } public class TestShape { public static void main(String[] args){ ShapeFactory sf = new ShapeFactory(); Shape shape1 = sf.getShape("Circle"); shape1.draw(); Shape shape2 = sf.getShape("Rectangle"); shape2.draw(); Shape shape3 = sf.getShape("Square"); shape3.draw(); } }
抽象工厂设计模式
比工厂设计模式,多了一个创建工厂的工厂。
abstract class AbstractFactory { abstract Shape getShape(String shape); abstract Printer getPrinter(String print); } public interface Shape { void draw(); } public interface Printer { void print(); } class Square implements Shape{ @Override public void draw() { System.out.println("我画了一个正方形"); } } class Circle implements Shape{ @Override public void draw() { System.out.println("我画了一个圆"); } } class PaperPrint implements Printer{ @Override public void print() { System.out.println("我用纸输出"); } } class WebPrint implements Printer{ @Override public void print() { System.out.println("我用web输出"); } } public class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice==null){ return null; }else if(choice.equalsIgnoreCase("Printer")){ return new PrinterFactory(); }else if(choice.equalsIgnoreCase("Shape")){ return new ShapeFactory(); } return null; } } public class ShapeFactory extends AbstractFactory{ @Override Shape getShape(String shape) { if(shape==null){ return null; }else if(shape.equalsIgnoreCase("Square")){ return new Square(); }else if(shape.equalsIgnoreCase("Circle")){ return new Circle(); } return null; } @Override Printer getPrinter(String print) { return null; } } public class PrinterFactory extends AbstractFactory{ @Override Shape getShape(String shape) { return null; } @Override Printer getPrinter(String print) { if(print==null){ return null; }else if(print.equalsIgnoreCase("WebPrint")){ return new WebPrint(); }else if(print.equalsIgnoreCase("PaperPrint")){ return new PaperPrint(); } return null; } } public class TestAbstratFactory { public static void main(String[] args) { AbstractFactory af = FactoryProducer.getFactory("Printer"); Printer p = af.getPrinter("WebPrint"); p.print(); AbstractFactory af1 = FactoryProducer.getFactory("Shape"); Shape shape = af1.getShape("Circle"); shape.draw(); } }
单例设计模式
单例模式是一种创建模式。
这种模式只涉及一个单独的类,它负责创建自己的对象。
该类确保只创建单个对象。
这个类提供了一种访问其唯一对象的方法。
public class MainWindows { //私有化构造器 private MainWindows(){ } private static MainWindows instance = new MainWindows(); public static MainWindows getInstance(){ return instance; } public void show(){ System.out.println("跳支舞吧"); } } public static void main(String[] args) { MainWindows mw = MainWindows.getInstance(); mw.show(); }
代理设计模式
在代理模式中,一个类代表另一个类的功能。
代理模式是一种结构模式。
在代理模式中,我们创建具有原始接口的对象,以将其功能暴露给外部世界。
public interface Printer { void print(); } class ConsolePrinter implements Printer{ private String fileName; public ConsolePrinter(String fileName) { this.fileName = fileName; } @Override public void print() { System.out.println("display: " + fileName ); } } class ProxyPrinter implements Printer{ private String fileName; private ConsolePrinter cp; public ProxyPrinter(String fileName) { this.fileName = fileName; } @Override public void print() { if(cp == null){ cp = new ConsolePrinter(fileName); } cp.print(); } } class test{ public static void main(String[] args) { Printer p = new ProxyPrinter("yhs"); p.print(); } }
观察者设计模式
观察者模式用于在一个对象被修改时通知其相关对象。
观察者模式是一种行为模式类别。
public class MyValue { private List<Observer> observers = new ArrayList<Observer>(); private int state; public int getState() { return state; } public void setState(int state) { this.state = state; notifyAllObservers(); } public void attach(Observer observer){ observers.add(observer); } public void notifyAllObservers(){ for (Observer observer:observers) { observer.update(); } } } abstract class Observer{ protected MyValue subject; public abstract void update(); } class PrinterObserver extends Observer{ public PrinterObserver(MyValue subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("Printer: " + subject.getState() ); } } class EmailObserver extends Observer{ public EmailObserver(MyValue subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("Email: " + subject.getState() ); } } class FileObserver extends Observer{ public FileObserver(MyValue subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println("FileObserver: " + subject.getState() ); } } class main{ public static void main(String[] args) { MyValue subject = new MyValue(); new FileObserver(subject); new EmailObserver(subject); new PrinterObserver(subject); subject.setState(15);//15 15 15 subject.setState(10);//10 10 10 } }
MVC模式
-
Model(模型)- 模型表示携带数据的对象。它也可以具有逻辑来更新控制器,如果其数据改变。
-
View(视图)- 视图表示模型包含的数据的可视化。通常它有UI逻辑。
-
Controller(控制器) - 控制器引用模型和视图。它控制数据流进入模型对象,并在数据更改时更新视图。它保持视图和模型分开。
public class Employee { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class EmployeeView { public void printEmployeeDetails(String name,String no){ System.out.println("Employee: "); System.out.println("Name: " + name); System.out.println("ID: " + no); } } public class EmployeeController { private Employee model; private EmployeeView view; public EmployeeController(Employee model, EmployeeView view) { this.model = model; this.view = view; } public void setEmployeeName(String name){ model.setName(name); } public String getEmployeeName(){ return model.getName(); } public void setEmployeeId(String no){ model.setId(no); } public String getEmployeeId(){ return model.getId(); } public void updateView(){ view.printEmployeeDetails(model.getName(),model.getId()); } } public static void main(String[] args) { Employee model = new Employee(); model.setName("Tom"); model.setId("1"); EmployeeView view = new EmployeeView(); EmployeeController controller = new EmployeeController(model,view); controller.updateView(); controller.setEmployeeName("New Name"); controller.updateView(); }