下面对一些设计模式的典型代码结构总结,个人认为这是在已经写过一些代码之后熟悉设计模式的最好方式
工厂方法模式(Factory Method)
普通工厂模式
多个工厂方法模式
public class SendFactory{
public Sender produceMail(){
return new MailSender();
}
public Sender produceSms(){
return new SmsSender();
}
}
//测试类如下:
public class FactoryTest {
public static void main(String[] args) {
//先实例化具体的类,然后按需求调用方法
SendFactory factory = new SendFactory();
Sender sender = factory.produceMail();
sender.Send();
}
}
静态工厂方法模式
public class SendFactory{
public static Sender produceMail(){
return new MailSender();
}
public static Sender produceSms(){
return new SmsSender();
}
}
//测试类如下:
public class FactoryTest {
public static void main(String[] args) {
//因为是静态的,可以不用实例化而是可以直接调用
Sender sender = SendFactory.produceMail();
sender.Send();
}
}
抽象工厂模式(Abstract Factory)
实现了接口Provider
的两个工厂SendSmsFactory和SendMailFactory
,在其中的produce()方法中返回(生产出)实现了Sender()接口
的两个类MailSender和SmsSender
所以在调用时:
①先通过接口Provider
调用SendSmsFactory和SendMailFactory
②再通过Sender接口
接收SendSmsFactory和SendMailFactory
中的produce()方法返回(生产出)的MailSender和SmsSender
实例
③最后通过Sender接口
调用send()方法
public interface Sender {
public void Send();
}
//两个实现类:
public class MailSender implements Sender {
@Override
public void Send() {
System.out.println("this is mailsender!");
}
}
public class SmsSender implements Sender {
@Override
public void Send() {
System.out.println("this is smssender!");
}
}
//接口
public interface Provider {
public Sender produce();
}
//两个工厂类:
public class SendMailFactory implements Provider {
@Override
public Sender produce(){
return new MailSender();
}
}
public class SendSmsFactory implements Provider{
@Override
public Sender produce() {
return new SmsSender();
}
}
//测试类:
public class Test {
public static void main(String[] args) {
Provider provider = new SendMailFactory();
Sender sender = provider.produce();
sender.Send();
}
}
单例模式(Singleton)
public class Singleton {
/* 私有构造方法,防止被实例化 */
private Singleton() {
}
/* 此处使用一个内部类来维护单例 */
private static class SingletonFactory {
private static Singleton instance = new Singleton();
}
/* 获取实例 */
public static Singleton getInstance() {
return SingletonFactory.instance;
}
}
适配器模式
类的适配器模式
核心思想就是:有一个 Source 类,拥有一个方法,待适配,目标接口Targetable,通过
Adapter 类,将Source的功能扩展到Targetable里。
public class Source {
public void method1() {
System.out.println("this is original method!");
}
}
public interface Targetable {
/* 与原类中的方法相同 */
public void method1();
/* 新类的方法 */
public void method2();
}
public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
}
//Adapter 类继承Source类,实现Targetable接口,下面是测试类:
public class AdapterTest {
public static void main(String[] args) {
Targetable target = new Adapter();
target.method1(); //this is original method!
target.method2(); //this is the targetable method!
}
}
对象的适配器模式
public class Wrapper implements Targetable {
//1.注意这里,是将Source类当成委托
private Source source;
//2.注意这里,构造函数需传入Source对象
public Wrapper(Source source){
super();
this.source = source;
}
@Override
public void method2() {
System.out.println("this is the targetable method!");
}
@Override
public void method1() {
source.method1();
}
}
//测试
public class AdapterTest {
public static void main(String[] args) {
Source source = new Source();
Targetable target = new Wrapper(source);
target.method1();
target.method2();
}
}
接口的适配器模式
public interface Sourceable {
public void method1();
public void method2();
}
//抽象类Wrapper2:
public abstract class Wrapper2 implements Sourceable{
public void method1(){}
public void method2(){}
}
public class SourceSub1 extends Wrapper2 {
public void method1(){
System.out.println("the sourceable interface's first Sub1!");
}
}
public class SourceSub2 extends Wrapper2 {
public void method2(){
System.out.println("the sourceable interface's second Sub2!");
}
}
public class WrapperTest {
public static void main(String[] args) {
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}
装饰器模式
给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例
public interface Sourceable {
public void method();
}
public class Source implements Sourceable {
@Override
public void method() {
System.out.println("the original method!");
}
}
public class Decorator implements Sourceable {
private Sourceable source;
public Decorator(Sourceable source){
super();
this.source = source;
}
@Override
public void method() {
System.out.println("before decorator!"); //在方法前的修饰
source.method(); //被修饰的方法
System.out.println("after decorator!"); //在方法后的修饰
}
}
//测试类:
public class DecoratorTest {
public static void main(String[] args) {
Sourceable source = new Source();
Sourceable obj = new Decorator(source);
obj.method(); //调用修饰后的方法
}
}
策略模式
定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户
public interface PaymentStrategy {
public void pay(int amount);
}
public class PaypalStrategy implements PaymentStrategy {
@Override
public void pay() {
System.out.println(" paid using Paypal.");
}
}
public class CashStrategy implements PaymentStrategy {
@Override
public void pay() {
System.out.println(" paid using Cash.");
}
}
public class ShoppingCart {
public void pay(PaymentStrategy paymentMethod){
paymentMethod.pay(amount);
}
}
//测试类:
public class StrategyTest {
public static void main(String[] args) {
ShoppingCart shoppingCart=new ShoppingCart();
shoppingCart.pay(new PaypalStrategy());//" paid using Paypal."
shoppingCart.pay(new CashStrategy());//" paid using Cash."
}
}
模板模式
public abstract class OrderProcessTemplate {
public abstract void doSelect();
public abstract void doPayment();
public abstract void doDelivery();
}
public class NetOrder extends OrderProcessTemplate {
@Override
public void doSelect() { … }
@Override
public void doPayment() { … }
@Override
public void doDelivery() { … }
}
访问者模式(Visitor)
public interface Visitor {
public void visit(Subject sub);
}
public class MyVisitor implements Visitor {
@Override
public void visit(Subject sub) {
System.out.println("visit the subject:"+sub.getSubject());
}
}
//Subject 类,accept 方法,接受将要访问它的对象,getSubject()获取将要被访问的属性,
public interface Subject {
public void accept(Visitor visitor);
public String getSubject();
}
public class MySubject implements Subject {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
public String getSubject() {
return "love";
}
}
//测试:
public class Test {
public static void main(String[] args) {
Visitor visitor = new MyVisitor();
Subject sub = new MySubject();
sub.accept(visitor); //visit the subject:love
}
}