デザインパターン
この記事では主にサンプルコードとクラス図に基づいてデザインパターンを、説明していません。これは、一定の基盤を持っているし、デザインパターンを確認したい人のために適している。あなたは詳細な説明を表示したい場合は、こちらをクリックしてくださいために詳細なデザインパターン。クラス図はすべて手作業で描かれています。クラス図を理解してください。
工場モード
// 顶层人类接口
public interface Person {
void say();
}
// 中国人实现类
public class Chinese implements Person {
public void say() {
System.out.println("中国人说话:你好~");
}
}
// 法国人实现类
public class French implements Person {
public void say() {
System.out.println("法国人说话:bonjour~");
}
}
// 美国人实现类
public class American implements Person {
public void say() {
System.out.println("美国人说话:Hello~");
}
}
// 人类工厂
public class PersonFactory {
public static Person getPerson(String person) {
if (person == null)
return null;
if (person.equalsIgnoreCase("CHINESE"))
return new Chinese();
if (person.equalsIgnoreCase("AMERICAN"))
return new American();
if (person.equalsIgnoreCase("FRENCH"))
return new French();
return null;
}
}
// 测试工厂模式
public class FactoryTest {
public static void main(String[] args) {
Chinese chinese = (Chinese) PersonFactory.getPerson("chinese");
chinese.say();
French french = (French) PersonFactory.getPerson("french");
french.say();
American american = (American) PersonFactory.getPerson("american");
american.say();
}
}
の結果
中国人说话:你好~
法国人说话:bonjour~
美国人说话:Hello~
UML类图
抽象ファクトリパターン
// 顶层人类接口
public interface Person {
void say();
}
// 中国人实现类
public class Chinese implements Person {
public void say() {
System.out.println("中国人说话:你好~");
}
}
// 法国人实现类
public class French implements Person {
public void say() {
System.out.println("法国人说话:bonjour~");
}
}
// 美国人实现类
public class American implements Person {
public void say() {
System.out.println("美国人说话:Hello~");
}
}
// 顶层工作接口
public interface Job {
void work();
}
// 医生实现类
public class Doctor implements Job {
public void work() {
System.out.println("医生要治病救人");
}
}
// 司机实现类
public class Driver implements Job {
public void work() {
System.out.println("司机要拉载乘客");
}
}
// 老师实现类
public class Teacher implements Job {
public void work() {
System.out.println("老师要教育孩子");
}
}
// 顶层抽象工厂接口
public interface AbstractFactory {
Person getPerson(String person);
Job getJob(String job);
}
// 人类工厂实现类
public class PersonFactory implements AbstractFactory{
public Person getPerson(String person) {
if (person == null)
return null;
if (person.equalsIgnoreCase("CHINESE"))
return new Chinese();
if (person.equalsIgnoreCase("AMERICAN"))
return new American();
if (person.equalsIgnoreCase("FRENCH"))
return new French();
return null;
}
public Job getJob(String job) {
return null;
}
}
// 工作工厂实现类
public class JobFactory implements AbstractFactory{
public Person getPerson(String person) {
return null;
}
public Job getJob(String job) {
if (job == null)
return null;
if (job.equalsIgnoreCase("DOCTOR"))
return new Doctor();
if (job.equalsIgnoreCase("TEACHER"))
return new Teacher();
if (job.equalsIgnoreCase("DRIVER"))
return new Driver();
return null;
}
}
// 测试抽象工厂模式
public class AbstractFactoryTest {
public static void main(String[] args) {
AbstractFactory personFactory = new PersonFactory();
Chinese chinese = (Chinese) personFactory.getPerson("chinese");
chinese.say();
AbstractFactory jobFactory = new JobFactory();
Doctor doctor = (Doctor) jobFactory.getJob("doctor");
doctor.work();
}
}
の結果
中国人说话:你好~
医生要治病救人
UML类图
シングルトンモード
- シングルトンモードでは、システムメモリにこのクラスのオブジェクトが1つだけ存在するため、システムリソースが節約されます。頻繁に作成および破棄する必要がある一部のオブジェクトでは、シングルトンモードを使用するとシステムパフォーマンスを向上させることができます。
- シングルトンクラスをインスタンス化する場合は、newを使用する代わりに、オブジェクトを取得するための対応するメソッドを使用することを忘れないでください。
- シングルトンモードで使用されるシナリオ:頻繁に作成および破棄する必要があるオブジェクト、時間またはリソースを消費するオブジェクトの作成(つまり、重量のあるオブジェクト)が、頻繁に使用されるオブジェクト、ツールオブジェクト、データベースまたはファイルにアクセスする頻繁なオブジェクト(データソース、セッションファクトリなど)
// 饿汉式单例(没有懒加载,推荐单线程使用)
public class SingletonHungry {
private static SingletonHungry instance = new SingletonHungry();
private SingletonHungry() {
}
public static SingletonHungry getInstance() {
return instance;
}
}
// 懒汉式单例(懒加载,线程不安全)
public class SingletonLazy {
private static SingletonLazy instance;
private SingletonLazy() {
}
public static SingletonLazy getInstance() {
if (instance == null)
instance = new SingletonLazy();
return instance;
}
}
//双重检查(线程安全,效率高)
public class SingletonDoubleCheck {
private static volatile SingletonDoubleCheck instance;
private SingletonDoubleCheck() {
}
public static SingletonDoubleCheck getInstance() {
if (instance == null) {
synchronized (SingletonDoubleCheck.class) {
if (instance == null)
instance = new SingletonDoubleCheck();
}
}
return instance;
}
}
の結果
测试饿汉单例:true
测试懒汉单例:true
测试双重检测单例:true
UML类图
エージェンシーモデル
// 顶层找对象的接口(代理者和被代理都要同时实现该接口)
public interface FindLover {
void blindDate();
}
// 程序员实现找对象接口(被代理者)
public class Programmer implements FindLover{
public void blindDate() {
System.out.println("卑微程序员相亲找对象");
}
}
// 婚介所实现找对象接口(代理者)
public class DatingService implements FindLover{
private Programmer programmer;
public DatingService(Programmer programmer) {
this.programmer = programmer;
}
public void blindDate() {
beforeBlindDate();
this.programmer.blindDate();
afterBlindDate();
}
public void beforeBlindDate() {
System.out.println("不敢迈出第一步,还是找婚介所帮忙吧");
}
public void afterBlindDate() {
System.out.println("我一个35的程序员居然还能找到对象,我何德何能...");
}
}
// 测试代理模式
public class ProxyTest {
public static void main(String[] args) {
Programmer you = new Programmer();
FindLover datingService = new DatingService(you);
datingService.blindDate();
}
}
の結果
不敢迈出第一步,还是找婚介所帮忙吧
卑微程序员相亲找对象
我一个35的程序员居然还能找到对象,我何德何能...
UML类图
オブザーバーモード
// 程序员(被观察者)
public class Programmer {
private List<Observer> observers = new ArrayList<Observer>();
private boolean hasGirlFirend;
public boolean isHasGirlFirend() {
return hasGirlFirend;
}
public void setHasGirlFirend(boolean hasGirlFirend) {
this.hasGirlFirend = hasGirlFirend;
if (hasGirlFirend == true)
notifyAllObservers();
}
public void attach(Observer observer) {
observers.add(observer);
}
public void notifyAllObservers() {
for (Observer observer : observers) {
observer.knowMsg();
}
}
}
// 观察者抽象类
public abstract class Observer {
protected Programmer programmer;
abstract void knowMsg();
}
// 朋友 1 作为观察者
public class Friend1 extends Observer{
public Friend1(Programmer programmer) {
this.programmer = programmer;
this.programmer.attach(this);
}
void knowMsg() {
System.out.println("说好的一起代码到终老,你怎么能这样~");
}
}
// 朋友 2 作为观察者
public class Friend2 extends Observer{
public Friend2(Programmer programmer) {
this.programmer = programmer;
this.programmer.attach(this);
}
void knowMsg() {
System.out.println("你再也不是爸爸的好儿子了,耗子为汁~");
}
}
// 测试观察者模式
public class TestObserver {
public static void main(String[] args) {
Programmer you = new Programmer();
// 将两个朋友添加到观察队列里
new Friend1(you);
new Friend2(you);
// 程序员找到对象会通知所有朋友
you.setHasGirlFirend(true);
}
}
の結果
说好的一起代码到终老,你怎么能这样~
你再也不是爸爸的好儿子了,耗子为汁~
UML类图
戦略モード
// 策略接口
public interface Strategy {
void doOperation();
}
// 策略实现类 1
public class LowKey implements Strategy{
public void doOperation() {
System.out.println("刚入职要低调做事~");
}
}
// 策略实现类 2
public class WorkCrazy implements Strategy{
public void doOperation() {
System.out.println("入职之后要发奋工作~");
}
}
// 策略实现类 3
public class DeleteData implements Strategy{
public void doOperation() {
System.out.println("压力太大,删库跑路~");
}
}
// 策略启动类
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void doOperation() {
this.strategy.doOperation();
}
}
// 测试策略模式
public class StrategyTest {
public static void main(String[] args) {
Context context = new Context(new LowKey());
// 执行第一个策略
context.doOperation();
// 执行第二个策略
context = new Context(new WorkCrazy());
context.doOperation();
// 执行第三个策略
context = new Context(new DeleteData());
context.doOperation();
}
}
の結果
刚入职要低调做事~
入职之后要发奋工作~
压力太大,删库跑路~
UML类图
更新中…