惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
罗磊的独立博客
月光博客
月光博客
GbyAI
GbyAI
腾讯CDC
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
雷峰网
雷峰网
B
Blog RSS Feed
美团技术团队
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
D
Docker

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Strategy
Yusuf Aytas · 2010-11-01 · via Yusuf Aytas

Published · 2 min read

Bu yazımda size Strategy adındaki nesneye dayalı yazılım şablonunu (Object-Oriented Design Pattern) anlatacağım. Bu şablonun genel amacı yapılacak iş için alternatifler oluşturup, bu alternatifleride çalışma zamanında(runtime) seçebilme kabiliyetini sağlamaktır. Buna örnek verecek olursak, bir sıralama algoritması yazmak istiyoruz. Ama  bize verilen nesnelerin sayısı 30 dan küçük olduğunda insertion sort, büyük olduğunda da quick sort kullanacağız. İşte bu durumda bize, bu esnekliği sağlayan bir tasarım gerekli ki, stratejimizi çalışma zamanında değiştirerek sağlayabiliyoruz. Aşağıda verdiğimiz kodu anlatacak olursak. Şimdi bizim toplama, çıkarma, çarpma ve bölme işlemlerimiz var. Fakat biz hangisini kullanacağımıza çalışma zamanında karar vereceğiz, bu durumda aşağıda uyguladığımız gibi stratejimizi oluşturuyoruz. Eğer işlemi değiştirmek istiyorsak setInstruction methodunu çağırıyoruz.  Böylece sadece işlem yapan nesneyi değiştirerek, işlemimizi değiştirebiliyoruz.

Strategy tasarım deseni şemasıStrategy tasarım deseni şeması

public class Calculator {

    public static void main(String[] args) {
        Executer ex = new Executer();

        ex.setInstruction(new Addition());
        System.out.println(ex.execute(3, 4));

        ex.setInstruction(new Substraction());
        System.out.println(ex.execute(3, 4));

        ex.setInstruction(new Multiplication());
        System.out.println(ex.execute(3, 4));

        ex.setInstruction(new Division());
        System.out.println(ex.execute(3, 4));
    }
}

class Executer {
    private Instruction instruction;

    public void setInstruction(Instruction instruction) {
        this.instruction = instruction;
    }

    public int execute(int x, int y) {
        return instruction.execute(x, y);
    }
}

interface Instruction {
    int execute(int x, int y);
}

class Addition implements Instruction {
    @Override
    public int execute(int x, int y) {
        return x + y;
    }
}

class Substraction implements Instruction {
    @Override
    public int execute(int x, int y) {
        return x - y;
    }
}

class Multiplication implements Instruction {
    @Override
    public int execute(int x, int y) {
        return x * y;
    }
}

class Division implements Instruction {
    @Override
    public int execute(int x, int y) {
        return x / y;
    }
}