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

推荐订阅源

博客园_首页
博客园 - 【当耐特】
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
D
Docker
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
罗磊的独立博客
小众软件
小众软件
A
About on SuperTechFans
MyScale Blog
MyScale Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
C
Check Point Blog
L
LangChain Blog

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;
    }
}