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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
D
Docker
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
L
LangChain Blog
月光博客
月光博客
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
T
Tailwind CSS Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
B
Blog
G
Google Developers Blog
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
C
Check Point Blog
V
V2EX
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
罗磊的独立博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
量子位
博客园 - 司徒正美
博客园 - 叶小钗

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 Prisoner's Dilemma Climbing No More The Weekly Win Mevlana Candy Brewing Turkish Tea Onboarding Your Engineering Manager Technical Deep Dives Yapay Zekâ Çağında Bilgisayar Mühendisliği Building Remote Teams From Idea to Launch in 2 Weeks Reflecting on Software Engineering Handbook Representing the Business New Manager Survival Guide Take Self Reviews Seriously Chasing Real Respect The Invisible Difference Learning the Johari Window Management is a Lonely Place Simple Task Management AI Balance in Work PIP Manager Insights Engineering Manager Interview Preparation Work-Life Balance as a Manager Bridging the Management Disconnect Tech Hiring Bubble Bursts Traits for EMs Simple Acts of Recognition Matter The Question I Ask Every New Report The Reality of an Employer's Market Bridging Ideals and Reality Hiring Red Flags Why The Godfather Is So Damn Good Subteam Tenets No Fluff Please Losing a Top Performer Balancing Act of Reliability Building Trust in Engineering Teams Ideal Number of Direct Reports Overriding a People Leader’s Decision From Misperception to Promotion Perception vs Perspective Setting Goals From Engineer to Manager Getting Delegation Right Interviewing Your Future Boss Celebrating Our Book in Iceland Operational Skills Needed On Writing Software Engineering Handbook Charlie Munger Quotes Working with Dependencies From Las Vegas to Canyons Navigating Layoffs Handling Competitive Dynamics A Weekend Getaway to Malta Engineering Health Essentials Should Dev Managers Code? Confronting the Life on Pause Winning Eleven Kindness is A Choice Bireysel Katılımcılar ve Yöneticiler Leading from Where You Are The Subtle Art of Listening Coding in Leadership The Power of Consistency The Making of a Leader The Path to Leadership Embracing TikTok Talent Sourcing Journey Leading Self Managing Teams Cracking Coding Bottlenecks
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;
    }
}