常见设计模式分析
2018-01-10 15:19
176 查看
/**
* @author shenqi
* @date 2018/1/10.
*/
/**
* 单例模式
* 保证一个类仅有一个实例,并提供一个访问它的全局访问点。
* 单例模式 懒汉式
*/
public class Singleton{
private static Singleton singleton = null;
private Singleton(){}
public static Singleton getSingleton(){
if (singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
/**
* 单例模式 双重线程检查模式
*/
public class Singleton{
private static volatile Singleton sSingleton = null;
private Singleton(){}
public static Singleton getSingleton(){
Singleton singleton = sSingleton;
if (singleton == null){
synchronized (Singleton.class){
singleton = sSingleton;
if (singleton == null){
singleton = new Singleton();
sSingleton = singleton;
}
}
}
return singleton;
}
}
/**
* 工厂方法模式
*/
//抽象产品角色
public interface MoveAble{
void run();
}
//具体产品角色
public class Plane implements MoveAble{
@Override
public void run(){
System.out.println("plane");
}
}
//具体产品角色
public class Car implements MoveAble{
@Override
public void run(){
System.out.println("car");
}
}
//抽象工厂
public abstract class VehicleFactory{
abstract MoveAble create();
}
//具体工厂
public class PlaneFactory extends VehicleFactory{
public MoveAble create(){
return new Plane();
}
}
//具体工厂
public class CarFactory extends VehicleFactory{
public MoveAble create(){
return new Car();
}
}
//测试
public class test{
private static void main(String [] args){
VehicleFactory factory = new Car();
MoveAble m = factory.create();
m.run();
}
}
/**
* 建造模式 Builder
* 是一种对象构建的设计模式,它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
*/
public interface Builder{
Product getResult();
}
public class Director{
private Builder builder;
private Director(Builder builder){
this.builder = builder;
}
private void construct(){
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}
public class ConcreteBuilder implements Builder{
Part partA,partB,partC;
public void buildPartA(){
}
public void buildPartB{
}
public void buildPartC{
}
public Product getResult(){
}
}
public interface Product{}
public interface Part{}
//调用Builder模式
//ConcreteBuilder builder = new ConcreteBuilder();
//Diretor diretor = new Director(builder);
//diretor.constuct();
//Product product = builder.getResult();
/**
* 观察者模式
* 该模式定义了一种一对多的依赖关系,让多个观察者对象同时监听同一主题对象,这个主题对象在状态发生变化时,会通知会通知所有观察者对象,使它们能够自动更新自己。观察者模式又叫发布-订阅(Publish/Subscribe)模式。
*/
//先创建一个Subject类
public interface Subject{
public void attach(Observer observer);
public void detach(Observer observer);
public void notice();
}
//创建Observer类
public interface Observer{
public void updte();
}
//创建ConcreteSubject类
public class Teacher implements Subject{
private String phone;
private List<String> students;
public Teacher(){
phone = "";
students = new ArrayList<>();
}
@Override
public void attach(Observer observer){
students.add(observer);
}
@Override
public void notice(){
for(int i =0;i<students.size();i++){
((Observer)students.get(i).update());
}
}
public String getPhone(){
return phone;
}
public String setPhone(String phone){
this.phone = phone;
notice();
}
}
//创建ConcreteObserver类
public class Students implements Observer{
private String name;
private String phone;
private Teacher teacher;
public Student(String name ,Teacher t){
this.name = name;
this.teacher=t;
}
@Override
public void update(){
phone = teacher.getPhone();
}
}
/**
* 适配器模式
* 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
*/
public interface Target{
public void aaa();
public void bbb();
}
public class Adaptee{
public void aaa();
}
public class Adapter extends Adaptee implements Target{
@Override
public void bbb();
}
/**
* 装饰模式
* 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
*/
public interface Component{
void show();
}
//创建一个具体的 ConcreteComponent 来实现 Component 接口:Person.java
public class Person implements Component{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public void show(){
System.out.print("zhuangban");
}
}
//创建装饰类 Decorator 实现 Component 接口
public class Decorator implements Component{
private Component component;
public void decoratorObj(Component mcomponent){
component= mcomponent;
}
@Override
public void show(){
if (component != null){
component.show();
}
}
}
//分别创建具体的装饰类:Jeans.java , Pelisse.java, Sandal.java ...等等,分别继承 Decorator.java 类
public class Jeans extends Decorator{
@Override
public void show(){
System.out.print("穿牛仔裤");
super.show();
}
}
* @author shenqi
* @date 2018/1/10.
*/
/**
* 单例模式
* 保证一个类仅有一个实例,并提供一个访问它的全局访问点。
* 单例模式 懒汉式
*/
public class Singleton{
private static Singleton singleton = null;
private Singleton(){}
public static Singleton getSingleton(){
if (singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
/**
* 单例模式 双重线程检查模式
*/
public class Singleton{
private static volatile Singleton sSingleton = null;
private Singleton(){}
public static Singleton getSingleton(){
Singleton singleton = sSingleton;
if (singleton == null){
synchronized (Singleton.class){
singleton = sSingleton;
if (singleton == null){
singleton = new Singleton();
sSingleton = singleton;
}
}
}
return singleton;
}
}
/**
* 工厂方法模式
*/
//抽象产品角色
public interface MoveAble{
void run();
}
//具体产品角色
public class Plane implements MoveAble{
@Override
public void run(){
System.out.println("plane");
}
}
//具体产品角色
public class Car implements MoveAble{
@Override
public void run(){
System.out.println("car");
}
}
//抽象工厂
public abstract class VehicleFactory{
abstract MoveAble create();
}
//具体工厂
public class PlaneFactory extends VehicleFactory{
public MoveAble create(){
return new Plane();
}
}
//具体工厂
public class CarFactory extends VehicleFactory{
public MoveAble create(){
return new Car();
}
}
//测试
public class test{
private static void main(String [] args){
VehicleFactory factory = new Car();
MoveAble m = factory.create();
m.run();
}
}
/**
* 建造模式 Builder
* 是一种对象构建的设计模式,它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
*/
public interface Builder{
Product getResult();
}
public class Director{
private Builder builder;
private Director(Builder builder){
this.builder = builder;
}
private void construct(){
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}
public class ConcreteBuilder implements Builder{
Part partA,partB,partC;
public void buildPartA(){
}
public void buildPartB{
}
public void buildPartC{
}
public Product getResult(){
}
}
public interface Product{}
public interface Part{}
//调用Builder模式
//ConcreteBuilder builder = new ConcreteBuilder();
//Diretor diretor = new Director(builder);
//diretor.constuct();
//Product product = builder.getResult();
/**
* 观察者模式
* 该模式定义了一种一对多的依赖关系,让多个观察者对象同时监听同一主题对象,这个主题对象在状态发生变化时,会通知会通知所有观察者对象,使它们能够自动更新自己。观察者模式又叫发布-订阅(Publish/Subscribe)模式。
*/
//先创建一个Subject类
public interface Subject{
public void attach(Observer observer);
public void detach(Observer observer);
public void notice();
}
//创建Observer类
public interface Observer{
public void updte();
}
//创建ConcreteSubject类
public class Teacher implements Subject{
private String phone;
private List<String> students;
public Teacher(){
phone = "";
students = new ArrayList<>();
}
@Override
public void attach(Observer observer){
students.add(observer);
}
@Override
public void notice(){
for(int i =0;i<students.size();i++){
((Observer)students.get(i).update());
}
}
public String getPhone(){
return phone;
}
public String setPhone(String phone){
this.phone = phone;
notice();
}
}
//创建ConcreteObserver类
public class Students implements Observer{
private String name;
private String phone;
private Teacher teacher;
public Student(String name ,Teacher t){
this.name = name;
this.teacher=t;
}
@Override
public void update(){
phone = teacher.getPhone();
}
}
/**
* 适配器模式
* 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
*/
public interface Target{
public void aaa();
public void bbb();
}
public class Adaptee{
public void aaa();
}
public class Adapter extends Adaptee implements Target{
@Override
public void bbb();
}
/**
* 装饰模式
* 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
*/
public interface Component{
void show();
}
//创建一个具体的 ConcreteComponent 来实现 Component 接口:Person.java
public class Person implements Component{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Override
public void show(){
System.out.print("zhuangban");
}
}
//创建装饰类 Decorator 实现 Component 接口
public class Decorator implements Component{
private Component component;
public void decoratorObj(Component mcomponent){
component= mcomponent;
}
@Override
public void show(){
if (component != null){
component.show();
}
}
}
//分别创建具体的装饰类:Jeans.java , Pelisse.java, Sandal.java ...等等,分别继承 Decorator.java 类
public class Jeans extends Decorator{
@Override
public void show(){
System.out.print("穿牛仔裤");
super.show();
}
}
相关文章推荐
- 常见单例设计模式的对比分析
- 单例模式及常见写法分析(设计模式01)
- [学习] 常见的 PHP 设计模式分析
- 单例模式及常见写法分析(设计模式01)
- 单例模式及常见写法分析(设计模式01)
- 单例模式及常见写法分析(设计模式01)
- 常见设计模式之装饰者模式(Decorator)
- Spring 框架的设计理念与设计模式分析
- Spring 框架的设计理念与设计模式分析
- Spring 框架的设计理念与设计模式分析
- Android常见设计模式总结
- Java设计模式之原型模式的分析
- 7.偏头痛杨的常见设计模式入门系列之门面模式篇
- C++几种常见设计模式的UML图
- 常见设计模式的解析和实现(C++)之十-Proxy模式
- 常见设计模式的解析和实现(C++)之十九-Memento模式
- Dubbo源码分析(2),Dubbo中采用的设计模式
- 常见设计模式
- 常见的的设计模式-结构型模式(7种)
- 设计模式—单例模式分析